我想让这个人输入例如112进入控制台,按回车键,然后结束" Hello,Mars。"作为答案。
我被困在"错误:找不到符号"。
import java.util.Scanner;
public class Main {
public static void main(String[] args)
{
int n1 = userVar;
switch ( userVar )
{
case 111:
System.out.println("Hello, Earth.");
break;
case 112:
System.out.println("Hello, Mars.");
break;
case 113:
System.out.println("Hello, Jupiter.");
break;
default:
System.out.println("Hello, Space.");
}
Scanner keyboard = new Scanner(System.in);
n1 = keyboard.nextInt();
}
}
现在,我尝试将int更改为
int userVar;
和
Scanner keyboard = new Scanner(System.in);
userVar = keyboard.nextInt();
但最终还是遇到了错误:变量userVar可能尚未初始化"。请注意,我在Ideone上执行此操作。
答案 0 :(得分:4)
首先,给userVar
一个默认值:
int userVar = 0;
然后尝试为其分配用户输入:
Scanner keyboard = new Scanner(System.in);
userVar = keyboard.nextInt();
答案 1 :(得分:0)
public class main(){
public static void main(String []args){
int userVar = 0;
System.out.print("ENTER NO > ");
Scanner keyboard = new Scanner(System.in);
userVar = keyboard.nextInt(); //The given input stoted in userVar.
// Now Apply Switch-case :)
} }