package morra;
import java. util. Scanner;
public class user {
public static String name;
public static int go;
public static int showFist ( ) {
System. out. println ( "请选择出拳:1.石头 2.剪刀 3.布" ) ;
Scanner input= new Scanner ( System. in) ;
int play= input. nextInt ( ) ;
if ( play== 1 ) {
System. out. println ( "你出拳: 石头" ) ;
}
if ( play== 2 ) {
System. out. println ( "你出拳: 剪刀" ) ;
}
if ( play== 3 ) {
System. out. println ( "你出拳: 布" ) ;
}
go= play;
return play;
}
}
package morra;
public class computer {
public static String npc;
public static int random;
public static int showFist ( ) {
do {
random = ( int ) ( Math. random ( ) * 2 + 1 ) ;
} while ( random!= 1 && random!= 2 && random!= 3 ) ;
if ( random == 1 ) {
System. out. println ( computer. npc+ "出拳: 石头" ) ;
}
if ( random == 2 ) {
System. out. println ( computer. npc+ "出拳: 剪刀" ) ;
}
if ( random == 3 ) {
System. out. println ( computer. npc+ "出拳: 布" ) ;
}
return random;
}
}
Game. java
package morra;
public class Game {
public static int k;
public static int j;
public static void startGame ( )
{
user. showFist ( ) ;
computer. showFist ( ) ;
k= computer. random;
j= user. go;
}
}
package morra;
import java. util. Scanner;
public class enter {
public static void main ( String[ ] args) {
System. out. println ( "*************************" ) ;
System. out. println ( "****欢迎来到猜拳游戏!****" ) ;
System. out. println ( "*************************" ) ;
System. out. println ( "是否开始?y/n" ) ;
Scanner input = new Scanner ( System. in) ;
String p = input. next ( ) ;
while ( "y" . equals ( p) ) {
int q = 0 , s = 0 , t = 0 ;
String x;
System. out. println ( "请输入您的姓名:" ) ;
String l = input. next ( ) ;
user. name = l;
System. out. println ( "请选择对战角色:1.刘备 2.曹操 3.孙权" ) ;
int js= input. nextInt ( ) ;
System. out. println ( "选择成功!" ) ;
if ( js== 1 ) {
computer. npc= "刘备" ;
}
if ( js== 2 ) {
computer. npc= "曹操" ;
}
if ( js== 3 ) {
computer. npc= "孙权" ;
}
do {
Game. startGame ( ) ;
if ( Game. k == Game. j ) {
System. out. println ( "平局" ) ;
q++ ;
}
if ( ( Game. k == 1 && Game. j == 2 ) || ( Game. k == 2 && Game. j == 3 ) || ( Game. k == 3 && Game. j == 1 ) ) {
System. out. println ( "很遗憾,您输了" ) ;
s++ ;
}
if ( ( Game. k == 2 && Game. j == 1 ) || ( Game. k == 3 && Game. j == 2 ) || ( Game. k == 1 && Game. j == 3 ) ) {
System. out. println ( "恭喜您!胜利!" ) ;
t++ ;
}
System. out. println ( "是否继续?y/n" ) ;
x = input. next ( ) ;
} while ( "y" . equals ( x) ) ;
System. out. println ( "一共对局了:" + ( q + s + t) + "盘" ) ;
System. out. println ( "胜利" + t + "盘" ) ;
System. out. println ( "失败" + s + "盘" ) ;
System. out. println ( "平局" + q + "盘" ) ;
System. out. println ( "积分表:" ) ;
System. out. println ( "" + user. name+ " " + t) ;
System. out. println ( "" + computer. npc+ " " + s) ;
System. out. println ( "重开一局?y/n" ) ;
p= input. next ( ) ;
}
System. out. println ( "感谢您的游玩!" ) ;
}
}