好久没写代码了 前天写了个猜拳 有改进或者错误的地方,希望大家指出 谢谢
核心判断:
1:石头 2:剪刀 3:布
电脑 玩家 做差 输赢
1 1 0 平
2 -1 电脑win
3 -2 玩家win
2 1 1 玩家win
2 0 平
3 -1 电脑win
3 1 2电脑win
2 1 玩家win
3 0 平
下面是代码:
Compare.java //判断模块
package com.caiquan;
public class Compare {
public void compare(int c,int g){
if(c == g){
System.out.println("平局!");
}else if((c-g) == -1 || (c-g) == 2){
System.out.println("电脑win!");
}else{
System.out.println("You win!");
}
}
}
RandomTest.java //主模块
package com.caiquan;
import java.util.Random;
import java.util.Scanner;
public class RandomTest {
public static void main(String[] args) {
int mind = 1;
while(mind != 0){
System.out.println("please input your choose(1~3)<1:石头 2:剪刀 3:布>:");
Scanner input = new Scanner(System.in);
int gamer = input.nextInt();
if(gamer < 1 || gamer > 3){
System.out.println("your input is error!");
System.exit(0);
}
Random r = new Random();
int computer = r.nextInt(3)+1;
Compare co = new Compare();
switch(computer){
case 1: System.out.println("电脑出的是:石头");
co.compare(1,gamer);
break;
case 2: System.out.println("电脑出的是:剪刀");
co.compare(2,gamer);
break;
case 3: System.out.println("电脑出的是:布");
co.compare(3,gamer);
break;
}
System.out.println("你的游戏次数为:"+mind);
System.out.println("是否继续游戏(1:继续 2:退出)?");
int m = input.nextInt();
if(m < 1 || m > 2){
System.out.println("输入有误,游戏结束!");
System.exit(0);
}
if(m == 1){
mind++;
}else{
mind = 0;
}
}
System.out.println("谢谢使用!");
}
}