石头剪刀布小游戏

    xiaoxiao2021-03-25  188

    /*

    实现思路

    1.用数字来代表 1:石头 2:剪刀 3:布 2.我方出拳(Scanner) 3.电脑出拳(Random) 4.两者比对 */ public class 石头剪刀布 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); String str=null; do{ method3(); System.out.println("你还想继续挑战吗?y/n"); str = scan.next(); }while("y".equals(str)); } //自定义一个方法,我方出拳 public static int  method1(){ System.out.println("请出拳:"); Scanner scan = new Scanner(System.in); int A = scan.nextInt(); if(A==1){ System.out.println("我方出的是石头"); }else if(A==2){ System.out.println("我方出的是剪刀"); }else{ System.out.println("我方出的是布"); } return A; } //电脑出拳 public static int method2(){ Random ran = new Random(); int B = ran.nextInt(3)+1; if(B==1){ System.out.println("电脑出的是石头"); }else if(B==2){ System.out.println("电脑出的是剪刀"); }else{ System.out.println("电脑出的是布"); } return B; } //两者对比,判断输赢 public static void method3(){ int A = method1(); int B = method2(); if(A==B){ System.out.println("打平了。。。"); }else if((A==1&&B==2) ||  (A==2&&B==3) || (A==3&&B==1)){ System.out.println("恭喜,你赢了电脑"); }else{ System.out.println("你输了!"); } } }
    转载请注明原文地址: https://ju.6miu.com/read-93503.html

    最新回复(0)