我試着做一個循環,但「{」符號防止所有的打印行重複我認爲。我希望循環在用戶完成輸入序列值後重新顯示整個第一個字符串。循環 - 做,而
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
「''符號可以防止所有打印行重複」呃,不。有時間閱讀舊的教程。 –