mo1090 - 2 years ago 83
Java Question

# Loop - Do , while

I tried doing a loop, but the "{" symbols prevent the all the print lines from repeating I think. I want the loop to redisplay the entire first string after the user finishes entering values for the sequence.

``````import java.util.Scanner;

public class FtoC{

static Scanner cin = new Scanner (System.in);

public static void main(String[] args)
{
char userInput = ' ';
System.out.print("\nEnter " + "\"F\"" + " or " + "\"f\"" + " for Fahrenheit to Celsius"
+ "\nEnter " + "\"C\"" + " or " +  "\"c\"" + " for Celsius to Fahrenheit"
+ "\nEnter something else to quit." +"\n");

userInput = cin.next().charAt(0);

if ((userInput == 'F') || (userInput =='f'))
{print("Enter a temp in Fahrenheit"
+"\n I will tell you temp in Celsius" +"\n");
far2cel();
}
else if ((userInput == 'C') || (userInput =='c')) {
print("Enter a temp in Celsius:"
+"\n I will tell you temp in fahrenheit" +"\n");
cel2far();
} else {
print("Terminating the program" + "\n");
}
}

private static void cel2far() {
Scanner input = new Scanner(System.in);
Double celsius = input.nextDouble();
print(celsius + " celsius is " + ((celsius * 9 / 5.0) + 32)
+ " Fahrenheit");

}

private static void far2cel() {
Scanner input = new Scanner(System.in);

Double Fahrenheit = input.nextDouble();
print(Fahrenheit + " Fahrenheit is " + ((Fahrenheit - 32) * (5 / 9.0))
+ " celsius");

}

private static void print(String string) {
System.out.print("\n" + string);
}
}
``````

I don't think that I get what you want exactly, but this way you can run the program until user enters something different than "F", "f", "C", "C" in the first part of the program.

``````import java.util.Scanner;

public class FtoC{

static Scanner cin = new Scanner (System.in);

public static void main(String[] args)
{
while(true) {
char userInput = ' ';
System.out.print("\nEnter " + "\"F\"" + " or " + "\"f\"" + " for Fahrenheit to Celsius"
+ "\nEnter " + "\"C\"" + " or " +  "\"c\"" + " for Celsius to Fahrenheit"
+ "\nEnter something else to quit." +"\n");

userInput = cin.next().charAt(0);

if ((userInput == 'F') || (userInput =='f'))
{print("Enter a temp in Fahrenheit"
+"\n I will tell you temp in Celsius" +"\n");
far2cel();
}
else if ((userInput == 'C') || (userInput =='c')) {
print("Enter a temp in Celsius:"
+"\n I will tell you temp in fahrenheit" +"\n");
cel2far();
} else {
print("Terminating the program" + "\n");
break;
}
}
}

private static void cel2far() {
Scanner input = new Scanner(System.in);
Double celsius = input.nextDouble();
print(celsius + " celsius is " + ((celsius * 9 / 5.0) + 32)
+ " Fahrenheit");

}

private static void far2cel() {
Scanner input = new Scanner(System.in);

Double Fahrenheit = input.nextDouble();
print(Fahrenheit + " Fahrenheit is " + ((Fahrenheit - 32) * (5 / 9.0))
+ " celsius");

}

private static void print(String string) {
System.out.print("\n" + string);
}
}
``````

I have placed it into while loop and added "break;" in the "Terminating program" part.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download