2015-12-21 73 views
-3

我想用掃描儀,使用戶在開關的代碼,我寫了使用一個數字:在java中switch語句中使用掃描儀

public class NewClass1 { 
    public static void main(String[] args) { 
     Scanner abd=new Scanner(System.in); 
     System.out.println("Enter a number"); 
     int k = 5; 
     int k = i.nextInt(); 
     switch (k) { 
      case 0: 
       System.out.println("You chosed 0"); 
       break; 
      case 1: 
       System.out.println("You chosed 1"); 
       break; 
      case 3: 
       System.out.println("You chosed 3"); 
       break; 
      case 9: 
       System.out.println("You chosed 9"); 
       break; 
      default: 
       System.out.println("Please enter something is in the list, Which is: 0,1,3,9."); 
       break; 
     } 
} 

我覺得有錯,還是在我的代碼失蹤,我會欣賞幫幫我。

+0

任何錯誤,你越來越? –

+1

是,int k = i.nextInt(); – Kaine

+0

你得到的錯誤是什麼? – Shriram

回答

2

您的代碼甚至不會編譯..

int k = 5; 
int k=i.nextInt(); // same variable already define in the scope. 

下一個點就是你指i,但你的掃描儀是初始化爲abd。刪除行int k = 5,你應該改變你的代碼爲

int k = abd.nextInt(); 
2

您的掃描儀被命名爲abd,但您致電i.nextInt()。那就是問題所在。

編輯

int k = 5; 
k = abd.nextInt(); 
+0

所以解決方案是int k = abd.nextInt(); ? – Kaine

+0

我編輯了我的答案。覈實。 –

+1

@Abdulaziz是的。 –