Ending a Game Based on a Boolean not running any more code afterwards In Java -


i'm making text based version of game "are smarter 5th grader. it's not quite same, asked first grade question, , if miss it, 1 more redemption 1st grade question, , if miss 1 game over, if right can move on 2nd grade question(s).

public boolean correct;  public void firstsection() {        scanner scan = new scanner(system.in);      main man = new main();       system.out.print("the first category 1st grade math. here question... \n 5 * 2 + 4 / 2 ?"             + " \n a. 18 \n b. 15 \n c. 12 \n d. 20");      string answer;     answer = scan.nextline();      if(answer.equalsignorecase("c")) {         system.out.print("congrtulations smarter 1st grader!");         correct = true;     } else {         system.out.print("you missed first question, have 1 chance redeem yourself. here 1st"                 + " grade social studies question... \n name of ship pilgrims sailed america named?"                 + " \n a. mayflower \n b. santa maria \n c. pinta \n d. nina");         answer = scan.nextline();         if(answer.equalsignorecase("a")) {             system.out.print("congratulations smarter 1st grader!");             correct = true;         } else {             system.out.print("you not smarter 1st grader!");             correct = false;          }      }  }  

i have method, iscorrect:

public void iscorrect() { if(correct) {     system.out.println("..."); } else {     system.out.println(" game over. loser."); } } 

my main class call methods looks like:

    man.firstsection();     man.iscorrect();      man.secondsection();     man.iscorrect();     .... 

i pretty want game cut off , not run 2nd-5th grade questions if boolean variable false when iscorrect() ran after firstsection() not know how end game. appreciated. in advance!

you have many way it... here simplest example:

public void iscorrect() {     if(correct)     {         system.out.println("...");     }     else     {         system.out.println(" game over. loser.");         system.exit(0); // close program     } } 

or add return statement iscorrect() (or xsection()) , modify function launch quizz:

man.firstsection(); if (!man.iscorrect())     return 0; // close function not program.  man.secondsection(); if (!man.iscorrect())     return 0; // close function not program. .... 

hint: way, have create many functions. better way create 1 function print out question, , check if answer right.

public boolean section(string question[][]) {     // code here } 

after launch function in loop.


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -