Java Stone Scissors Cloth Game

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.