Results 1 to 3 of 3
  1. #1
    JavaProg is offline Member
    Join Date
    Nov 2011
    Posts
    6
    Rep Power
    0

    Default 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! ");
    }
    }
    }

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,652
    Blog Entries
    7
    Rep Power
    21

    Default 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: how do i reload my program?.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Cosmos is offline Member
    Join Date
    Jul 2011
    Location
    New Delhi,India
    Posts
    56
    Rep Power
    0

    Default 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

  1. Replies: 4
    Last Post: 02-21-2011, 03:52 AM
  2. Replies: 1
    Last Post: 01-18-2011, 06:27 PM
  3. Replies: 3
    Last Post: 01-07-2011, 12:23 AM
  4. [Help] Change to give back program
    By Duranx in forum New To Java
    Replies: 5
    Last Post: 02-06-2010, 03:52 AM
  5. Replies: 5
    Last Post: 03-20-2009, 10:04 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •