如何将 if 和 else-if 语句转换为 switch case?
How can i convert my if and else-if statements into a switch case?
我最近开始学习 Java。我的老师给了我们这个家庭作业,它是关于编写下面的 If-Else 语句代码并将其转换为 Switch-Case 语句代码。我正确地编写了 If-Else 语句代码,但无法将我的 if 和 else-if 转换为 switch case 语句。我越早得到答复越好。谢谢。
我试过添加一个开关,然后将每个数字更改为案例 1、案例 2 等
import javax.swing.JOptionPane;
public class ExerciseV {
public static void main(String [] args){
String rating;
String performance;
rating = JOptionPane.showInputDialog("What is your rating for the gymnast from (1-10)?");
if(rating.equalsIgnoreCase("One")||rating.equalsIgnoreCase("one")||rating.equalsIgnoreCase("1")||
rating.equals("Two")||rating.equalsIgnoreCase("two")||rating.equalsIgnoreCase("2")||
rating.equals("Three")||rating.equalsIgnoreCase("three")||rating.equalsIgnoreCase("3")){
System.out.println("You scored the performance a rating of: " + rating);
performance = "BAD";
System.out.println("You typed in the number "+ rating +" so they had a "+ performance +" performance.");
}
else if(rating.equals("Four")||rating.equalsIgnoreCase("four")||rating.equalsIgnoreCase("4")||
rating.equals("Five")||rating.equalsIgnoreCase("five")||rating.equalsIgnoreCase("5")||
rating.equals("Six")||rating.equalsIgnoreCase("six")||rating.equalsIgnoreCase("6")){
System.out.println("You scored the performance a rating of: " + rating);
performance = "AVERAGE";
System.out.println("You typed in the number "+rating+" so they had a "+performance+" performance.");
}
所有这些都是正确的,但我只是不知道在哪里以及如何将 switch case 放入这个程序中。
我认为这段代码可以帮到你
switch(rating.toUpperCase()) {
case "ONE":
case "1":
case "TWO":
case "2":
performance = "BAD";
break;
case "THREE":
case "3":
//...
}
现在,根据您的实际问题调整此类解决方案
我最近开始学习 Java。我的老师给了我们这个家庭作业,它是关于编写下面的 If-Else 语句代码并将其转换为 Switch-Case 语句代码。我正确地编写了 If-Else 语句代码,但无法将我的 if 和 else-if 转换为 switch case 语句。我越早得到答复越好。谢谢。
我试过添加一个开关,然后将每个数字更改为案例 1、案例 2 等
import javax.swing.JOptionPane;
public class ExerciseV {
public static void main(String [] args){
String rating;
String performance;
rating = JOptionPane.showInputDialog("What is your rating for the gymnast from (1-10)?");
if(rating.equalsIgnoreCase("One")||rating.equalsIgnoreCase("one")||rating.equalsIgnoreCase("1")||
rating.equals("Two")||rating.equalsIgnoreCase("two")||rating.equalsIgnoreCase("2")||
rating.equals("Three")||rating.equalsIgnoreCase("three")||rating.equalsIgnoreCase("3")){
System.out.println("You scored the performance a rating of: " + rating);
performance = "BAD";
System.out.println("You typed in the number "+ rating +" so they had a "+ performance +" performance.");
}
else if(rating.equals("Four")||rating.equalsIgnoreCase("four")||rating.equalsIgnoreCase("4")||
rating.equals("Five")||rating.equalsIgnoreCase("five")||rating.equalsIgnoreCase("5")||
rating.equals("Six")||rating.equalsIgnoreCase("six")||rating.equalsIgnoreCase("6")){
System.out.println("You scored the performance a rating of: " + rating);
performance = "AVERAGE";
System.out.println("You typed in the number "+rating+" so they had a "+performance+" performance.");
}
所有这些都是正确的,但我只是不知道在哪里以及如何将 switch case 放入这个程序中。
我认为这段代码可以帮到你
switch(rating.toUpperCase()) {
case "ONE":
case "1":
case "TWO":
case "2":
performance = "BAD";
break;
case "THREE":
case "3":
//...
}
现在,根据您的实际问题调整此类解决方案