I am trying to make a program which can accept certain integer type input from user, but stop the input as soon the user enters "stop". I have tried doing so, but it's not working properly. Have a look.
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");
}
}
}
}
It does stops when I type stop, but I need to enter it two times.
Here's the run tab where I executed my program.
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
change this block
catch(NumberFormatException e1) {
al = in.nextLine();
if(al.equalsIgnoreCase("stop"))
break;
}
to
catch(NumberFormatException e1) {
if(e.equalsIgnoreCase("stop"))
break;
}
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.