如何允许用户在同一个变量

时间:2017-05-08 22:05:47

标签: java string try-catch

我正在尝试创建一个程序,它可以接受来自用户的某些整数类型输入,但是一旦用户输入"停止"就停止输入。 我试过这样做,但它没有正常工作。看看。

import java.util.*;
import java.util.concurrent.TimeUnit;
import java.io.*;

class Iron {
    public static void main(String args[])  throws InterruptedException {
        Scanner in = new Scanner(System.in);

        System.out.println("Do you wanna play?, type yes to start.");
        String a = in.nextLine();
        String b; String c="goo";
        String d,e="helloWorld";
        String al="helloworld";

        int f=0,g;

        if (a.equalsIgnoreCase("yes")) {
            System.out.println("Thanks for staring the game ");
            TimeUnit.SECONDS.sleep(1);
            System.out.println("-------Welcome to Need For Fun-------");
            System.out.println();
            TimeUnit.SECONDS.sleep(1);

            while (!c.equalsIgnoreCase("yes")) {
                System.out.println();
                System.out.println("Enter your name_");
                b = in.nextLine();
                System.out.println("Is your name " + b + "?");
                System.out.println("If yes type yes or if not type something else");
                c = in.nextLine();
            }
            TimeUnit.SECONDS.sleep(0);
            System.out.println("So, the game is quite similar to hand cricket");
            System.out.println("You will win, if your number and Computer's number are equal");
            System.out.println("To stop the game any time type start");
            System.out.println();
            TimeUnit.SECONDS.sleep(1);

            System.out.println("So ready to play the game?, type yes to confirm.");
            d = in.nextLine();

            if(d.equalsIgnoreCase("yes"));
            {
                while(!al.equalsIgnoreCase("stop")) {
                    System.out.println();

                    try {
                        System.out.println("Type your number");
                        e = in.nextLine();
                        f = Integer.parseInt(e);
                    }

                    catch(NumberFormatException e1) {
                        al = in.nextLine();
                        if(al.equalsIgnoreCase("stop"))
                            break;
                    }

                    if (f < 10) {
                        g = (int) (Math.random() * 10);

                        if (f == g)
                            System.out.println("Congrats, you won. ");
                        else
                            System.out.println("Oops!, try again");
                    } else if (f>10)
                        System.out.println("Please enter a number between 1-10");

                }
                System.out.println("Thanks for playing, better luck next Time");
            }
        }
    }
}

当我输入停止时会停止,但我需要输入两次。

这是我执行程序的运行标签。

Do you wanna play?, type yes to start.
yes
Thanks for staring the game 
-------Welcome to Need For Fun-------

Enter your name_
gourav
Is your name gourav?
If yes type yes or if not type something else
yes
So, the game is quite similar to had cricket
You will win, if your number and Computer's number are equal
To stop the game any time type start

So ready to play the game?, type yes to confirm.
yes

Type your number
4
Oops!, try again

Type your number
54
Please enter a number between 1-10

Type your number
4
Oops!, try again

Type your number
stop
3
Oops!, try again

Type your number
stop
stop

Process finished with exit code 0

1 个答案:

答案 0 :(得分:0)

更改此块

           catch(NumberFormatException e1) {
               al = in.nextLine();
               if(al.equalsIgnoreCase("stop"))
                   break;
           }

               catch(NumberFormatException e1) {
                   if(e.equalsIgnoreCase("stop"))
                       break;
               }