java猜拳程序_java小程序 猜拳

好久没写代码了  前天写了个猜拳  有改进或者错误的地方,希望大家指出   谢谢

核心判断:

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("谢谢使用!");

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值