2017-01-04 30 views
1
// This portion works 

    import java.util.Scanner; 
     class test { 
    private static Scanner inp; 
    public static void main(String[] args) { 
     inp = new Scanner(System.in); 
     System.out.println("Input Password"); 
     int n = inp.nextInt(); 
     System.out.println(n); 
     if(n!=234) { 
      System.out.println("Denied Acess"); 

     } else { 
      System.out.print("Password Accepted"); 
     } 

程序錯誤出現錯誤和輸出以下我試着去創建一個加密程序,但我在開始下面

java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match   valid=false] 
[need input=false][source closed=false][skipped=false][group separator=\,] 
[decimal separator=\.][positive prefix=][negative prefix=\Q-\E][positive suffix=] 
[negative suffix=][NaN string=\Q?\E][infinity string=\Q?\E] 

/*Pseudocode: 
Text: Input Text to Encrypt 
      "User Input" 
Text: "User input output for test pourposes" 
*/ 
     Scanner enc = new Scanner(System.in); 
     System.out.println("Input Text to encrypt"); 
     System.out.println(enc); 
     } 
    } 
+0

你應該只包裝一次'System.in'。你有一個'inp'只是繼續使用它。 –

+0

順便說一句,這不是一個錯誤。這是該計劃的預期產出。你打印出掃描儀對象,這就是你應該得到的,你能澄清你預期會發生什麼嗎? –

+0

我希望它把用戶輸入的文本,但它不允許輸入 – Heyo13579

回答

1
import java.util.Scanner; 

class Main { 
    private static Scanner inp; 
    public static void main(String[] args) 
    { 
     inp = new Scanner(System.in); 
     System.out.print("Input Password: "); 
     int n = inp.nextInt(); // For int 
     System.out.println(n); 

     if(n != 234) 
     { 
      System.out.println("Denied Access"); 
     } 
     else 
     { 
      System.out.println("Password Accepted"); 
     } 

     /* Scanner has already been initialized, don't create 
      a new one, use existing one, don't try and print the 
      Scanner because it is of type Scanner. */ 


     System.out.print("Input Text to encrypt: "); 
     String m = inp.next(); // For String 
     System.out.println(m); 
    } 
} 

請參閱此解釋上的掃描儀類:http://www.javatpoint.com/Scanner-class

+0

這工作:D現在感謝您如何讓它顯示「密碼接受「和」輸入文本「以在不同線路上顯示,並且不在彼此旁邊 – Heyo13579

+0

請標記爲正確,然後請不要忘記閱讀該鏈接。 – Kenyanke

相關問題