This small program is to imitate the real life scissors writes, through the random number realization computer's fist, inside has 3 classes, respectively is person class, computer class and game class. The person class and the computer class record the user name, winning and losing times, and the punch of the user and computer respectively. Game class to compare the punch of the user and the computer, output the corresponding statement. The following is code: (1) Person class package com. Guessfist;import java.util.scanner;public class person { public String Name; public int Score; public void getname (String name) { name=name; } public int getfist () { scanner input =new scanner (system.in); int fist=input.nextint (); if (fist<1&&fist>3) {system.out.println ("warning! input Error"); return 0;} switch (Fist) {case 1:system.out.println ("\ t player" +name+ ", You have Scissors"); return fist;case 2:system.out.printlN ("\ t player" +name+ ", you are out of stone"), return fist;case 3:system.out.println ("\ t player" +name+ ", you Out of cloth");return fist; DEFAULT:SYSTEM.OUT.PRINTLN ("warning! input error, please re-enter"); Getfist (); break;} return 0; }} (2) Computer class package com. Guessfist;import java.util.random;public class computer { public String Name; public int Score; public void getname (String name) { name=name; } public int getfist () { random r=new random (); int fist=r.nextint (2) +1; switch (Fist) {case 1:system.out.println ("\ t computer" +name+ "scissors"); Return fist;case 2:system.out.println ("\ T computer "+name+" out of the Stone ") return fist;case 3:system.out.println (" \ t Computer "+name+" out of the cloth "); return fist;defAult:break;} return 0; }} (3)
This article is from the Java web design blog, so be sure to keep this source http://10760879.blog.51cto.com/10750879/1717203
Man-Machine interactive version Scissors game