是否可以将switch语句中的用户输入分配给变量?
case 1: //ask the user to input a subject code
System.out.println("Please enter a Subject Code");
String subjectCode = JOptionPane.showInputDialog("Enter subject code", "ITC100");
subject.isValidCode(subjectCode);
输入没有分配给String subjectCode?
我试过了input.next()
,但它没有用。
答案 0 :(得分:0)
在以下代码中,我会弹出 JOptionPane 时指定主题代码。
import javax.swing.JOptionPane;
public class InputSwitch {
private static String subjectCode;
public static void switchInputTest(int choice){
switch(choice){
case 1:
System.out.println("Please enter a Subject Code");
subjectCode = JOptionPane.showInputDialog("Enter subject code", "ITC100");
}
}
public static void main(String[] args) {
switchInputTest(1);
System.out.println(subjectCode);
}
}
答案 1 :(得分:0)
以下是您的问题的解决方案
case 1:
System.out.println("Enter your subject code: ");
Scanner scanner = new Scanner(System.in);
String subjectCode = scanner.nextLine();
System.out.println("subject code " + subjectCode);
subject.isValidCode(subjectCode);
}