java作业整理

1. 小游戏(一般)

要求:程序随机产生20—50根火柴,由人与计算机轮流拿,每次拿的数量不超过3根,拿到最后一根为胜。


public class Match {
     int number_Of_Match;
     public void random_Number(){
    	 number_Of_Match=(int)(Math.random()*30)+21;
    //	 return number_Of_Match;
     }
     public void machine_Operation(){
    	 int number_Of_Machine=(int)(Math.random()*3)+1;
    	 number_Of_Match-=number_Of_Machine;
     }
}


import java.util.Scanner;

public class MatchGame {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		int human_Number;
		Match match = new Match();
		match.random_Number();
		System.out.println("系统产生的随机数为:" + match.number_Of_Match);
		System.out.println("你是否想先开始游戏?YES/NO");
		String answer = input.nextLine();
		if (answer.equals("YES")) {
			while (match.number_Of_Match > 0) {
				System.out.println("请输入你所输入的数字,要求不超过3:");
				human_Number = input.nextInt();
				match.number_Of_Match -= human_Number;
				if (match.number_Of_Match <= 0) {
					System.out.println("你赢了!");
					System.exit(0);
				}
				match.machine_Operation();
				System.out.println(match.number_Of_Match);
				if (match.number_Of_Match <= 0) {
					System.out.println("机器赢了。。。");
					System.exit(0);
				}
			}
		} else if (answer.equals("NO")) {
			while (match.number_Of_Match > 0) {
				match.machine_Operation();
				System.out.println(match.number_Of_Match);
				if (match.number_Of_Match <= 0) {
					System.out.println("机器赢了。。。");
					System.exit(0);
				}
				System.out.println("请输入你所输入的数字:");
				human_Number = input.nextInt();
				match.number_Of_Match -= human_Number;
				if (match.number_Of_Match <= 0) {
					System.out.println("你赢了!");
					System.exit(0);
				}
			}
		} else {
			System.exit(1);
		}
	}
}


posted @   wojiaohuangyu  阅读(6)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
点击右上角即可分享
微信分享提示