2017-10-11 40 views
-2

我需要讓用戶輸入一個從1到10的數字,然後得到這個數字的階乘,我能夠做所有事情,除了驗證用戶名(To在範圍之內& &不是一個字符串)我該如何驗證掃描器的數據(Java)

package test2; 

import java.util.Scanner; 

public class Test2 { 

    public static void main(String[] args) { 

     int userInput = 0 ; 
     Scanner sc = new Scanner(System.in); 
     System.out.println("Please enter a number from 2 to 100"); 

     if (sc.nextInt() < 101) { 
      userInput = sc.nextInt(); 
     } 
     System.out.println(userInput); 

    } 

} 
+0

你不要求*用戶名*,所以我想你談論了*用戶輸入*。基本上,你想要求用戶輸入一些輸入,直到找到有效的輸入爲止。 – Al1

+0

你在說什麼用戶名?從1到10(或根據您的代碼2到100)的數字應該是用戶名嗎? –

+0

可能的重複https://stackoverflow.com/q/46590539/4906586 – Al1

回答

0
int userInput = 0; 
    Scanner sc = new Scanner(System.in); 
    System.out.println("Please enter a number from 2 to 100"); 
    while (!sc.hasNextInt()) { 
     System.out.println("Please enter number"); 
     sc.next(); 
    } 
    while(true) { 
     userInput = sc.nextInt(); 
     if (userInput > 2 && userInput < 100) { 
      break; 
     }else{ 
      System.out.println("Please enter a number between 2 to 100"); 
     } 
    } 
    System.out.println(userInput); 
+1

如果用戶輸入'「yohoho」'怎麼辦? – Al1

+0

這不檢查輸入類型。 – Lokesh

+0

我已經更新了我的答案,現在檢查它 –

-1

你要求用戶給出兩個輸入。最好接受一個輸入並驗證。將您的代碼更改爲此。您還可以識別輸入是整數還是字符串。

public class TEST{ 

     public static void main(String[] args) { 

      int userInput = 0 ; 
      Scanner sc = new Scanner(System.in); 
      System.out.println("Please enter a number from 2 to 100"); 
      int n = sc.nextInt(); 
      if (n <= 100 && n>=2) { 
       try { 
        if(show(n)) { 
         userInput = n; 
        } 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 
      System.out.println(userInput); 

     } 
     public static boolean show(Object obj) throws IOException{ 
      boolean type = false; 
      if(obj instanceof Integer){ 
       type = true; 
      }else if(obj instanceof String){ 
       type = false; 
      } 
      return type; 
     } 
    } 
+0

downvotes被接受,但有正當的理由。 – Lokesh

0

或者乾脆用一個做,而

0

一個方法,你可以嘗試是

public static void main(String[] args) { 

int userInput = 0 ; 
Scanner sc = new Scanner(System.in); 
System.out.println("Please enter a number from 2 to 100"); 

while (!sc.hasNextInt()) 
{ 
System.out.println("wrong input"); 
sc.next(); 
} 

if (sc.nextInt() < 101) { 
    userInput = sc.nextInt(); 
} 
System.out.println(userInput); 

} 
} 
0

按照java文件

nextInt掃描下一個令牌的輸入作爲int。

在你的代碼

if (sc.nextInt() < 101) { 
     userInput = sc.nextInt(); 
    } 

您正在使用sc.nextInt()的兩倍。如果條件掃描一個整數,如果數字小於101,它將輸入,如果,接下來它將掃描另一個數字,其中用戶可以給出大於101的任何數字。因此,您的如果根本沒有效果。

我建議,您掃描在無限循環變量的值,直到它小於101由@Kalaiselvan一個建議或@Lokesh

0

您可以使用hasNextInt()來驗證從正INT輸入。

import java.util.Scanner; 

public class Test2 { 

    public static void main(String[] args) { 
      Scanner sc = new Scanner(System.in); 
      int userInput = 0 ; 
     do { 
       System.out.println("Please enter a number from 2 to 100"); 
       while (!sc.hasNextInt()) { 
        System.out.println("That's not a number!"); 
        sc.next(); 
       } 
       userInput = sc.nextInt(); 
       if(userInput<2 || userInput > 100) { 
        break; 
       } 
       System.out.println(userInput); 
      } while (userInput <= 1 || userInput >= 100); 


    } 

} 

您將得到更好的主意,如果你可以按照下面的例子programs.The經歷顯示java.util.Scanner.hasNext()方法的使用。

import java.util.*; 
import java.util.regex.Pattern; 

public class ScannerDemo { 

    public static void main(String[] args) { 

    String s = "Hello World! 3+3.0=6"; 

    // create a new scanner with the specified String Object 
    Scanner scanner = new Scanner(s); 

    // check if the scanner has a token 
    System.out.println("" + scanner.hasNext()); 

    // print the rest of the string 
    System.out.println("" + scanner.nextLine()); 

    // check if the scanner has a token after printing the line 
    System.out.println("" + scanner.hasNext()); 

    // close the scanner 
    scanner.close(); 
    } 
} 

輸出

真,世界,你好! 3 + 3.0 = 6,假