小程序__剪头石头布,小程序石头布,用JAVA语言编写一个程
分享于 点击 14453 次 点评:18
小程序__剪头石头布,小程序石头布,用JAVA语言编写一个程
用JAVA语言编写一个程序来实现这个游戏。程序中让您选择1,2,3出拳(其中1代表石头,2代表剪子,3代表布),程序则使用一个1-3之间的随机数出拳(电脑出拳),判断胜负并显示结果(电脑赢,您赢,打平)。
实现步骤:
1) 定义程序中使用的变量
2) 接受用户出拳,使用函数完成用户出拳的功能
3) 完成电脑出拳,使用函数完成电脑出拳的功能
4) 判断胜负,使用函数完成判断胜负的功能
import java.util.Scanner;public class XunHuan2 { public static void main(String[] args) { for(;;){setShouShi();} } public static void setShouShi(){ Scanner sc=new Scanner(System.in); //1代表石头,2代表剪子,3代表布 System.out.print("玩家请出手势(1代表石头,2代表剪子,3代表布):"); int x1=sc.nextInt(); int x2=(int)Math.round((Math.random()*3)); int x3=x2%3+1; System.out.println("电脑出的手势是(1代表石头,2代表剪子,3代表布):"+x3); if(x1>=1&x1<=3){ switch(x1-x3){ case 0:System.out.println("这是平局"+'\\n');break; case 2: case -1:System.out.println("恭喜你,你打败了电脑"+'\\n');break; case 1: case -2:System.out.println("很遗憾,你输给了电脑"+'\\n');break; default:System.out.println("你的输入有误"+'\\n'); } } else {System.out.println("你的输入有误,请再次输入!"); System.out.println('\\n'); } }}//该片段来自于http://byrx.net
用户点评