JAVA 第1-4次上机作业

import java.util.Scanner;
public class Work {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.println("请输入总人数:");
		int sum = input.nextInt();//sum为总人数
		int[]flag = new int[sum+1];//flag数组表示每个人心中的密码
		int i;
		int count = 0;//记录出局的人数
		int num = 0;//报数器 
		for(i = 1;i <= sum;i++) {
		    System.out.println("请输入第【"+ i +"】个人的密码:");
			flag[i] = input.nextInt();
		}//此循环舍去下标为1的元素 
		System.out.print("请输入初始密码:");
		int key = input.nextInt();
		//输入部分结束
		while(count < sum - 1){
			for(i = 1;i <= sum;i++){
				if(flag[i]!=0){
					num++;
				    if(num == key){
					System.out.println("序号为【"+ i +"】的人退出,不必外出寻找食物!");
					count++;
					key = flag[i];//退出的人心里的密码称为新的初始密码key 
					flag[i] = 0;
					num = 0;
				    }
					if(count == sum - 1)
				     break;
				}
			}
		} 
		for(i=1;i<=sum;i++){
			if(flag[i]!=0)
			 System.out.println("序号为【"+i+"】的人被选中为外出寻找食物的人!");
		}
	}

}

第二次

public class Homework02 {
	public static void main(String[] args){
		boolean stone = false;//stone表示石头的状态,false为粗糙面,true为光滑面;
		//开始时,石头为花纹面,且所有人都知道
		int[]people = new int[51];
		int i,num,sum=0,count=0,day=0;//count用于计算已确定健在的葫芦娃人数,day用于计算天数
		//初始化数组
		for(i=0;i<51;i++){
			if(i==0)
				people[i]=1;//a[0]=1表示老爷爷的牢房
			else
				people[i]=0;//其余值为0的元素表示葫芦娃
		}
		for(i=0;i<10;i++) {
			for(count=0;count<=49;) {
				num=(int)(Math.random()*50);
				if(num!=0){
					stone = true;
					day++;
				}
				if(num==0){
					stone = false;
					count++;
					day++;
				}
			}
			System.out.println("第"+i+"次取样的值为:"+day);
			sum+=day;
			day=0;
			stone=false;
			count=0;
		}
		System.out.println("取样10次的平均值为:"+(sum/10));
	}

}

第三次

import java.util.Scanner;
public class Calendar {
	public static void main(String[] agrs) {
		Scanner input = new Scanner(System.in);
		//输入部分开始
		System.out.println("请输入年份:");
		int year = input.nextInt();
		System.out.println("请输入该年第一天的星期数");
		int number = input.nextInt();
		boolean isLeapYear;
		//判断闰年
		if((year % 4 == 0 && year % 100 != 0)||(year % 400 ==0)) {
			isLeapYear = true;
		}
		else isLeapYear = false;
		//输出部分开始
		int month,i,last;
		for(month = 1;month <= 12;month++) {
			//输出表头部分
			switch(month) {
			case 1:System.out.println("       January "+ year +"          ");break;
			case 2:System.out.println("       Frebraury "+ year +"          ");break;
			case 3:System.out.println("       March "+ year +"          ");break;
			case 4:System.out.println("       April "+ year +"          ");break;
			case 5:System.out.println("       May "+ year +"          ");break;
			case 6:System.out.println("       June "+ year +"          ");break;
			case 7:System.out.println("       July "+ year +"          ");break;
			case 8:System.out.println("       August "+ year +"          ");break;
			case 9:System.out.println("       September "+year +"          ");break;
			case 10:System.out.println("       Octuber "+ year +"          ");break;
			case 11:System.out.println("       November "+ year +"          ");break;
			case 12:System.out.println("       December "+ year +"          ");
			}
			System.out.println("_____________________________");
			System.out.println("Sun Mon Tue Wed Thu Fri Sat");
			//开始输出1月
			if(month == 1) {
				for(i = number;i > 0;i--) {
					System.out.print("    ");
				}
				for(i = 1;i <= 31;i++) {
					if(i<10)
					 System.out.print(" "+i+"  ");
					else
						System.out.print(" "+i+" ");
					if((i + number) % 7 == 0) {
						System.out.printf("\n");
					}
				}System.out.printf("\n");
				//更新number的值
				if(number > 4) {
					last = 42 - 31 - number;//last代表每月日历后空余的格子数
					number = 7 - last;
				}
				else {
					last = 35 - 31 - number;
				    number = 7 - last;
				}
			}
			//1月输出结束
			//开始输出2月
			int day_2;
			if(isLeapYear == true)
				day_2 = 29;
			else day_2 = 28;
			if(month == 2) {
				for(i = number;i > 0;i--) {
					System.out.print("    ");
				}
				for(i = 1;i <= day_2;i++) {
					if(i < 10)
					 System.out.print(" " + i + "  ");
					else
						System.out.print(" " + i + " ");
					if((i + number) % 7 == 0) {
						System.out.printf("\n");
					}
					
				}System.out.printf("\n");
				
					last = 35-day_2-number;
					if(last!=0)
					     number = 7 - last;
					else number = 0;
				
			}
			//2月输入结束
			//开始输出其他月份
			int day;
			if(month==3||month==5||month==7||month==8||month==10||month==12) {
				day=31;
				for(i = number;i > 0;i--) {
					System.out.print("    ");
				}
				for(i = 1;i <= day;i++) {
					if(i < 10)
					 System.out.print(" " + i + "  ");
					else
						System.out.print(" " + i + " ");
					if((i + number) % 7 == 0) {
						System.out.printf("\n");
					}
					
				}System.out.printf("\n");
				if((number > 4 && day == 31)||(number > 5 && day == 30)) {
					last = 42 - day - number;
					number = 7 - last;
				}
				else {
					last = 35 - day - number;
					if(last != 0)
					     number = 7 - last;
					else number = 0;
				}
			}
			if(month == 4 || month == 6 || month == 9 || month == 11) {
				day = 30;
				for(i = number;i > 0;i--) {
					System.out.print("    ");
				}
				for(i = 1;i <= day;i++) {
					if(i < 10)
					 System.out.print(" " + i + "  ");
					else
						System.out.print(" " + i + " ");
					if((i + number) % 7 == 0) {
						System.out.printf("\n");
					}
				}System.out.printf("\n");
				if((number > 4 && day == 31)||(number > 5 && day == 30)) {
					last = 42 - day - number;
					number = 7 - last;
				}
				else {
					last = 35 - day - number;
					if(last != 0)
				     number = 7 - last;
					else number = 0;
				}
			}
		}
	}

}

第三次作业PLUS

import java.util.Scanner;


public class Homework031 {
	public static void main(String[] agrs) {
		Scanner input = new Scanner(System.in);
		//输入部分开始
		System.out.println("请输入年份:");
		int year = input.nextInt();
		System.out.println("请输入月份:");
		int month = input.nextInt();
		//输入部分结束
		//定义一个输出表头的方法
		printHead(month,year);
		int startDay;
		startDay=getStartDay(month,year);
		printBody(startDay,month,year);
	}
		public static void printHead(int month,int year){
			switch(month) {
			case 1:System.out.println("       January "+ year +"          ");break;
			case 2:System.out.println("       Frebraury "+ year +"          ");break;
			case 3:System.out.println("       March "+ year +"          ");break;
			case 4:System.out.println("       April "+ year +"          ");break;
			case 5:System.out.println("       May "+ year +"          ");break;
			case 6:System.out.println("       June "+ year +"          ");break;
			case 7:System.out.println("       July "+ year +"          ");break;
			case 8:System.out.println("       August "+ year +"          ");break;
			case 9:System.out.println("       September "+year +"          ");break;
			case 10:System.out.println("       Octuber "+ year +"          ");break;
			case 11:System.out.println("       November "+ year +"          ");break;
			case 12:System.out.println("       December "+ year +"          ");
			}
			System.out.println("_____________________________");
			System.out.println("Sun Mon Tue Wed Thu Fri Sat");
		}
		//定义一个判断闰年的方法
		public static boolean isLeapYear(int year){
			if((year % 4 == 0 && year % 100 != 0)||(year % 400 ==0)) {
				return true;
			}
			else return false;
		}
		
		public static int getStartDay(int month,int year){
			final int START_DAY_FOR_JAN_1_1800 = 3;
			int total=0,i;
			for(i=1800;i<year;i++)
				if(isLeapYear(i)==true)
					total+=366;
				else
					total+=365;
			for(i=1;i<month;i++)
				total+=getNumberOfMonth(i,year);
			return (total+START_DAY_FOR_JAN_1_1800)%7;
		}
		//定义一个获取月份天数的方法
		public static int getNumberOfMonth(int month,int year){
			if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
				return 31;
			else if(month==2){
				if(isLeapYear(year)==true)
					return 29;
				else return 28;
			}
			else return 30;
		}
		//定义一个打印月历体的方法
		public static void printBody(int startDay,int month,int year){
			int day,i;
			day=getNumberOfMonth(month,year);
			for(i = startDay ;i > 0;i--) {
				System.out.print("    ");
			}
			for(i = 1;i <= day;i++) {
				if(i < 10)
				 System.out.print(" " + i + "  ");
				else
					System.out.print(" " + i + " ");
				if((i + startDay) % 7 == 0) {
					System.out.printf("\n");
				}
		    }
		}
	
}

 

第4次

import java.util.Scanner;
import java.util.Random;
public class guessCapital {

	public static void main(String[] args) {
		String[][] capital = {
				{"Beijing","Beijing"},
				{"Shanghai","Shanghai"},
				{"Tianjin","Tianjin"},
				{"Chongqing","Chongqing"},
				{"Heilongjiang","Haerbin"},
				{"Jilin","Changchun"},
				{"Liaoning","Shenyang"},
				{"Neimenggu","Huhehaote"},
				{"Hebei","Shijiazhuang"},
				{"Xinjiang","Wulumuqi"},
				{"Gansu","Lanzhou"},
				{"Qinghai","Xining"},
				{"Shaanxi","Xi'an"},
				{"Ningxia","Yinchuan"},
				{"Henan","Zhengzhou"},
				{"Shandong","Jinan"},
				{"Shanxi","Taiyuan"},
				{"Anhui","Hefei"},
				{"Hubei","Wuhan"},
				{"Hunan","Changsha"},
				{"Jiangsu","Nanjing"},
				{"Sichuan","Chengdu"},
				{"Guizhou","Guiyang"},
				{"Yunnan","Kunming"},
				{"Guangxi","Nanning"},
				{"Xizang","Lasa"},
				{"Jiangxi","Nanchang"},
				{"Guangdong","Guangzhou"},
				{"Fujian","Fuzhou"},
				{"Taiwan","Taibei"},
				{"Hainan","Haikou"},
				{"Xianggang","Xianggang"},
				{"Aomen","Aomen"}
				//33
			};
		Scanner input = new Scanner(System.in);
		int i,j,count=0;
		for(i=0;i<5;i++){
			j=(int)(Math.random()*34);
			System.out.println("******************************************");
			System.out.println("What is the capital of "+capital[j][0]+"?");
			String s = input.next();
			if(s.equalsIgnoreCase(capital[j][1])==true){
				count++;
				System.out.println("Your answer is correct");
			}
			else{
				System.out.println("The correct answer should be "+capital[j][1]);
			}
		}
		System.out.println("*********************");
		System.out.println("*The correct count is "+count+"*");
		System.out.println("*********************");

	}

}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值