Here is a little program I wrote to help me memorize my student ID #. I will * out the actual number but here is my code.
import java.util.Scanner; class baylorID { public static void main(String[] args){ //clear the console System.out.print("\033[H\033[2J"); System.out.flush(); //store correct baylor id in a string String correctID = "*********"; String inputID = new String(); String quit = "quit"; int numCorrect = 0; int numIncorrect = 0; int netCorrect = 0; //ask user for student id while (netCorrect < 10) { System.out.print("\033[H\033[2J"); System.out.flush(); Scanner scan = new Scanner(System.in); System.out.println("What is your Student ID? \t Type 'quit' to exit."); inputID = scan.next(); //compare the two if (inputID.equals(quit)) { numCorrect = 12; numIncorrect = 1; } if (inputID.equals(correctID)) { System.out.println("Correct! You remembered it!"); numCorrect += 1; System.out.println(numCorrect); } else { System.out.println("Incorrect. Try Again."); numIncorrect += 1; System.out.println(numIncorrect); } netCorrect = numCorrect - numIncorrect; } } }
It compiles and runs successfully. However, when I type quit, it does not do what I want.
What I Want:
The program to stop running
What Happens:
Then it stops runningWhat is your Student ID? Type 'quit' to exit. quit Incorrect. Try Again. 2