Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Making a Question repeat with Loops

    Hello,

    I have an assignment where I need to have a question about the temperature loop after giving you the answer. For example:

    What is the temp? 0 degrees

    Is that in Farenheit or Celsius? Press 1 for Farenheit, press 2 for Celsius - 2

    0.0000 degrees celsius is 32.000 Farenheit.

    Would you like to continue?
    0 = No, 1 = Yes.


    If the person puts 1, the program needs to ask the original question: What is the temp?

    If they put 0, then it is suppose to say "Good bye!"

    I got everything working correctly until the part where it loops. Here is my code:

    import java.util.Scanner;

    public class practice {
    public static void main(String [] args) {
    Scanner input = new Scanner(System.in);


    System.out.println("Please enter the temperature you would like to convert - ");


    double temperature = input.nextDouble();

    System.out.print("Enter 1 if the temperature you entered is in degrees Fahrenheit and 2 if it is in degrees Celsius - ");
    int scale = input.nextInt();

    double celsius = (5.0 / 9) * (temperature - 32);
    double fahrenheit = (9 / 5.0) * temperature + 32;{

    }
    if (scale == 2)
    System.out.printf("%.5f degrees Celsius = %.3f degrees Fahrenheit.\nWould you like to continue? ( 0 = No ; 1 = Yes ) - \n", temperature, fahrenheit);
    else if (scale == 1)

    System.out.printf("%.3f degrees Fahrenheit = %.5f degrees Celsius.\nWould you like to continue? ( 0 = No ; 1 = Yes ) - \n", temperature, celsius);}

    }



    Anyone have any idea how to make the loop to ask the original question again? Thanks in advance!

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Simply wrap the entire thing in a do/while loop:
    PHP Code:
    do
    {
        
    // your stuff
    }
    while (
    input != 0); 
    You may want to indicate that the user should enter 0 to stop. Scanner also needs to be declared outside of the loop to be accessible in the while.

    Edit:
    sorry my bad, you asked for a continue. That will work fine nonetheless.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    New to the CF scene
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Java loops for repeating question

    Thanks for your input. I think I am one step closer. The problem that I am having is that it doesn't give me a chance to answer if I want to go again. It just asks the question without giving me a chance to say 0 (no) or 1 (yes). Here is what I have now.


    import java.util.Scanner;

    public class practice {
    public static void main(String [] args) {
    Scanner input = new Scanner(System.in);
    int choice = 0;
    do {
    System.out.println("Please enter the temperature you would like to convert - ");


    double temperature = input.nextDouble();

    System.out.print("Enter 1 if the temperature you entered is in degrees Fahrenheit and 2 if it is in degrees Celsius - ");
    int scale = input.nextInt();

    double celsius = (5.0 / 9) * (temperature - 32);
    double fahrenheit = (9 / 5.0) * temperature + 32;{

    }
    if (scale == 2)
    System.out.printf("%.5f degrees Celsius = %.3f degrees Fahrenheit.\nWould you like to continue? ( 0 = No ; 1 = Yes ) - \n", temperature, fahrenheit);
    else if (scale == 1)

    System.out.printf("%.3f degrees Fahrenheit = %.5f degrees Celsius.\nWould you like to continue? ( 0 = No ; 1 = Yes ) - \n", temperature, celsius);
    }
    while (choice == 0);
    System.out.println("Good bye!");


    }
    }


    What am I doing wrong?

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    You need to populate choice with input.nextInt. It can be done after the if/elseif check on the scale so you only need to add it once.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    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
    •