Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Making a Question repeat with Loops

1. ## 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!

• 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.

• ## 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?

• 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.

•

#### Posting Permissions

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