java - How do I get the system to exit if the user is wrong? -


if user guesses wrong system prints out wrong. want game end whenever user wrong, can me? tried using system exit exiting when guess correct well. want system print "game over" if user wrong , next cards appear after that.

static scanner console = new scanner(system.in);     public static void main(string[] args) {         int[] cards = {6,4,2,7,5,9,11,1,12};          system.out.println("predict next number typing in 'higher' or 'lower'");          (int = 1; < cards.length; i++)         {             printhead(cards, i);             system.out.println("");;             string predict = console.next();             if (predict.equals("higher"))                 checkhigher(cards, i-1, i);             if (predict.equals("lower"))                 checklower(cards, i-1, i);         }         printall(cards);                 }      public static void printall(int[] cards){         (int =0; i< cards.length; i++)             system.out.print(cards[i]+ " ");     }      public static void printhead(int[] cards, int upto){         (int =0; i< upto; i++)             system.out.print(cards[i]+ " ");     }      public static void checkhigher (int[] cards, int pos1, int pos2){         if (cards[pos2]>cards[pos1])              system.out.println ("correct!");         else               system.out.println("wrong");     }      public static void checklower (int[] cards, int pos1, int pos2){         if (cards[pos2]<cards[pos1])             system.out.println ("correct!");         else              system.out.println("wrong");     } } 

the simpler way exit program

system.exit(0); 

an alternative return boolean value each check , use decide if exit or not.

a third alternative wrap main method code in try catch , through exception when user wrong, exit.

follow 3 alternatives snippet of codes:


exiting directly check

public static void checklower (int[] cards, int pos1, int pos2){     if (cards[pos2]<cards[pos1]) {         system.out.println ("correct!");     } else {         system.out.println("wrong");         system.exit(0);     } } 

use boolean return value

public boolean void checklower (int[] cards, int pos1, int pos2){     if (cards[pos2]<cards[pos1]) {         system.out.println ("correct!");         return true;     } else {         system.out.println("wrong");         return false;     } } 

and in main

if (!checklower(cards, i-1, i)) {     system.exit(0); }    

throw exception

public boolean void checklower (int[] cards, int pos1, int pos2) throws exception {     if (cards[pos2]<cards[pos1]) {         system.out.println ("correct!");     } else {         system.out.println("wrong");         throw new exception("wrong answer");     } } 

and in main

public static void main(string[] args) {     try {         // code here     } catch (exception e) {        system.out.println("there exception in input");     } } 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -