⽯头剪⼑布的JAVA代码
import java.util.Scanner;
import java.util.Random;
class Tom {
//属性:结果(0; 1; 2)、胜利次数
int result;
int wins;
//胜利则 wins 加 1
public void winGame() {
wins += 1;
}
/
/输⼊结果(0; 1; 2), 并返回
public int getResult() {
System.out.println("请输⼊0(⽯头)、1(剪⼑)、2(布)");
Scanner scanner = new Scanner(System.in);
int result = Int();
return result;
}
//打印赢得次数
public void showWins() {
System.out.println("Tom赢了" + wins + "局");
}
简单的java游戏代码
}
class Machine {
int result;
// 返回机器结果
public int showResult() {
Random random = new Random();
result = Int(3);
System.out.println("机器输出" + result);
return result;
}
}
class Game {
Tom tom;
Machine machine;
int round;
// 传⼊Tom对象、machine对象、局数
public void gameStart(Tom tom, Machine machine, int round) {
for(int i = 1; i <= round; i++) {
System.out.println("===开始第" + i + "场游戏===");
int tomResult = Result();
int machineResult = machine.showResult();
gameRule(tom, tomResult, machineResult);
}
}
// 传⼊tom和机器结果判断输赢并记录
public void gameRule(Tom tom, int tomResult, int machineResult) {
if((tomResult == 0 && machineResult == 1) ||
(tomResult == 1 && machineResult == 2) ||
(tomResult == 2 && machineResult == 0)) {
tom.winGame();
}
}
}
public class pra34 {
public static void main(String args[]) {
int round = 4;
int round = 4;
Tom tom = new Tom();
Machine machine = new Machine();
Game game = new Game();
game.gameStart(tom, machine, round);  tom.showWins();
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。