我试着做一个循环,但“{”符号防止所有的打印行重复我认为。我希望循环在用户完成输入序列值后重新显示整个第一个字符串。循环 - 做,而
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);
}
}
我没有看到一个循环。你可以编辑这个来发布你试过的循环代码吗? – ajb
您是否认真询问了一个do-while循环,而您的代码中没有单个循环?试试这个:https://www.google.com/search?q=java+do+while&oq=java+do+while – aliteralmind
“''符号可以防止所有打印行重复”呃,不。有时间阅读旧的教程。 –