Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

finnschi

macrumors 6502
Original poster
Dec 30, 2008
460
0
Hamburg, Germany
Hey guys i am planning on Programming a little Java Programm, that shows the amount of money earned in a certain time but i don't know how to Programm something that updates itself every 30 seconds or so...


1. User input : "how much money do you earn per hour"

2. calculate money earned per second

3. Display Current Amout earned every 30 seconds


i would start like this ...

Code:
package countdownpack;
import java.util.Scanner;





public class countdown {
	
	// TODO Auto-generated method stub
	
	
	static Scanner sc = new Scanner(System.in);
	
	public static void main(String[] args) {
	
		
		System.out.print("how much Money do you earn in 1 Hour?");
		double money = sc.nextDouble();
		
		double mps = (money/3600);
		System.out.print("you earn"+ mps + " Money per second"   );
		
		System.out.print("Start Working now? yes/no ");
		String s1 = sc.next();
		char answer = s1.charAt(0);
		
		if (answer == 'y'); { 
			
		????????????????????????????????????????	
			
			
			
		}else {
		exit();
			System.out.println("END");
			
			
		
		
		

}
}
}

Maybe you guys can help me out here :D
 
Code:
while(true) {
  //calculate and display
  Thread.sleep(30000);
}

Put that in where your ????? is, and fill in the calculation and display.

You can wrap the sleep in a try-catch, but probably not needed at this stage.

-Lee
 
Wow thank you for your quick reply, but it didn't quite work, I had to wrap it but thats okay! its almost perfect, but now I need to know how I can tell the Programm to re calculate (add 1 mps each time it displays the current amount earned, right now its just displaying the same value over and over again... :D

Code:
package countdownpack;
import java.util.Scanner;

public class countdown {
	
	// TODO Auto-generated method stub
	
	
	static Scanner sc = new Scanner(System.in);
	
	public static void main(String[] args) {
	
		
		System.out.print("how much Money do you earn in 1 Hour?");
		double money = sc.nextDouble();
		
		double mps = (money/3600);
		System.out.print("you earn"+ mps + " Money per second"   );
		
		System.out.print("Start Working now? yes/no ");
		String s1 = sc.next();
		char answer = s1.charAt(0);
		
		if (answer == 'y'); { 
			
			while(true) {
				  //calculate and display
				double currentmoney= (+mps);
				  try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				  
				  
				  System.out.print("Your current money earned is "+ currentmoney );
				
			}		
			
		}
}
}
 
Change:
double currentmoney= (+mps);
to:
double currentmoney+= mps;

If you want to do what you said originally:
double currentmoney+= mps*30.;
and change the sleep to 30000.

-Lee
 
Oops, didn't even look at the code. Declare currentmoney before the loop:
double currentmoney = 0.;

Then in the loop:
currentmoney += mps;

I decided it would be cooler to have it display it every second :D

Nothing stresses a user out than a program updating too fast. You're just getting started, and 1 second isn't too fast, but at a call center job I had the call tracking software could update the call length every millisecond. It was devastating to your psyche if you hit that accidentally.

-Lee
 
Oops, didn't even look at the code. Declare currentmoney before the loop:
double currentmoney = 0.;

Then in the loop:
currentmoney += mps;

-Lee

OMG that totally worked :D

now just 1 more things

can I somehow easily display currentmoney in the console using a new line for each new amount, right now its showing like this:

Your current money earned is 0.002777777777777778Your current money 333336Your current money earned is 0.13611111111111113

everything in 1 line I need to somehow add a <br> after each output..., i am coming from HTML as you can see... :rolleyes:


You are awesome btw
 
Substitute print with println.

-Lee

ha wow thank you I wanted to accomplish doing this for month.. but I finally got around doing it it feels great... first java Program Wohooo :D

Thank you so much for helping me !!!


if you know a good source for learning how i can now turn this into a little Java Applet , it would be GREAT !! I want to display it on a website so I can turn it On whenever i start working and have it running in the Background and take a peek every couple of Minutes to check how much I have earned :D
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.