Results 1 to 3 of 3
- 11-05-2011, 07:23 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 6
- Rep Power
- 0
Question about allowing the user jump back to the start of the program? Help please!
This is my program to calculate Speed, distance or time, given the other two variables. This is working perfectly, thankfully. However, I now wish to add a prompt at the end asking if the user would like to go again, I have been fiddling around with it but can't seem to find anything that will work. Basically if the user answers "yes" to question " Would you like to start again?", I want the program to go again for him. Assumed there would be something simple but haven't found anything yet!
Help truly appreciated!
Jack
import java.util.Scanner;
class SDTCalc{
public static void main(String [] args){
double dist;
double time;
double speed;
String target;
Scanner jack = new Scanner(System.in);
System.out.println("Are you looking for speed, distance or time? ");
target = jack.nextLine();
if (target.equals("Speed") || target.equals("speed")) {
System.out.println("Please enter a value for Distance (km): ");
dist = jack.nextDouble();
System.out.println("Please enter a value for Time (hours): ");
time = jack.nextDouble();
System.out.println("Answer: " + dist/time + " km/h ");
}
else if (target.equals("Distance") || target.equals("distance")) {
System.out.println("Please enter a value for Speed (km/h): ");
speed = jack.nextDouble();
System.out.println("Please enter a value for Time (hours): ");
time = jack.nextDouble();
System.out.println("Answer: " + speed*time + " km ");
}
else if (target.equals("Time") || target.equals("time")) {
System.out.println("Please enter a value for Speed (km/h): ");
speed = jack.nextDouble();
System.out.println("Please enter a value for Distance (km): ");
dist = jack.nextDouble();
System.out.println("Answer: " + dist/speed + " hours ");
}
else{
System.out.println("Oooooooops " + target + " is not a valid choice! ");
}
}
}
- 11-05-2011, 07:29 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Re: Question about allowing the user jump back to the start of the program? Help plea
I (and others) answered this exact same question here: http://www.java-forums.org/new-java/...tml#post243375.
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
- 11-06-2011, 09:32 AM #3
Member
- Join Date
- Jul 2011
- Location
- New Delhi,India
- Posts
- 56
- Rep Power
- 0
Re: Question about allowing the user jump back to the start of the program? Help plea
just add the code in a do{}while loop....
in loop add this too-
BufferedReader br=new BufferedReader(new InputStreamReader(System.in);
and at the end of the loop,i.e. in the last line add this-
try{
System.out.println("do u want to continue?<yes/no>");
choice=br.readLine();//make a String choice at the beginning of the program
}catch(IOException ie){System.out.println(ie);}
and in the end add condition to the while loop-
do{}while(choice.equals("yes"));
so the code should be like this-
do{
BuffereReader br=new BufferedReader(new InputStreamReader(System.in));
//your code here
try{
System.out.println("do u want to continue?<yes/no>");
choice=br.readLine();//make a String choice at the beginning of the program
}catch(IOException ie){System.out.println(ie);}
}while(choice.equals("yes));
Similar Threads
-
how to loop through an array and jump back to beginning
By drhinri in forum New To JavaReplies: 4Last Post: 02-21-2011, 04:52 AM -
Start Swing GUI program by Java Web Start with IE in Eclipse debug mode
By albertkao in forum EclipseReplies: 1Last Post: 01-18-2011, 07:27 PM -
How to start a for loop back to top without starting the var count over?
By AcousticBruce in forum New To JavaReplies: 3Last Post: 01-07-2011, 01:23 AM -
[Help] Change to give back program
By Duranx in forum New To JavaReplies: 5Last Post: 02-06-2010, 04:52 AM -
How do you start a Java program from the "Start" menu under Windows?
By ScottVal in forum New To JavaReplies: 5Last Post: 03-20-2009, 11:04 PM
Bookmarks