SoFunction
Updated on 2025-03-02

Java implements simple rock-scissors game

This article shares the specific code for Java to implement a simple rock-scissor game for your reference. The specific content is as follows

There are so many rock-scissors online games, but they all use switch to enter numbers, so use if nesting to write a simple rock-scissors game

package Game;
import ;
import ;//Generate random numbers, use switch to generate random stones, scissors or cloth
public class CaiQuan {
 public static void main(String[] args) {
 while(true)
 {
 
 ("Please enter stone, scissors or cloth");
 Scanner sc = new Scanner();
 String quantou = ();
 int month = (int)(3*())+1;
 String com;// Generate strings for computer punches //The computer punches switch(month)
 {
 case 1:
 com = "Stone";
 break;
 case 2:
 com = "Scissors";
 break;
 case 3:
 com = "cloth";
 break;
 }
 if(("Stone"))
 {
 if(month==1)
 {
 ("What you are out of the stone, what you are out of the computer is out of the stone");
 ("draw");
 }
 else if(month==2)
 {
 ("You're out of stones, and the computer's out of scissors");
 ("You won");
 }
 else if(month==3){
 ("What you are out of stones, what you are out of computers is cloth");
 ("You lost");
 }
 }
 else if(("Scissors"))
 {
 if(month==1)
 {
 ("What you came out is scissors, what you came out of the computer is stones");
 ("You lost");
 }
 else if(month==2)
 {
 ("You're out scissors, and the computer's out scissors");
 ("draw");
 }
 else if(month==3){
 ("What you came out is scissors, what you came out of the computer is cloth");
 ("You won");
 }
 }
 else if(("cloth"))
 {
 if(month==1)
 {
 ("What you are out of cloth, what you are out of computers is out of stones");
 ("You won");
 }
 else if(month==2)
 {
 ("What you came out is cloth, what you came out of computer is scissors");
 ("You lost");
 }
 else if(month==3) {
 ("What you are out is cloth, what you are out of cloth");
 ("draw");
 }
 }
 
 }
 
 }
}

I'll add a piece of rock-scissor game code to you:

import ;
import ;
public class GuessingBoxing {

 public static void main(String[] args) {
 while(true) {
 ("----Russiasm game----");
 ("Please punch (1, scissors 2, stone 3. cloth)");
 Scanner in=new Scanner();
 /**
  * people represent the number of people
  * computer means the number of computers
  */
 int people=(); 
 int computer=(int)(()*3+1);
 f(people,computer);
 ();
 ();
 //The input is completed and start to judge the winner or lose }
 }

 private static void f(int people, int computer) {
 String logo1="Scissors"; //Number characterization String logo2="Scissors";
 switch(people) {
 case 1:
 logo1="Scissors";
 break;
 case 2:
 logo1="Stone";
 break;
 case 3:
 logo1="cloth";
 }
 switch(computer) {
 case 1:
 logo2="Scissors";
 break;
 case 2:
 logo2="Stone";
 break;
 case 3:
 logo2="cloth";
 }
 if(people==computer) {
 ("What you're going to do is:"+logo1+"The computer is out"+logo1);
 }else
 if(people==1&&computer==2||people==2&&computer==3||people==3&&computer==1) {
 ("You lost what you gave:"+logo1+"The computer is out"+logo2);
 }else
 ("You win, what you give:"+logo1+"The computer comes out:"+logo2); 
 } 
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.