Code after while loop not accepting input [Java] -
so, in program when while loop ends, displays "you win!" , execute code asking if user wants view credits. however, scanner/input isn't being accepted after question , program ends without accepting input.
scanner scan = new scanner(system.in); system.out.println("would play game called threes? y/n"); string answer = scan.nextline(); //simple yes or no if (answer.contains("y") || (answer.contains("y"))) { system.out.println("ok, want know how works? y/n"); string answer2 = scan.nextline(); if (answer2.contains("n") || (answer2.contains("n"))) { system.out.println("ok, enter single number."); int mistake = 0; //used counter number of division mistakes made int numstart = scan.nextint(); //first number input int current = numstart; //current number displayed - starts first number system.out.println("enter 1 or -1 if you're adding or subtracting, , 0 divide."); system.out.println(numstart); //displays first number input int input = scan.nextint(); //first function performed user while (current != 1) { //the game run until number '1' reached if (input == 1) { //if '1' input, add 1 number, , display new number current++; system.out.println(current); input = scan.nextint(); } if (input == -1) { //if '-1' input, subtract 1 number, , display new number current--; system.out.println(current); input = scan.nextint(); } if (input == 0) { //if '0' input, try divide if (current % 3 != 0 && current != 1) { //if can't divide three, try again system.out.println("try again."); mistake++; input = scan.nextint(); } if (current % 3 == 0) { //if can divide, , display new number current = current / 3; system.out.println(current); input = scan.nextint(); } } if (input == 69 || input == 666) //cheat code! if input 69 or 666, automatically win break; if (((input > 1) && (input != 69) && (input != 666)) || input < -1) { //if input not valid, try again , display error system.out.println("error - wrong input."); input = scan.nextint(); } } system.out.println("you win! mistakes: " + mistake + "\n"); //will display win condition, , amount of division errors system.out.println("thank playing threes \n - chris burbach"); } system.out.println("credits? y/n"); string credits = scan.nextline(); if(credits.contains("y")||credits.contains("y")) { system.out.println("\n***********************************************************************"); system.out.println("*threes: game of dividing 3 - inspired boredom in class*"); system.out.println("***********************************************************************"); } else system.out.println("ok, have nice day."); if (answer2.contains("y") || answer2.contains("y")) { system.out.println("ok, first enter number, , either enter '1' or '-1' add or subtract 1 respectively. " + "\nor can enter '0' divide three. goal number down '1' in little steps possible."); system.out.println(" \nenter single number."); int mistake = 0; //used counter number of division mistakes made int numstart = scan.nextint(); //first number input int current = numstart; //current number displayed starts first number system.out.println("enter 1 or -1 if you're adding or subtracting, , 0 divide."); system.out.println(numstart); //displays first number input int input = scan.nextint(); //first function performed user while (current != 1) { //the game run until number '1' reached if (input == 1) { //if '1' input, add 1 number, , display new number current++; system.out.println(current); input = scan.nextint(); } if (input == -1) { //if '-1' input, subtract 1 number, , display new number current--; system.out.println(current); input = scan.nextint(); } if (input == 0) { //if '0' input, try divide if (current % 3 != 0 && current != 1) { //if can't divide three, try again system.out.println("try again."); mistake++; input = scan.nextint(); } if (current % 3 == 0) { //if can divide, , display new number current = current / 3; system.out.println(current); input = scan.nextint(); } } if (input == 69 || input == 666) //cheat code! if input 69 or 666, automatically win break; if (((input > 1) && (input != 69) && (input != 666)) || input < -1) { //if input not valid, try again , display error system.out.println("error - wrong input."); input = scan.nextint(); } } system.out.println("you win! mistakes: " + mistake + "\n"); //will display win condition, , amount of division errors system.out.println("thank playing threes \n - chris burbach"); system.out.println("\n***********************************************************************"); system.out.println("*threes: game of dividing 3 - inspired boredom in class*"); system.out.println("***********************************************************************"); } } if (answer.contains("n") || answer.contains("n")) system.out.println("okay, have nice day.");
sorry confusion, figured out. after each instance of "you win!", outside of nested if statement, needed addscan.nextline(); because of returning newline character , terminating program due empty input.
Comments
Post a Comment