簡體   English   中英

關於掃描儀(BlueJ)的問題

[英]Questions about scanner (BlueJ)

我們對此任務有一些指導原則:1.使用掃描儀(Scanner掃描儀= new Scanner(System.in);)2.使用方法scans.nextLine()

我們必須一步一步地構建一個游戲(Mastermind),並且在切換情況下使用nextInt()(輸入不是int的東西)時,我總是遇到錯誤(BlueJ)btw:我們不應該使用nextInt-我們應該使用nextLine-但是我該如何使用開關櫃?

import java.util.Scanner;
public class Game {
/**
 * Methods
 */
public void play() {
    System.out.println("*******************  Game  **********************");
    System.out.println("* (1) CPU vs Human                              *");
    System.out.println("* (2) CPU vs CPU                                *");
    System.out.println("* (3) Human vs CPU                              *");
    System.out.println("* (4) Highscore                                 *");
    System.out.println("* (5) End                                       *");
    System.out.println("-------------------------------------------------");
    System.out.println("Your choice:                                    ");

    Scanner scanner = new Scanner(System.in);

    // i used this so far but i get an error for entering a-z or other stuff than numbers
    int userInput = scanner.nextInt();  

    //i have to use this but it doesnt work with switchcase - any suggestions?
    //String userInput = scanner.nextLine(); 

    scanner.close();
    switch(userInput) {
        case 1: // not written yet
        case 2: // not written yet
        case 3: // not written yet
        case 4: // not written yet
        case 5: System.exit(0);
        default: System.out.println("Illegal userinput! Only enter numbers between 1 and 5!");
    }
  }
}

您需要做的是使用Integer.parseInt(String s)方法將字符串(由.nextLine()方法產生)解析為一個整數。 如果給定的字符串不是整數,則此方法將引發NumberFormatException ,您可以捕獲並使用該方法來知道用戶何時輸入了無效數字,以便您可以再次詢問他/她。

像這樣:

public static void main(String[] args)
{
    boolean validInput = false;
    Scanner scanner = new Scanner(System.in);
    while (!validInput)
    {
        System.out.println("*******************  Game  **********************");
        System.out.println("* (1) CPU vs Human                              *");
        System.out.println("* (2) CPU vs CPU                                *");
        System.out.println("* (3) Human vs CPU                              *");
        System.out.println("* (4) Highscore                                 *");
        System.out.println("* (5) End                                       *");
        System.out.println("-------------------------------------------------");
        System.out.println("Your choice:                                    ");

        try
        {

            // i used this so far but i get an error for entering a-z or other stuff than numbers
            int userInput = Integer.parseInt(scanner.nextLine().trim());
            //If no error took place, then the input is valid.
            validInput = true;

            //i have to use this but it doesnt work with switchcase - any suggestions?
            //String userInput = scanner.nextLine();                 
            switch (userInput)
            {
                case 1: // not written yet
                case 2: // not written yet
                case 3: // not written yet
                case 4: // not written yet
                case 5:
                    System.exit(0);
                default:
                    validInput = false;
                    System.out.println("Illegal userinput! Only enter numbers between 1 and 5!");
            }
        } catch (Exception e)
        {
            System.out.println("Invalid Input, please try again");
        }
    }
    scanner.close();
}

您不能直接將nextLine()的輸出用於切換用例,因為nextLine()返回的是String而不是char。 但是,如果您只讀取一個字符,則可以執行以下操作:

String userInput = scanner.nextLine();
switch(userInput.charAt(0)) {

如果下一個標記不可解析為int,則scanner.nextInt()顯然將引發Exception。 您應該將這些語句包裝在try-catch塊中,如果輸入了非數字,則應做一些適當的事情。

nextLine()給您一個String ,您應該將其轉換為整數(如您nextLine() )並執行后續邏輯。

看看Integer.parseInt()方法

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM