Java - 在写入字符串而不是int

时间:2015-07-24 17:54:22

标签: java loops

场景:我已经制作了一些命令行游戏,而且每个游戏都必须输入int。我做了一个if语句来检查用户是否输入了int或其他内容。如果他们确实进入int游戏就会进入。如果用户输入了其他内容,例如string,那么游戏就会打印错误并且应该继续,但这就是问题发生的地方。

问题:用户输入字符串后,程序才开始循环,我不知道如何阻止它。

import java.util.Scanner;

public class SimpleShips {

static Scanner userInput = new Scanner(System.in);
static int hitsNum;
static int triesNum;
static int cell1;
static int cell2;
static int cell3;

static void setUp() {

    hitsNum = 0;
    //Cells 1-7
    cell1 = (int) (Math.random() * 5);
    cell2 = cell1 + 1;
    cell3 = cell1 + 2;

}

static void checkNum() {

    int tempNum;

    System.out.print("Choose a number from 1 to 7: ");

    if(userInput.hasNextInt()) {

         tempNum = userInput.nextInt();

         if(tempNum < 8 && tempNum > 0) {
             triesNum++;

             if(tempNum == cell1) {
                 cell1 = 0;
                 hitsNum++;
                 System.out.println("Hit!");
             } else if(tempNum == cell2) {
                 cell2 = 0;
                 hitsNum++;
                 System.out.println("Hit!");
             } else if(tempNum == cell3) {
                 cell3 = 0;
                 hitsNum++;
                 System.out.println("Hit!");
             } else {
                 System.out.println("Miss!");
             }   
         } else {
             System.out.println("Enter a number from 1 to 7!");
         }

    } else {

        System.out.println("You need to enter a number!");

    }
}

public static void main(String[] args) {

    System.out.println("Welcome to the one ship sinking game / BATTLESHIPS!");

    setUp();

    while(hitsNum < 3) {
        checkNum();
    }

    System.out.println("You successfully sank the ship! You sank it in " + triesNum + " tries.");
    System.out.print("Do you want to play again? ");

}

}

2 个答案:

答案 0 :(得分:2)

将处理错误输入大小写的else更改为:

else {
    System.out.println("You need to enter a number!");
    userInput.next();
}

当用户输入String时,您不会使用它。这就是你进入无限循环的原因。

因此,当添加userInput.next();时,将消耗当前字符串,您将进行下一次输入。

答案 1 :(得分:0)

import java.util.Scanner;

public class SimpleShips {

static Scanner userInput = new Scanner(System.in);
static int hitsNum;
static int triesNum;
static int cell1;
static int cell2;
static int cell3;

static void setUp() {

    hitsNum = 0;
    //Cells 1-7
    cell1 = (int) (Math.random() * 5);
    cell2 = cell1 + 1;
    cell3 = cell1 + 2;

}

static void checkNum() {

    int tempNum;

    System.out.print("Choose a number from 1 to 7: ");

    if(userInput.hasNextInt()) {

         tempNum = userInput.nextInt();

         if(tempNum < 8 && tempNum > 0) {
             triesNum++;

             if(tempNum == cell1) {
                 cell1 = 0;
                 hitsNum++;
                 System.out.println("Hit!");
             } else if(tempNum == cell2) {
                 cell2 = 0;
                 hitsNum++;
                 System.out.println("Hit!");
             } else if(tempNum == cell3) {
                 cell3 = 0;
                 hitsNum++;
                 System.out.println("Hit!");
             } else {
                 System.out.println("Miss!");
             }   
         } else {
             System.out.println("Enter a number from 1 to 7!");
         }

    } else {

        System.out.println("You need to enter a number!");
        userInput.next();
    }
}

public static void main(String[] args) {

    System.out.println("Welcome to the one ship sinking game / BATTLESHIPS!");

    setUp();

    while(hitsNum < 3) {

        checkNum();
    }

    System.out.println("You successfully sank the ship! You sank it in " + triesNum + " tries.");
    System.out.print("Do you want to play again? ");

}

}