2017-02-18 118 views
-3

這是代碼 -錯誤switch語句中的Java蝕

 import java.util.Scanner; 
    public class HelloWorld{ 

    public static void main(String []args){ 
    Scanner kb=new Scanner(System.in); 
    System.out.println("enter a four digit number"); 
    int number=kb.nextInt(); 
    int digit2=number; 

    switch (digit1) 
    { 
     case 1: System.out.println("One");break; 
     case 2: System.out.println("Two");break; 
     case 3: System.out.println("Three");break; 
     case 4: System.out.println("Four");break; 
     case 5: System.out.println("Five");break; 
     case 6: System.out.println("Six");break; 
     case 7: System.out.println("Seven");break; 
     case 8: System.out.println("Eight");break; 
     case 9: System.out.println("Nine");break; 
     case 0: System.out.println("Zero");break; 
     default: System.out.println("");break; 
    } 
    switch (digit2) 
     { 
      case 1: System.out.println("One");break; 
     case 2: System.out.println("Two");break; 
     case 3: System.out.println("Three");break; 
     case 4: System.out.println("Four");break; 
     case 5: System.out.println("Five");break; 
     case 6: System.out.println("Six");break; 
     case 7: System.out.println("Seven");break; 
     case 8: System.out.println("Eight");break; 
     case 9: System.out.println("Nine");break; 
     case 0: System.out.println("Zero");break; 
     default: System.out.println("");break; 
     } 
    } 
} 

而這個錯誤是未來

2 errors                                      
sh-4.3$ javac HelloWorld.java                                 
HelloWorld.java:10: error: cannot find symbol                             
    switch (digit1)                                   
      ^                                    
    symbol: variable digit1                                 
    location: class HelloWorld                                 
HelloWorld.java:10: error: illegal start of type                            
    switch (digit1)                                   
     ^                                    
2 errors  

請幫我

+0

其中digit1聲明? – RSon1234

+0

程序中沒有數字1變量:)。 – Maverick

回答

0

您還沒有定義的命名digit1變量。

下面是一些粗糙的代碼做你想要什麼:

import java.util.Scanner; 

public class Test { 

    public static void main(String[] args) { 
     Scanner kb = new Scanner(System.in); 
     System.out.println("Enter a four digit number"); 

     String numStr = kb.next(); 

     int number = 0; 
     try { 
     number = Integer.parseInt(numStr); 
     } catch (Exception e) { 
     System.out.println("Input was not a number!"); 
     System.exit(1); 
     } 

     if (number < 1000) { 
     System.out.println("Number must be at least four digits!"); 
     System.exit(1); 
     } 

     for (int digitNdx = 0; digitNdx < 4; digitNdx++) { 
     switch (numStr.charAt(digitNdx)) { 
      case '1': 
       System.out.println("One"); 
       break; 
      case '2': 
       System.out.println("Two"); 
       break; 
      case '3': 
       System.out.println("Three"); 
       break; 
      case '4': 
       System.out.println("Four"); 
       break; 
      case '5': 
       System.out.println("Five"); 
       break; 
      case '6': 
       System.out.println("Six"); 
       break; 
      case '7': 
       System.out.println("Seven"); 
       break; 
      case '8': 
       System.out.println("Eight"); 
       break; 
      case '9': 
       System.out.println("Nine"); 
       break; 
      case '0': 
       System.out.println("Zero"); 
       break; 
      default: 
       System.out.println(""); 
       break; 
     } 
     } 
    } 
} 
+1

Stack Overflow是一個問答網站,而不是作業寫作服務。請不要讓用戶有理由相信。謝謝。 –

+1

@JoeC好的,我下次只會發佈與他們的問題有關的信息。感謝您的指導。 –

0

您的代碼

Scanner kb = new Scanner(System.in); 
System.out.println("enter a four digit number"); 
int number = kb.nextInt(); 
int digit2 = number; 

switch (digit1) { 

錯誤的發生是由於

這裏是沒有分配任何變量爲digit1


int digit1 = 5; 
int digit2 = number;