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);
		}
	}
}


你可能感兴趣的:(java作业整理)