共计 1942 个字符,预计需要花费 5 分钟才能阅读完成。
2.1 要求
与电脑进行猜拳并记录分数。
2.2 Computer.java 源代码(电脑自动随机出拳)
public class Computer {public void Fingers(int index) {String[] fingers = {"石头", "剪刀", "布"}; | |
System.out.println("电脑出拳:" + fingers[index]); | |
} | |
} | |
2.3 Game.java 源代码(实现次要性能的类)
import java.util.Random; | |
import java.util.Scanner; | |
public class Game {public void initial(){ | |
// 页面 | |
Scanner scanner = new Scanner(System.in); | |
System.out.println("------------------- 欢送进入游戏界面 -------------------"); | |
System.out.println(); | |
System.out.println("***********************"); | |
System.out.println("******* 猜拳,开始!******"); | |
System.out.println("***********************"); | |
System.out.println("出拳规定:0. 石头 1. 剪刀 2. 布"); | |
// 输出姓名 | |
System.out.print("请输出你的姓名:"); | |
String name = scanner.next(); | |
System.out.println(name+"VS 汶老板 对战"); | |
// 判断是否开始 | |
System.out.print("要开始吗?(输出 yes/no):"); | |
String b =scanner.next(); | |
int count = 0; | |
int usercount=0; | |
int computercount=0; | |
while (b.equals("yes")){ | |
// 统计对战次数 | |
count++; | |
// 玩家出拳 | |
System.out.print("请出拳 0. 石头 1. 剪刀 2. 布(输出绝对应的数字):"); | |
int a = scanner.nextInt(); | |
String [] fingers = {"石头","剪刀","布"}; | |
System.out.println("你出拳:"+fingers[a]); | |
// 电脑随机出拳 | |
Random random = new Random(); | |
int index = random.nextInt(3); | |
Computer computer = new Computer(); | |
computer.Fingers(index); | |
// 判断输赢 | |
if ((a == 0 && index == 1)||(a == 1 && index == 2)||(a == 2 && index == 0)) {System.out.println("后果说:祝贺你,你博得了!"); | |
usercount = usercount+1; | |
}else if ((a == 0 && index == 2)||(a == 1 && index == 0)||(a == 2 && index == 1)){System.out.println("后果说:很道歉,你输了!"); | |
computercount = computercount+1; | |
}else{System.out.println("后果说:平局!"); | |
} | |
// 是否进行下一局 | |
System.out.print("是否持续进行下一局!(输出 yes/no)"); | |
b = scanner.next();} | |
// 总结 | |
System.out.println("---------------------- 总结 ---------------------"); | |
System.out.println(name+"VS 汶老板"); | |
System.out.println("对战次数:"+count); | |
System.out.println("姓名 \t\t 得分"); | |
System.out.println("汶老板 \t"+computercount); | |
System.out.println(name+"\t"+usercount); | |
String i = computercount>usercount?"很遗憾,你输了本场较量!":computercount<usercount?"祝贺你,你博得了本场较量!":"平局!"; | |
System.out.println(i); | |
} | |
} | |
2.3 Client.java 源代码(测试类)
public class Client {public static void main(String[] args) {Game game = new Game(); | |
game.initial();} | |
} | |
2.4 后果截图
【猜拳 h5 游戏开发】
正文完