Source Code Stonegame.java
1 ImportJava.io.BufferedReader;2 Importjava.io.IOException;3 ImportJava.io.InputStreamReader;4 ImportJava.util.Random;5 6 Public classStonegame {7 Private Static intPlayerscores = 0;8 Private Static intComputerscores = 0;9 Private Static Booleanplaying =true;Ten Private StaticString[] Playerskills = {"Stone", "scissors", "cloth"}; One A Public Static voidMain (string[] args) { -BufferedReader BufferedReader =NewBufferedReader (NewInputStreamReader (system.in)); - Try { theSYSTEM.OUT.PRINTLN ("Input: Stone [1]" scissors [2] cloth [3] exit [4] "); - while(playing) { - intPlayerskill =Integer.parseint (Bufferedreader.readline ()); -Random random =NewRandom (); + intComputerskill = Random.nextint (3) + 1; - if(Playerskill = = 4) { +System.out.println ("Bye.")); Aplaying =false; at Break; - } - if(Playerskill > 0 && Playerskill < 4) { -System.out.println ("Player:" + playerskills[playerskill-1]); -SYSTEM.OUT.PRINTLN ("Computer:" + playerskills[computerskill-1]); - Compare (Playerskill, computerskill); inSystem.out.println ("Player score:" + playerscores + "+" PC Score: "+computerscores); -}Else { toSystem.out.println ("Please try again.")); + } - } the}Catch(IOException e) { * e.printstacktrace (); $}Catch(NumberFormatException e) {Panax NotoginsengSystem.out.println ("Please enter the correct number!")); - } the } + A Private Static voidCompareintPlayerskill,intComputerskill) { the if(Playerskill = =Computerskill) { +System.out.println ("Draw.")); - return; $ } $ if(Playerskill = = 1) {//player skill is stone. - if(Computerskill = = 2) { -System.out.println ("player wins.")); theplayerscores++; -}Else {WuyiSYSTEM.OUT.PRINTLN ("Computer victory.")); thecomputerscores++; - } Wu } - if(Playerskill = = 2) {//player skill is scissor. About if(Computerskill = = 3) { $System.out.println ("player wins.")); -playerscores++; -}Else { -SYSTEM.OUT.PRINTLN ("Computer victory.")); Acomputerscores++; + } the } - $ if(Playerskill = = 3) {//player skill is cloth. the if(Computerskill = = 1) { theSystem.out.println ("player wins.")); theplayerscores++; the}Else { -SYSTEM.OUT.PRINTLN ("Computer victory.")); incomputerscores++; the } the } About } the}
Java Stone Scissors Cloth Game