我有一個小問題,這個do while
循環;當我運行該程序時,它至少部分地工作,我的意思是首先你需要做出從C到F或從F到C的轉換的選擇,並且在你輸入值之後程序停止了我想要做的是繼續詢問值,直到你輸入3.我試圖用do while
循環做到這一點,但它不工作,所以如果有人有任何想法,我將不勝感激。下面是代碼:有問題做了... while Loop
import java.util.Scanner;
public class DegreesInConversion2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Conversion table: ");
int choice = input.nextInt();
do {
System.out.println();
System.out.println("1 for convertion from Celsious to Fahrenhait: ");
System.out.println("2 for convertion froom Fahrenheit to Celsious: ");
System.out.println("3 for Exit: ");
System.out.println();
System.out.println("Make a choice between 1 - 3 ");
choice = input.nextInt();
System.out.println();
switch (choice) {
case 1:
System.out.println("Enter temperature in Celsious: ");
double cel = input.nextDouble();
if (cel < -273.15) {
System.out.println("Invalid values, please enter temperature greater than -273.15 in C:");
} else {
System.out.println("You enetered " + cel + "C " + "which is " + (((cel * 9)/5) + 32) + "F");
}
break;
case 2:
System.out.println("Enter temperature in Farhneit: ");
double far = input.nextDouble();
if (far < -459.67) {
System.out.println("Invalid values, please enter temperature greater than -459.67 in F:");
} else {
System.out.println("You enetered " + far + "F " + "which is " + (((far - 32) * 5)/9) + "C");
}
break;
case 3:
System.out.println("Goodbyu have a nice day: ");
break;
default:
System.out.println("Invalid entry: Please enter a number between 1-3:");
}
} while (choice != 3);
}
}
我改變它,比當我編譯我「在線程異常‘得到這個消息的主要’java.lang.Error的:未解決的問題,編譯: \t選擇不能解析爲一個可變 \t在DegreesInConversion2.main(DegreesInConversion2.java:48)」 – Kiril
其中線48是}而(選擇= 3!); – Kiril
您確定將'choice = input.nextInt();'改爲了'int choice = input。nextInt()'? – Nivas