Package practicego;import java.util.scanner;/* * 8. Programs that use loops to play scissors with computers ( Set a winning condition--lose 3 or win 3 times to exit) * 0-stone 1-cloth 2-Scissors */public class Cto {public Static void main (String[] args) {scanner sc = new scanner (System.in); Int i = 0;int j = 0;while (True) {System.out.println ("please punch: 0-stone 1-scissors 2-Cloth" ); Int pernum = sc.nextint ();if (pernum>2 | | pernum<0) {system.out.println ("input error"); continue;} int sysnum = ((int) (Math.random () *10))%3; SYSTEM.OUT.PRINTLN ("Computer Punch" +sysnum);if (pernum-sysnum==-1 | | pernum-sysnum==2) {i++; System.out.println ("player wins" +i+ "Times");} Else if (pernum-sysnum==1 | | pernum-sysnum==-2) {j + +; SYSTEM.OUT.PRINTLN ("Computer wins" +j+ "Times");} Else{system.out.println ("Draw. ");} if (i==3) {system.out.println ("player wins"); else if (j==3) {system.out.println ("Computer wins");
Test results:
Please punch: 0-Stone 1-Scissors 2-cloth 0 computer Punch 0 draw. Please punch: 0-Stone 1-Scissors 2-cloth 0 computer Punch 2 computer wins 1 times please punch: 0-Stone 1-Scissors 2-cloth 0 computer Punch 1 player wins 1 times please punch: 0-Stone 1-Scissors 2-cloth 0 computer punch 1 player wins 2 times please punch: 0-Stone 1-Scissors 2-cloth 0 computer Punch 1 player wins 3 times player wins
Punch: 0-Stone 1-Scissors 2-cloth 1 computer Punch 0 Computer wins 1 times please punch: 0-Stone 1-Scissors 2-cloth 1 computer Punch 0 Computer wins 2 times please punch: 0-Stone 1-Scissors 2-cloth 1 computer punch 2 Player wins 1 times please punch: 0-Stone 1-Scissors 2- Cloth 1 Computer Punch 2 Player wins 2 times please punch: 0-Stone 1-Scissors 2-cloth 1 computer Punch 0 Computer wins 3 times computer wins
Java Cycle Practice: stone scissors Cloth