0
点赞
收藏
分享

微信扫一扫

循环练习题2-综合练习石头剪刀布

爱喝酒的幸福人 2022-01-06 阅读 46

代码未精简,只做了简单验证,如有错误,还请指正。

import java.util.Scanner;

public class TwentySix {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        boolean choice1 = true;
        int playtimes = 0;
        int winNumber = 0;
        int rival = 0;

        do {
            System.out.println("************************************************************");
            System.out.println("欢迎ThreeGoldXD进入猜拳游戏");
            System.out.println("1.石头     2.剪刀     3.布     0.退出");
            System.out.println("************************************************************");
            System.out.println("请输入数字:");
            int choice = input.nextInt();

            if (choice == 0) {
                choice1 = false;
                double winRate = (double) ((double) winNumber / (double) playtimes);
                double winRate1 = (double) Math.round(winRate * 100) / 100; //保留两位小数

                System.out.println("                  排行榜                              ");
                System.out.println("******************************************************************");
                System.out.println("姓名" + "\t\t" + "总局数" + "\t" + "胜场" + "\t" + "胜率");
                System.out.println("ThreeGoldXD" + "\t  " + playtimes + "\t\t " + winNumber + "\t\t" + (int) (winRate1 * 100) + "%");

            } else if (choice == 1 || choice == 2 || choice == 3) {
                playtimes++;
                rival = (int) (Math.random() * 3 + 1);

                System.out.println("-------------------");
                if (choice == 1) System.out.print("|您:✊  VS");
                else if (choice == 2) System.out.print("|您:✌  VS");
                else if (choice == 3) System.out.print("|您:🖐  VS");

                if (rival == 1) System.out.println("  电脑:✊|");
                else if (rival == 2) System.out.println("  电脑:✌|");
                else if (rival == 3) System.out.println("  电脑:🖐|");

                if (choice == rival) System.out.println("-----平局再来------");
                else {
                    choice++;
                    if (choice > 3) choice = 1;
                    if (choice == rival) {
                        System.out.println("----恭喜您赢了-----");
                        winNumber++;
                    } else System.out.println("------您输了-------");
                }


            } else System.out.println("输入错误,请重新输入");
            System.out.println();
            System.out.println();
        } while (choice1);

    }
}
举报

相关推荐

0 条评论