Java-默认选项在开关中不起作用
你好,我写了一个程序,用开关把一个温度从一个转换成另一个 默认值不起作用: 当我输入一个有效选项时,它可以正常工作,但当我选择无效选项时,它将进入默认输出行:如果输入无效选项,请再次选择并停止工作,而不允许我选择其他选项 在我之前测试它时,默认设置允许我选择另一个选项Java-默认选项在开关中不起作用,java,switch-statement,case,java.util.scanner,default,Java,Switch Statement,Case,Java.util.scanner,Default,你好,我写了一个程序,用开关把一个温度从一个转换成另一个 默认值不起作用: 当我输入一个有效选项时,它可以正常工作,但当我选择无效选项时,它将进入默认输出行:如果输入无效选项,请再次选择并停止工作,而不允许我选择其他选项 在我之前测试它时,默认设置允许我选择另一个选项 import java.util.Scanner; public class Tempature { public static void main(String[] args){ Scanner sca
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();
}
}
这可以帮助您。添加一个
while
循环,并在default
中读取用户的输入,它将工作。 当用户输入
q
时,他/她将退出程序
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");
}
}
输出:
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
你所说的“停止工作”是什么意思?在处理了默认情况后,你希望它做什么?输入有效选项后会发生什么情况,它是否继续工作?当我输入有效选项时,它继续工作正常,早期写测试它默认选项允许我再次选择另一个选项,这次不允许我选择仅停止程序。代码中没有允许用户输入多个选项的内容,当您选择c/f/k时,允许输入一个值,然后程序结束。正如所写,您的编程工作正常。要允许用户选择第二个…n选项,需要有某种循环。也许可以在接受用户输入的Java命令行应用程序上进行google搜索,这正是您为它编写的。当键入无效字符时,它将打印您提到的语句并退出。如果希望它在输入有效字符之前一直提示您,您可以尝试使用do while循环。作为补充说明,您可能希望将重复/复制的代码移动到方法中。用户输入要转换的值后会发生什么?他们可以输入第二个选项吗?或者,如果输入了无效选项,则只能输入第二个选项。也许更好的解决方案是将
word=scan.nextLine()
移动到交换机外部,在循环结束之前?或者如果您只是想在成功操作后停止:word!=“f”和“word!”“c”和单词!=“k”
。或者最好在循环之外添加一个变量,跟踪循环是否应该继续。有人回答了我关于成功操作的问题,所以我编辑了,ty verymuch guys非常有用:)让编辑看一看。现在一切都好了。