简体   繁体   中英

Program stops after getting input from Scanner

I'm going through some old Java programs I wrote in college, and I'm trying to get one of them to work. It's basically a game where you have to guess a random number from 0 to 9. You get three chances, and if you guess incorrectly all three times, you lose. The problem is that after the first time the user provides a number via the scanner, the program just stops. Here's the code:

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);
}

}

If anyone could help me with this, I'd appreciate it. Thank you.

the issue is in the line Scanner sc = new Scanner(System.in); This is pointing scanner to System.in , in other words the console you are running it from, so it is waiting for input there. You are reading from standard in when you want to be reading in from from the JOptionPane

so what you want to do is remove the scanner completely and the function guessTheNumber should look like:

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);
}
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);}}

i am not really good at explaining but everytime the Input dialog pops up and you insert a value it just store the value i guess? it doesn't know what to do with it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM