从扫描仪获取输入后程序停止

时间:2018-09-06 17:53:47

标签: java input

我正在研究我在大学编写的一些旧Java程序,我正在尝试让其中一个工作。从本质上讲,这是一个游戏,您必须猜测0到9之间的一个随机数。您有3次机会,如果您三遍都猜错了,那您就输了。问题在于,在用户第一次通过扫描仪提供号码后,程序便停止了。这是代码:

import java.util.Scanner;
import java.util.Random;
import javax.swing.JOptionPane;

public class homework
{
    public static void main (String [] args)
    {
        int secret;
        Random generator = new Random();

    JOptionPane.showMessageDialog(null,"Let us begin\n");

    secret = generator.nextInt(11);
    if (secret > 9)
    {
        secret = secret-10;
    }

    JOptionPane.showInputDialog ("Enter a number between 0 and 9. You have three tries.");

    guessTheNumber(secret);


}

public static void guessTheNumber(int secret) {

    int guess;
    Scanner sc = new Scanner(System.in);

    for (int chance = 0; chance < 4; chance++) {
        guess = sc.nextInt();
        if (guess == secret)
        {
            JOptionPane.showMessageDialog(null, "Correct! You WIN!");
            sc.close();
            System.exit(1);

        }
        if (guess < secret)
        {
            JOptionPane.showMessageDialog(null, "Too low!");
        }

        if (guess > secret)
        {
            JOptionPane.showMessageDialog(null, "Too high!");
        }

    }

    JOptionPane.showMessageDialog(null, "Sorry! You LOSE!");
    sc.close();
    System.exit(1);
}

}

如果有人可以帮助我,我将不胜感激。谢谢。

2 个答案:

答案 0 :(得分:3)

问题就在这 Scanner sc = new Scanner(System.in); 这会将扫描仪指向System.in,换句话说,就是您从中运行扫描仪的控制台,因此它正在等待那里的输入。您想从JOptionPane

读入标准书

因此,您要做的是彻底删除扫描仪,并且功能guessTheNumber应该如下所示:

public static void guessTheNumber(int secret) {
    int guess;
    for (int chance = 0; chance < 4; chance++) {
        guess = Integer.parseInt(JOptionPane.showInputDialog("Enter a number between 0 and 9. You have three tries."));
        if (guess == secret) {
            JOptionPane.showMessageDialog(null, "Correct! You WIN!");
            System.exit(1);
        }
        if (guess < secret) {
            JOptionPane.showMessageDialog(null, "Too low!");
        }
        if (guess > secret) {
            JOptionPane.showMessageDialog(null, "Too high!");
        }
    }
    JOptionPane.showMessageDialog(null, "Sorry! You LOSE!");
    System.exit(1);
}

答案 1 :(得分:0)

public class homework
{
public static void main (String [] args)
{
    int secret;
    Random generator = new Random();

JOptionPane.showMessageDialog(null,"Let us begin\n");

secret = generator.nextInt(11);
if (secret > 9)
{
    secret = secret-10;
}

guessTheNumber(secret);
}
public static void guessTheNumber(int secret) {
Scanner sc = new Scanner(System.in);

for (int chance = 0; chance < 3; chance++) {
    int a =Integer.valueOf(JOptionPane.showInputDialog("Enter a number between 0 and 9. You have three tries."));

    if (a == secret)
    {
        JOptionPane.showMessageDialog(null, "Correct! You WIN!");
        sc.close();
        System.exit(1);

    }
    if (a < secret)
    {
        JOptionPane.showMessageDialog(null, "Too low!");
    }

    if (a > secret)
    {
        JOptionPane.showMessageDialog(null, "Too high!");
    }

}

JOptionPane.showMessageDialog(null, "Sorry! You LOSE!");
sc.close();
System.exit(1);}}

我不太擅长于解释,但是每次输入对话框弹出并插入一个值时,它只是存储我猜的值?它不知道该怎么办