Java: Restarting program after achieving certain value - java

I'm making a game which allows the user to guess the random number. In the event the user guesses correctly, and he/she decides to play again, a new number must be generated. I assumed that if I set boolean value "restart" to "true" at both the beginning (while making it false soon thereafter), and then setting it back to true after "restart" became true again, then the program would start over...it did not. Do you see what I'm doing wrong? Thanks.
import java.util.*;
import javax.swing.*;
import java.util.Scanner;
public class RandomGuess2
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
String userNum;
int userInput, numInt, genNum, amount = 0;
int repeatPlay = 0;
int x = 1;
boolean numTruth, restart;
boolean repeatIsYes = false;
restart = true;
userInput = JOptionPane.showConfirmDialog(null, "Would you like to try and guess the magic
number?", "Guessing Game", JOptionPane.YES_NO_OPTION);
genNum = (1 + (int)(Math.random() * 1000));
do
if((numTruth = (userInput == JOptionPane.YES_OPTION)) || (repeatIsYes = (userInput ==
JOptionPane.YES_OPTION)))
restart = false;
{
userNum = JOptionPane.showInputDialog(null,"Enter the number you're thinking between 1 &
1000: ");
numInt = Integer.parseInt(userNum);
if((numInt > 1000) || (numInt < 1)) {
JOptionPane.showMessageDialog(null, "Incorrect entry");
repeatIsYes = true;
}
else if(numInt > genNum) {
repeatIsYes = true;
repeatPlay = JOptionPane.showConfirmDialog(null, "You guessed too high!"
+ "\nWould you like to guess again?" + genNum);
}
else if(numInt < genNum) {
repeatIsYes = true;
repeatPlay = JOptionPane.showConfirmDialog(null, "You guessed too low!"
+ "\nWould you like to guess again?" + genNum);
}
else if(numInt == genNum) {
repeatIsYes = false;
repeatPlay = JOptionPane.showConfirmDialog(null, "How did you do that? You guessed
+ correctly!\nWould you like to guess again?" + genNum);
if(restart = (repeatPlay == JOptionPane.YES_OPTION))
{restart = true;}
}
if(repeatIsYes = (repeatPlay == JOptionPane.YES_OPTION)) {
numTruth = true;
repeatIsYes = true;
}
else {
numTruth = false;
repeatIsYes = false;
JOptionPane.showMessageDialog(null, "Goodbye! \nYou played " + amount + " times.");
break;
}
}
else {
numTruth = false;
repeatIsYes = false;
JOptionPane.showMessageDialog(null, "Goodbye!");
break;
}
while(numTruth = true);
}}

You never really use restart, i.e you only set it. To make your application start again check the value of restart at an appropriate place in your loop .
I don't have the time to thoroughly read your code (which, btw, is hard to read due to formatting and structure), but a simple solution would be to use two loops.
Here's some pseudocode:
while( playAnotherGame) { //this could be your "restart" flag
boolean gameIsRunning = true;
while( gameIsRunning ) {
//ask for user input
//check guesses
if( guessedCorrectly ) {
gameIsRunning = false; //game is finished
//display result
//ask for user input: restart or quit
if( quit ) {
playAnotherGame = false;
}
}
else {
//ask for user input: guess again, new game or quit
//handle user input
}
}
}
Btw, constructs like this are error prone and hard to read:
//you set and check repeatIsYes at the same time
if(repeatIsYes = (repeatPlay == JOptionPane.YES_OPTION)) {
numTruth = true;
repeatIsYes = true; //this is already true, see the if-condition
}

Related

counter is off in program

I have to write a program for my class.
The instructions are
Your program will choose a random 4 digit number as the secret number.
Your program must prompt the user to enter a 4 digit number as their guess.
The program will respond with a message indicating how many of the digits in the user’s guess are the same as the digit in the same position in the secret number.
For example, if the secret number is 3749, and the user’s guess is 9753, then the program would respond with the message You matched 1, because only one of the digits (the 7) in the user’s guess is the same as the digits in the same position in the secret number.
The program will allow the user to continue to enter guesses until they guess the correct secret number.
After the user has entered the secret number, the program will output a count of the total number of guesses the user took to find the secret number.
Then the program will ask the user if they would like to play again. If the user answers “yes”, then the program will choose another random 4 digit number and play continues as described above.
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
Random random = new Random();
System.out.println("----- MASTERMIND -----");
System.out.println("Guess the 4 digit number!");
boolean keepGoing = true;
while (keepGoing = true)
{
String secretNumber = String.format("%04d", random.nextInt(10000));
int guessCount = 0;
int matchCount = 0;
while (matchCount != 4)
{
System.out.println("Enter your guess: ");
String userGuess = input.nextLine();
guessCount++;
if (userGuess.substring(0,1).equals(secretNumber.substring(0,1)));
{
matchCount++;
}
if (userGuess.substring(1,2).equals(secretNumber.substring(1,2)));
{
matchCount++;
}
if (userGuess.substring(2,3).equals(secretNumber.substring(2,3)));
{
matchCount++;
}
if (userGuess.substring(3,4).equals(secretNumber.substring(3,4)));
{
matchCount++;
}
System.out.println("You matched " + matchCount + " digit/digits.");
if (userGuess.equals(secretNumber))
{
System.out.println("Congratulations! You guessed the right number in " + guessCount + " guess/guesses.");
System.out.println("Would you like to play again? Enter Y for yes or N for no.");
String keepGoingYesOrNo = input.nextLine();
if (keepGoingYesOrNo.equals("N"));
{
keepGoing = false;
}
}
}
}
}
For some reason the matchCount is always 4 once the program runs even when the numbers don't match.
I thought the code under the if statement would only be executed if the condition is true but for some reason it runs it anyways.
You have semicolons after your if statements making them useless. The format should be like this:
if (userGuess.substring(0,1).equals(secretNumber.substring(0,1))) {
matchCount++;
}
But also as the program is currently written, matchCount will keep increasing even if they duplicate numbers. So if the number was 1234 and the user guesses 2222 then 2223 matchCount will be two. I recommend setting boolean values for digit1, digit2, digit3, and digit4. When the digit is guessed, set the value to true. Only run each if block if digit[x] = false.
Edit: After some trial and error I made the program as reliable as possible. Before, matchCount would keep increasing even if you repeated the same digits, so to prevent this I implemented some boolean values.
Furthermore, if a digit was correctly guessed but then not guessed in following attempts matchCount would remain the same, so I wrote another set of if statements to decrease matchCount in this event. I also fixed some other minor issues within the code including resetting matchCount when replaying and also successfully ending the program when a user does not want to play. Let me know if you have any questions! Here is the final code.
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
Random random = new Random();
System.out.println("----- MASTERMIND -----");
System.out.println("Guess the 4 digit number!");
boolean keepGoing = true;
boolean digit1 = false;
boolean digit2 = false;
boolean digit3 = false;
boolean digit4 = false;
while (keepGoing = true)
{
String secretNumber = String.format("%04d", random.nextInt(10000));
int guessCount = 0;
int matchCount = 0;
while (matchCount != 4 && keepGoing == true)
{
System.out.println("Enter your guess: ");
System.out.println(secretNumber);
String userGuess = input.nextLine();
guessCount++;
if (userGuess.substring(0,1).equals(secretNumber.substring(0,1)) && digit1 == false)
{ //if the first digit is correct and hasn't been guessed already, matchcount increases
matchCount++;
digit1 = true;
}
if (!userGuess.substring(0,1).equals(secretNumber.substring(0,1)) && digit1 == true)
{ //if the first digit is incorrect but you previously guessed it, matchcount decreases and digit1 is set to false
matchCount--;
digit1 = false;
}
if (userGuess.substring(1,2).equals(secretNumber.substring(1,2)) && digit2 == false)
{
matchCount++;
digit2 = true;
}
if (!userGuess.substring(1,2).equals(secretNumber.substring(1,2)) && digit2 == true)
{
matchCount--;
digit2 = false;
}
if (userGuess.substring(2,3).equals(secretNumber.substring(2,3)) && digit3 == false)
{
matchCount++;
digit3 = true;
}
if (!userGuess.substring(2,3).equals(secretNumber.substring(2,3)) && digit3 == true)
{
matchCount--;
digit3 = false;
}
if (userGuess.substring(3,4).equals(secretNumber.substring(3,4)) && digit4 == false)
{
matchCount++;
digit4 = true;
}
if (!userGuess.substring(3,4).equals(secretNumber.substring(3,4)) && digit4 == true)
{
matchCount--;
digit4 = false;
}
System.out.println("You matched " + matchCount + " digit/digits.");
if (userGuess.equals(secretNumber))
{
System.out.println("Congratulations! You guessed the right number in " + guessCount + " guess/guesses.");
System.out.println("Would you like to play again? Enter Y for yes or N for no.");
String keepGoingYesOrNo = input.nextLine();
if (keepGoingYesOrNo.equals("Y"))
{
keepGoing = true;
matchCount=0;
guessCount=0;
} else {
System.out.println("Thanks for playing!");
System.exit(0);
}
}
}
}
}
First of all, I think you can change the code to this and it is easier to maintain and it is more readable
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
Random random = new Random();
System.out.println("----- MASTERMIND -----");
System.out.println("Guess the 4 digit number!");
boolean keepGoing = true;
while (keepGoing = true)
{
String secretNumber = String.format("%04d",
random.nextInt(10000));
int guessCount = 0;
int matchCount = 0;
while (matchCount != 4)
{
matchCount = 0
System.out.println("Enter your guess: ");
String userGuess = input.nextLine();
guessCount++;
for(int i = 0 ; i < secretNumber.length() ; i++){
if(secreetNumber.charAt[i] == userGuess.charAt[i])
matchCount++;
}
System.out.println("You matched " + matchCount + "
digit/digits.");
if (userGuess.equals(secretNumber))
{
System.out.println("Congratulations! You guessed the
right
number in " + guessCount + " guess/guesses.");
System.out.println("Would you like to play again? Enter
Y for yes or N for no.");
String keepGoingYesOrNo = input.nextLine();
if (keepGoingYesOrNo.equals("N"));
{
keepGoing = false;
}
}
}
}
}
And reset matchCount after each guess
Empty if blocks
The semicolon at the end of the ifs closes them and the block next to them is executed no matter what. Fix:
if (userGuess.substring(0,1).equals(secretNumber.substring(0,1)))
{
matchCount++;
}
if (userGuess.substring(1,2).equals(secretNumber.substring(1,2)))
{
matchCount++;
}
if (userGuess.substring(2,3).equals(secretNumber.substring(2,3)))
{
matchCount++;
}
if (userGuess.substring(3,4).equals(secretNumber.substring(3,4)))
{
matchCount++;
}
matchCount should be 0 at each iteration
If you happen not to guess the full number, but at least one of your digits are correct, then matchCount will be a strictly positive number. Since you do not set it to 0, the matchCount will behave in the wrong way. You need to reset it, like
if (userGuess.equals(secretNumber))
{
System.out.println("Congratulations! You guessed the right number in " + guessCount + " guess/guesses.");
System.out.println("Would you like to play again? Enter Y for yes or N for no.");
String keepGoingYesOrNo = input.nextLine();
if (keepGoingYesOrNo.equals("N"));
{
keepGoing = false;
}
}
else
{
matchCount = 0;
}

Rockpapergame with rounds and counter variables - java

I am creating a rock paper project which has the following requirement:
Continually plays rounds of rock, paper, scissors until one of the players wins three rounds. At that point, the program outputs the winner and the number of rounds it took them to win. If there is no winner after 10 rounds, the competition is declared a tie
Something seems to be missing which I am not quite able to understand or notice. How would I make my game stop after the rounds and declare a winner?
Note: No arrays, external libraries other than scanner, or any built-in methods of java allowed
This is my attempt:
import java.util.Scanner;
public class Rockpaper{
public static void main(String[]args){
Scanner keyboard = new Scanner(System.in);
String player1 = keyboard.next();
String player2 = keyboard.next();
String player = "Player1";
int round = 1;
boolean go = true;
boolean win = false;
while(go){
if (player1.equals("r") && player2.equals("p")){
win = true;
}
else if (player1.equals("p") && player2.equals("r")){
win = true;
}
else if (player1.equals("p") && player2.equals("s")){
win = true;
}
else if (player1.equals("s") && player2.equals("p")){
win = true;
}
else if (player1.equals("s") && player2.equals("r")){
win = true;
}
else if (player1.equals("r") && player2.equals("s")){
win = true;
}
else if (player1.equals("r") && player2.equals("r")){
win = false;
}
else if (player1.equals("p") && player2.equals("p")){
win = false;
}
else if (player1.equals("s") && player2.equals("s")){
win = false;
}
if (round < 5){
System.out.println(win+" after "+round+" rounds!");
go = false;
}else{
System.out.println("Tie - No winner after "+round+" rounds!");
}
if (player.equals("Player1"){
Player = "Player2";
}else{
Player = "Player1";
}
}
}
}
The problem I see is that there needs to be a separate variable that counts each of the win possibilities, for example, "win1" which would count the player1 win possibility and "win2" that would count the player2 wins. I am not quite sure about the rounds variable that would initially start counting the rounds up to 10 which is the maximum.
Sample input/output:
Currently you read the input only once, before the loop:
String player1 = keyboard.next();
String player2 = keyboard.next();
After every match, you should ask if players should continue playing. If so, then you must ask for their input again. This is, just move the "playerX" variable declaration and initialization inside the loop:
//comment/remove these
//String player1 = keyboard.next();
//String player2 = keyboard.next();
//inside the loop
while(go){
String player1 = keyboard.next();
String player2 = keyboard.next();
if (player1.equals("r") && player2.equals("p")){
/* rest of your code */
}
Also, this section:
if (round < 5){
System.out.println(win+" after "+round+" rounds!");
go = false;
}else{
System.out.println("Tie - No winner after "+round+" rounds!");
}
if (player.equals("Player1"){
Player = "Player2";
}else{
Player = "Player1";
}
}
It seems odd for two things:
round is never increased.
The else after round < 5 will be always executed, wrongly stating that there's a tie.
Reassigning Player variable for asking user input is not necessary. Instead, you could use 2 variables to store names of your players that are initialized before the game begins.
One more thing: instance of Scanner is Closeable, so each time you use it to read user input, you make sure that the instance is closed after is not needed anymore, in this case, at the end of the program.
More hints:
Reduce several if/else with the same output to a single if evaluation
You could make use of methods to ease game result.
With all this in mind, your code may look like this:
import java.util.Scanner;
public class RockPaperScizzorGame {
public static int getGameResult(String player1Move, String player2Move) {
int result = 0; //assume the game will be a tie
//player 2 wins
if (player1Move.equals("r") && player2Move.equals("p")
|| player1.equals("p") && player2.equals("s")
|| player1.equals("s") && player2.equals("r")
) {
result = 2;
}
//player 1 wins
if (player1.equals("p") && player2.equals("r")
|| player1.equals("s") && player2.equals("p")
|| player1.equals("r") && player2.equals("s")) {
result = 1;
}
//return the result: 0, 1 or 2
return result;
}
public static void main (String[] args) {
try (Scanner keyboard = new Scanner(System.in)) {
String player1Name = "Player 1";
String player2Name = "Player 2";
int round = 0;
boolean go = true;
int winsPlayer1 = 0;
int winsPlayer2 = 0;
while (go) {
System.out.println("Make your move " + player1Name + ": ");
String player1Move = keyboard.next();
System.out.println("Make your move " + player2Name + ": ");
String player2Move = keyboard.next();
int gameResult = getGameResult(player1Move, player2Move);
switch(gameResult) {
case 1:
winsPlayer1++;
break;
case 2:
winsPlayer2++;
break;
}
round++;
if (winsPlayer1 == 3) {
System.out.println(player1Name + " won after " + round + " rounds!");
go = false;
} else if (winsPlayer2 == 3) {
System.out.println(player2Name + " won after " + round + " rounds!");
go = false;
} else {
if (round == 5 && winsPlayer1 < 3 && winsPlayer2 < 3) {
System.out.println("Tie - No winner after "+round+" rounds!");
go = false;
}
}
} catch (IOException e) {
System.out.println("Issues when trying to accept user input.");
e.printStacktrace();
}
}
}
You can improve the code even more:
Use more methods to ease the code in main method.
Since your main loop depends more on a counter rather than a boolean flag, you may use a for loop rather than a while.
You may ask for user input for the name of the players.
You may create a class to encapsulate data of your players: name, currentMove, number of wins.
Problems with your code:
Not using separate variables for individual players.
Not putting input statements inside the loop as a result of which the input statements run only once.
Not changing the value of the variable, round but using its value in the condition, if (round < 5) which will always evaluate true if the value of round is not increased.
Solution
import java.util.Scanner;
public class Rockpaper {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int round = 1;
boolean go = true;
int player1Score = 0;
int player2Score = 0;
while (go && round <= 10) {
String player1 = keyboard.next();
String player2 = keyboard.next();
if (player1.equals("r") && player2.equals("p")) {
player2Score++;
} else if (player1.equals("p") && player2.equals("r")) {
player1Score++;
} else if (player1.equals("p") && player2.equals("s")) {
player2Score++;
} else if (player1.equals("s") && player2.equals("p")) {
player1Score++;
} else if (player1.equals("s") && player2.equals("r")) {
player2Score++;
} else if (player1.equals("r") && player2.equals("s")) {
player1Score++;
}
if (player1Score >= 3) {
System.out.println("Player1 wins " + " after " + round + " rounds!");
go = false;
}
if (player2Score >= 3) {
System.out.println("Player2 wins " + " after " + round + " rounds!");
go = false;
}
round++;
}
if (round > 10) {
System.out.println("Tie - No winner after " + (round - 1) + " rounds!");
}
}
}
First sample run:
p
r
r
s
s
s
r
r
p
r
Player1 wins after 5 rounds!
Second sample run:
p
p
p
r
r
r
s
s
p
p
s
s
s
s
p
p
r
p
s
p
Tie - No winner after 10 rounds!

I can't figure out why the "play again" potion of my code isn't working

So I've been using java for a few weeks now and wanted to try to make my first little game. Everything I had was working fine until I tried adding a play again feature. It looks like it just skipping a small portion of code at the bottom and I can't figure out why.
I've tried moving different parts of the code around but can't figure out why it's not working.
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("You have 10 tries to correctly guess a random number between 1 and 10. Good luck! \n"
+ "After you have entered a number, press ENTER to see if you won.");
boolean play;
play = true;
do
{
int tries = 10, triesLeft = 0, triesUsed = 0;
String YorN;
do
{
System.out.print("\nEnter A Number: ");
int numInp = scan.nextInt();
int randomNumber = (int)(Math.random()*9) + 1;
System.out.println("The number was " + randomNumber);
if(numInp == randomNumber) {
System.out.println("\nYou Win! Great job.");
triesLeft = --tries;
triesUsed = 10 - triesLeft;
tries = -1;
} else {
System.out.println("Try Again...");
tries --;
}
}
while( tries > 0);
String playAgain = (" Feel free to play again soon!");
if(tries == 0) {
System.out.println("\nOut of tries :( You lose loser " + playAgain);
}
if(tries == -1) {
System.out.println("\nYou used " + triesUsed + " tries and had "
+ triesLeft + " tries left! :)" + playAgain);
}
System.out.print("\nDo you want to play again? Y or N?");
YorN = scan.nextLine();
if (YorN == ("Y") || YorN == ("y"))
{
play = true;
}
else if (YorN == ("N") || YorN == ("n"))
{
play = false;
}
else {}
} while(play = true);
if (play = false){
System.out.println("\nGood Bye");
}
}
This system doesn't seem to be doing anything when I run the code:
YorN = scan.nextLine();
if (YorN == ("Y") || YorN == ("y"))
{
play = true;
}
else if (YorN == ("N") || YorN == ("n"))
{
play = false;
}
else {}
Here's what I see when I run it:
You Win! Great job.
You used 5 tries and had 5 tries left! :) Feel free to play again soon!
Do you want to play again? Y or N?
Enter A Number:
It doesn't give me a spot to type y or n as it should. If anyone knows why I would really appreciate the help
nextInt() or next() method does not read enter. usually after number
you must be pressing enter which is not readied by above method. a
sort cut to avoid this is keep 1 extra sc.nextLine() method call. it
will read and discard enter.
scan.nextLine();
YorN = scan.nextLine();
if (YorN == ("Y") || YorN == ("y"))
{
play = true;
}
else if (YorN == ("N") || YorN == ("n"))
{
play = false;
}
else {}

Java Loop only running once

Im building a code guessing game if you insert an invalid input ie( 333 ) it will prompt you to change your guess. however this only works on guess #1 on guess #2 - #6 it will let any invalid input go through
public void game(){
System.out.println("Enter guess #" + (guessAtt + 1));
guess = keyboard.next();
guess = guess.toLowerCase();
if( guess.equals(quit)){
System.exit(0);
}
if (guess.length() < 2){
System.out.println("Guess Too short try again");
game();
}
if (guess.length() > 3){
System.out.println("Guess too long try again");
game();
}
letter1 = guess.charAt(0);
letter2 = guess.charAt(1);
letter3 = guess.charAt(2);
isValid();
}
public boolean isValid(){
if (letter1.equals('a')|| letter1.equals('b')|| letter1.equals('c')|| letter1.equals('d')|| letter1.equals('e')){
isValid1 = true;
}
if(letter2.equals('a')|| letter2.equals('b')|| letter2.equals('c')|| letter2.equals('d')|| letter2.equals('e')){
isValid2 = true;
}
if(letter3.equals('a')|| letter3.equals('b')|| letter3.equals('c')|| letter3.equals('d')|| letter3.equals('e')){
isValid3 = true;
}
if(isValid1 == true && isValid2 == true && isValid3 == true){
isValid = true;
}
else {
isValid = false;
}
while (isValid == false){
System.out.println("invalid input try again\n");
game();
}
return isValid;
}
you could both use a while loop in the game that breaks when isValid() returns a true. You could also call the function game if isValid() returns a false value. Now you ask for a boolean value, but you don't use it. No matter what it returns, as long as your value contains the right lenght, the game ends.

How to go back into a while-loop, from an if statement?

Here's what i've been working on. I'm trying loop this while method, using booleans. (My teacher is incompetent, so i've been learning out of textbook.)
else { System.out.println("Do you want to restart? Y/N");
string answer = scn.next();
return;
if (scn.hasNext() && !no)) {
System.out.println("end");
} else{
continue;
}
/*if (repeat) {
continue;
} else {
System.out.println("End");
break;
}*/
}
This is nested in a while loop like so ....
import java.util.Scanner; import java.lang.String;
public class booleanvariables {
public static void main (String[] args){
Scanner scn = new Scanner(System.in);
int score1, score2;
String answer, e;
boolean bothHigh, atLeastOneHigh, atLeastOneModerate, noLow, tooLow, repeat;
while (true) {
System.out.print("Enter the first test score:\t");
score1 = scn.nextInt();
System.out.print("Enter the second test score:\t");
score2 = scn.nextInt();
answer = null;
e = "n";
bothHigh = (score1 >= 90 && score2 >= 90);
atLeastOneHigh = (score1 >= 90 || score2 >= 90);
atLeastOneModerate = (score1 >= 70 || score2 >= 70);
noLow = !(score1 < 50 || score2 < 50);
tooLow = (score1 <= 50 || score2 <= 50);
repeat = (answer == "yes" || answer == "y"); //|| answer == Y || answer == Yes);
if (tooLow)
System.out.println("Inputs are too low");
if (bothHigh)
System.out.println("Qualified to be a manager");
if (atLeastOneHigh)
System.out.println("Qualified to be a supervisor");
if (atLeastOneModerate && noLow)
System.out.println("Qualified to be a clerk");
/** NESTED WRONG I'M AWARE
*/
else { System.out.println("Do you want to restart? Y/N");
string answer = scn.next();
return;
if (scn.hasNext() && !no)) {
System.out.println("end");
} else{
continue;
}
/*if (repeat) {
continue;
} else {
System.out.println("End");
break;
}*/
}
}
}
}
This is much simpler than you think.
Just do it like this:
boolean stop = false;
while(!stop) {
//do whatever you want here
System.out.println("Do you want to quit?(yes or no");
String input = scan.nextLine();
if(input.equals("no")) {
stop = true;
}
}
That way, if you enter "no", it'll set the boolean to true, which then will make the condition for the while loop, !stop, equal to false.
answer == "yes"
You are checking if two objects are the same. You should use the equals method answer.equals("yes") || answer.equals("y")
Tested and Working to My Liking
I've reworked some branching. ( I use BlueJ as a compiler and it thinks this is an error without the input = scn.nextLine();
do {
//same booleans i've been using
if (!stop) {
System.out.print("Do you want to quit? (yes or no):\t");
//String input;
input = scn.nextLine();
}
//String input;
input = scn.next();
if(input.equals("yes")) {
stop = true;
System.out.println("Goodbye");
return;
}
} while (!stop);
I really don't know why blue J doesn't like it when initialize input from within the if statement

Categories