java 石头剪刀布 赢了才能退出 两种方法

java 石头剪刀布 赢了才能退出 两种方法,第1张

java 石头剪刀布 赢了才能退出 两种方法

1.

public class Test09_02 {
    public static void main(String[] args) {
        System.out.println("欢迎来到猜拳游戏:t 1:代表剪刀 t 2:代表石头 t 3:代表布");
        //记录赢得次数
        int num = 0;        
        //电脑随机生成1-3;
        int i = (int) (Math.random()*3+1) ;
        System.out.println("电脑出的是"+i);
        //判断赢了几句;
        while (num<1) {
        System.out.println("请输入1-3:");
        Scanner input = new Scanner(System.in);
        int b = input.nextInt();        
        //判断用户输入的数是否是1-3
        while (b<1 || b>3) {
            //不是1-3,重新输入     
            b = input.nextInt();            
        }  // -1,2输  0平局  2,1 赢了.
        int c = b -i;
        switch (c) {
            case -2:
                System.out.println("赢了");
                num++;
                break;
            case -1:
                System.out.println("输了请继续");
                break;
            case 0:
                System.out.println("平局请继续");
                break;
            case 1:
                System.out.println("恭喜赢了");                
                num++;
                break;
            case 2:
                System.out.println("输了继续");
                break;
            } 
        }
        System.out.println("游戏结束!");
    }

 2

public class Test09 {
    public static void main(String[] args) {
        //产生1-3的随机数;
        int i = (int)(Math.random()*3+1);
        System.out.println(i);
        int choose = 0;
        //提示用户输入1-3                
        System.out.println("请输入1-3:nt1:剪刀nt2:石头nt3:布");
        Scanner input = new Scanner(System.in);
        choose = input.nextInt();    
             while (choose > 3 || choose <1){  //判断用户输入的数是否在1-3            
            System.out.println("输入有误!"
                    + "请输入1-3:nt1:剪刀nt2:石头nt3:布");
            choose = input.nextInt();    
            }  if (choose == i) {
            System.out.println("平局!");
        }  else if (i==1 && choose == 2){
            System.out.println("恭喜!你赢了!");
        }  else if (i==1 && choose == 3) {
            System.out.println("你输了!");
        } else if (i==2 && choose == 3) {
            System.out.println("你输了!");
        }  else if (i==2 && choose == 1) {
            System.out.println("恭喜!你赢了!");
        }  else if (i==3 && choose == 1) {
            System.out.println("你输了!");
        } else  if (i==3 && choose == 2) {
            System.out.println("恭喜!你赢了!");
        } 
        System.out.println("程序结束!");  
    }

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/zaji/5661421.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存