Java随机点名和猜拳游戏运行代码

随机点名集合方法

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Name1 {
    public static void main(String[] args) {
        //创建一个存储名字的集合
        List<String > names = new ArrayList<>();
        names.add("张三");
        names.add("麻子");
        names.add("王五");
        names.add("李四");
        // 随机选择一个名字
        String randomName = pickRandomName(names);
        System.out.println("被选中的名字是:"+randomName);

    }
    // 从名字集合中随机选择一个名字
    private static String pickRandomName(List<String> names) {
        Random random = new Random();
        int randomIndex = random.nextInt(names.size());// 生成随机索引
        return  names.get(randomIndex);// 返回对应索引处的名字
    }
}

随机点名数组方法

import java.util.Random;
public class Name2 {
    public static void main(String[] args) {
        // 创建一个包含名字的数组
        String[] Arr = {"张", "李", "王", "麻"};

        // 随机选择一个名字
        String randomName = getRandomName(Arr);
        System.out.println("选中的名字是:" + randomName);
    }

    // 从名字数组中随机选择一个名字
    public static String getRandomName(String[] Arr) {
        Random random = new Random();
        int index = random.nextInt(Arr.length); // 生成随机索引
        return Arr[index]; // 返回对应索引处的名字
    }
}

随机点名文件中读取

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;

public class Name3 {
    public static void main(String[] args) {
        String filePath = "E:\\workspace-java\\demo\\src\\names.txt"; // 文件路径
        ArrayList<String> names = readNamesFromFile(filePath); // 从文件中读取名字

        if (!names.isEmpty()) {
            String randomName = getRandomName(names); // 随机选择一个名字
            System.out.println("选中的名字是:" + randomName);
        } else {
            System.out.println("文件中不存在该名字");
        }
    }

    // 从文件中读取名字,并返回名字列表
    public static ArrayList<String> readNamesFromFile(String filePath) {
        ArrayList<String> names = new ArrayList<>();

        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = reader.readLine()) != null) {
                names.add(line); // 将每行名字添加到名字列表中
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return names;
    }

    // 从名字列表中随机选择一个名字
    public static String getRandomName(ArrayList<String> names) {
        Random random = new Random();
        int index = random.nextInt(names.size()); // 生成随机索引
        return names.get(index); // 返回对应索引处的名字
    }
}

猜拳游戏代码

import java.util.Random;
import java.util.Scanner;

public class RockPaperScissors {
    public static void main(String[] args) {
        // 定义石头、剪刀、布的代表数字
        final int ROCK = 0;
        final int PAPER = 1;
        final int SCISSORS = 2;

        // 定义用户输入
        Scanner scanner = new Scanner(System.in);

        // 定义计数变量
        int userWins = 0;
        int computerWins = 0;

        // 循环运行游戏
        while (true) {
            // 获取用户选择
            System.out.println("请选择:0 - 石头,1 - 剪刀,2 - 布");
            int userChoice = scanner.nextInt();

            // 随机生成计算机选择
            Random random = new Random();
            int computerChoice = random.nextInt(3);

            // 输出计算机选择
            System.out.print("计算机选择:");
            switch (computerChoice) {
                case ROCK:
                    System.out.println("石头");
                    break;
                case PAPER:
                    System.out.println("剪刀");
                    break;
                case SCISSORS:
                    System.out.println("布");
                    break;
            }

            // 判断胜负
            if (userChoice == computerChoice) {
                System.out.println("平局");
            } else if (userChoice == ROCK && computerChoice == SCISSORS ||
                    userChoice == PAPER && computerChoice == ROCK ||
                    userChoice == SCISSORS && computerChoice == PAPER) {
                System.out.println("你赢了");
                userWins++;
            } else {
                System.out.println("你输了");
                computerWins++;
            }

            // 询问是否继续游戏
            System.out.println("是否继续游戏?(y/n)");
            String continueChoice = scanner.next();
            if (!continueChoice.equalsIgnoreCase("y")) {
                break;
            }
        }

        // 游戏结束,输出结果
        System.out.println("游戏结束");
        System.out.println("你赢了 " + userWins + " 次");
        System.out.println("计算机赢了 " + computerWins + " 次");
    }
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员 小侯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值