英文:
Java - default option does not fuction in switch
问题
import java.util.Scanner;
public class Temperature {
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
double fahrenheit, celcius, kelvin;
System.out.println("Choose type of temperature:\nf. Fahrenheit\nc. Celsius\nk. Kelvin");
String word = scan.nextLine();
switch(word){
case "f":
System.out.println("Enter Fahrenheit temperature: ");
fahrenheit = scan.nextDouble();
celcius = (fahrenheit - 32) * 5/9;
kelvin = (fahrenheit + 459.67) * 5/9;
System.out.println("" + celcius + " °C");
System.out.println("" + fahrenheit + " °F");
System.out.println("" + kelvin + " K");
break;
case "c":
System.out.println("Enter Celsius temperature: ");
celcius = scan.nextDouble();
fahrenheit = (celcius * 9) / 5 + 32;
kelvin = celcius + 273.15;
System.out.println("" + celcius + " °C");
System.out.println("" + fahrenheit + " °F");
System.out.println("" + kelvin + " K");
break;
case "k":
System.out.println("Enter Kelvin temperature: ");
kelvin = scan.nextDouble();
fahrenheit = 1.8 * (kelvin - 273.15) + 32;
celcius = kelvin - 273.15;
System.out.println("" + celcius + " °C");
System.out.println("" + fahrenheit + " °F");
System.out.println("" + kelvin + " K");
break;
default:
System.out.println("You entered an invalid choice, please choose again");
}
scan.close();
}
}
英文:
hello I wrote a program to convert one temperature from one to another using switch.
the default does not fuction:
When i enter a valid option it works perfectly fine, but when i choose invalid choice it goes the default outputting the lines: You enter invalid choice, please choose again and stop workings without allowing me to choose another choice.
earlier when I tested it, the default allowed me to choose another option.
import java.util.Scanner;
public class Tempature {
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
double fahrenheit,celcius,kelvin;
System.out.println("Choose type of temperature:\nf. Fahrenheit\nc. Celcius\nk. Kelvin");
String word = scan.nextLine();
switch(word){
case "f": System.out.println("Enter Fahrenheit temperature: ");
fahrenheit=scan.nextDouble();
celcius = (fahrenheit-32) * 5/9;
kelvin = (fahrenheit + 459.67) * 5/9;
System.out.println("" + celcius + " C");
System.out.println("" + fahrenheit + " F");
System.out.println("" + kelvin + " K");
break;
case "c": System.out.println("Enter the Celcius temperature: ");
celcius=scan.nextDouble();
fahrenheit = (celcius*9)/5 + 32;
kelvin = celcius + 273.15;
System.out.println("" + celcius + " C");
System.out.println("" + fahrenheit + " F");
System.out.println("" + kelvin + " K");
break;
case "k": System.out.println("Enter the Kelvin temperature: ");
kelvin=scan.nextDouble();
fahrenheit = 1.8*(kelvin - 273.15) + 32;
celcius = kelvin - 273.15;
System.out.println("" + celcius + " C");
System.out.println("" + fahrenheit + " F");
System.out.println("" + kelvin + " K");
break;
default: System.out.println("You enter invalid choice, please choose again");
}
scan.close();
}
}
答案1
得分: 2
这可能会对你有所帮助。<br>
添加一个while
循环并在default
中读取用户输入,它将正常工作<br>。
当用户输入q
时,他/她将退出程序。
class Test {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
double fahrenheit, celcius, kelvin;
// 更改了
System.out.println("选择温度类型:\nf. 华氏度\nc. 摄氏度\nk. 开尔文\nq. 退出");
String word = scan.next();
// 添加了
while (!word.equals("q")) {
switch (word) {
case "f": {
System.out.println("输入华氏度温度:");
fahrenheit = scan.nextDouble();
celcius = (fahrenheit - 32) * 5 / 9;
kelvin = (fahrenheit + 459.67) * 5 / 9;
System.out.println("" + celcius + " C");
System.out.println("" + fahrenheit + " F");
System.out.println("" + kelvin + " K");
break;
}
case "c": {
System.out.println("输入摄氏度温度:");
celcius = scan.nextDouble();
fahrenheit = (celcius * 9) / 5 + 32;
kelvin = celcius + 273.15;
System.out.println("" + celcius + " C");
System.out.println("" + fahrenheit + " F");
System.out.println("" + kelvin + " K");
break;
}
case "k": {
System.out.println("输入开尔文温度:");
kelvin = scan.nextDouble();
fahrenheit = 1.8 * (kelvin - 273.15) + 32;
celcius = kelvin - 273.15;
System.out.println("" + celcius + " C");
System.out.println("" + fahrenheit + " F");
System.out.println("" + kelvin + " K");
break;
}
default: {
System.out.println("您输入了无效的选项,请重新选择");
// 添加了
System.out.println("选择温度类型:\nf. 华氏度\nc. 摄氏度\nk. 开尔文\nq.退出");
word = scan.next();
break;
}
} // 结束 switch
// 检查是否退出
if (!word.equals("q")) {
System.out.println("选择温度类型:\nf. 华氏度\nc. 摄氏度\nk. 开尔文\nq. 退出");
word = scan.next();
}
} // 结束 while
System.out.println("已退出");
}
}
输出:
选择温度类型:
f. 华氏度
c. 摄氏度
k. 开尔文
q. 退出
f
输入华氏度温度:
45
7.222222222222222 C
45.0 F
280.3722222222222 K
选择温度类型:
f. 华氏度
c. 摄氏度
k. 开尔文
q. 退出
c
输入摄氏度温度:
45
45.0 C
113.0 F
318.15 K
选择温度类型:
f. 华氏度
c. 摄氏度
k. 开尔文
q. 退出
k
输入开尔文温度:
45
-228.14999999999998 C
-378.66999999999996 F
45.0 K
选择温度类型:
f. 华氏度
c. 摄氏度
k. 开尔文
q. 退出
cdvd
您输入了无效的选项,请重新选择
选择温度类型:
f. 华氏度
c. 摄氏度
k. 开尔文
q.退出
q
已退出
英文:
This could help you.<br>
add a while
loop and read the input of user in default
and it will work<br>.
When user puts q
he/she will exit the program.
class Test {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
double fahrenheit,celcius,kelvin;
// changed
System.out.println("Choose type of temperature:\nf. Fahrenheit\nc. Celcius\nk. Kelvin\nq. Quit");
String word = scan.next();
//added
while(!word.equals("q")) {
switch(word){
case "f": {
System.out.println("Enter Fahrenheit temperature: ");
fahrenheit=scan.nextDouble();
celcius = (fahrenheit-32) * 5/9;
kelvin = (fahrenheit + 459.67) * 5/9;
System.out.println("" + celcius + " C");
System.out.println("" + fahrenheit + " F");
System.out.println("" + kelvin + " K");
break;
}
case "c":{
System.out.println("Enter the Celcius temperature: ");
celcius=scan.nextDouble();
fahrenheit = (celcius*9)/5 + 32;
kelvin = celcius + 273.15;
System.out.println("" + celcius + " C");
System.out.println("" + fahrenheit + " F");
System.out.println("" + kelvin + " K");
break;
}
case "k": {
System.out.println("Enter the Kelvin temperature: ");
kelvin=scan.nextDouble();
fahrenheit = 1.8*(kelvin - 273.15) + 32;
celcius = kelvin - 273.15;
System.out.println("" + celcius + " C");
System.out.println("" + fahrenheit + " F");
System.out.println("" + kelvin + " K");
break;
}
default: {
System.out.println("You enter invalid choice, please choose again");
// added
System.out.println("Choose type of temperature:\nf. Fahrenheit\nc. Celcius\nk. Kelvin\nq.Quit");
word = scan.next();
break;
}
}// end of swtich
// checking for exit
if(!word.equals("q")) {
System.out.println("Choose type of temperature:\nf. Fahrenheit\nc. Celcius\nk. Kelvin\nq. Quit");
word = scan.next();
}
}// end of while
System.out.println("Exited");
}
}
Output:
Choose type of temperature:
f. Fahrenheit
c. Celcius
k. Kelvin
q. Quit
f
Enter Fahrenheit temperature:
45
7.222222222222222 C
45.0 F
280.3722222222222 K
Choose type of temperature:
f. Fahrenheit
c. Celcius
k. Kelvin
q. Quit
c
Enter the Celcius temperature:
45
45.0 C
113.0 F
318.15 K
Choose type of temperature:
f. Fahrenheit
c. Celcius
k. Kelvin
q. Quit
k
Enter the Kelvin temperature:
45
-228.14999999999998 C
-378.66999999999996 F
45.0 K
Choose type of temperature:
f. Fahrenheit
c. Celcius
k. Kelvin
q. Quit
cdvd
You enter invalid choice, please choose again
Choose type of temperature:
f. Fahrenheit
c. Celcius
k. Kelvin
q.Quit
q
Exited
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论