Java第一次作业

《Java技术》第一次作业

(一)学习总结

1.在java中通过Scanner类完成控制台的输入,查阅JDK帮助文档,Scanner类实现基本数据输入的方法是什么?不能只用文字描述,一定要写代码,通过具体实例加以说明。
Java.util.Scanner的主要功能是简化文本扫描。扫描控制台输入,将输入的文本转化为不同类型的值。Scanner的用法:

Scanner reader = new Scanner(System.in);
int yourNumber = reader.nextInt();
double nextDouble()输入的信息为double 类型
float nextFloat()输入的信息为float类型
Int nextint()输入的信息为int类型
nextLine()读取输入的下一行内容

2.Random类和Math类的random()方法都能产生随机数,这两种方式有什么区别,各有什么特点呢?查阅JDK帮助文档,并举例加以说明。
Random类是随机数产生类,可以指定随机数的范围,然后产生任意随机数。常用方法:

public boolean nextBoolean()随机生成boolean值
public double nextDouble()随机生成double值
public float nextFloat()随机生成float值
public int nextInt()随机生成int值
public int nextInt(int n)随机生成给定最大值的int值
public long nextLong()随机生成long值

产生1~100之间的随机数

int realNumber = random.nextInt(100)
Random random = new Random();

(二)实验总结

1.猜数游戏

  • 程序设计思路:由电脑随机产生随机数,如果猜的价格大于随机数则提示猜大了,如果猜的价格小于随机数则提示猜小了,用户根据猜大了猜小了提示进行猜价格,限定次数为10次超过次数则认定为失败

  • 实验问题分析:
    问题1:猜成功后,输出的次数不对
    原因:i的次数统计错误
    解决方案:初始赋值为0,第一次没有统计,修改初始赋值为1,程序运行正确
    问题2:第10次没有停止
    原因:进行了10次没有跳出
    解决方案:if语句,break直接跳出

     			int i = 1;
     			int yourNumber = 0;
     			i++;
     			System.out.print("物品的价格是1~100的整数,输入你的答案:");
     			yourNumber = reader.nextInt();
     			while(yourNumber != realNumber)
     			{
     				i++;
     				if(yourNumber < realNumber)
     				{
     					System.out.print("猜小了,再输入你的答案:");	
     					yourNumber = reader.nextInt();
     				
     				}
     				if(yourNumber > realNumber)
     				{
     					System.out.print("猜大了,再输入你的答案:");
     					yourNumber = reader.nextInt();
     			
     				}
     				if(i == 10)
     				{
     					System.out.print("很遗憾 ,次数太多游戏结束!");
     					break;
     				}
     			
     			}
    

2.万年历

  • 程序设计思路:输入年份判断不合法年份请重新输入,闰年的判定,每个月有多少天,计算1900年距离所输入年月有多少天,所对应的是星期几,并输出。

  • 实验问题分析:
    问题1:2月的天数不对
    原因:闰年的2月的判断不对,返回值的位置错误
    解决方案:修改了返回值的位置
    问题2:星期的位置不对
    原因:天数统计的问题
    解决方案:修改了闰年的问题就解决了

    	public static int days(int year ,int month){
    		int day=0;
    		if (month==1||month==3||month==5||month==7||month==8||month==10||month==12){
    			day=31;
    			}
    		if (month==4||month==6||month==9||month==11){
    			day=30;
    			}
    		while(month==2)
    			if(isLeapYear(year)){
    				day=29;
    				return day;
    			}
    			else {
    				day=28;
    				return day;
    			}
    		return day;
    		
    	} 
    
    	public static void printCalendar(int year, int month) {
    		int week;
    		week=1+TotalDays(year,month)%7;
    		System.out.println("星期日	星期一	星期二	星期三	星期四	星期五	星期六");
    		for(int i=0;i<week;i++){
    			if(week==7){
    				break;
    			}
    			System.out.print("	");
    			}
    			for(int i=1;i<=days(year,month);i++ ){
    				System.out.printf("%2d	",i);
    				week++;
    				if(week%7==0)
    					System.out.println("
    ");
    			}
    		System.out.print("		");
    		
    	}
    }
    

(三)[代码托管]

https://git.oschina.net/hebau_cs15/FMM.git

(四)学习进度条

代码行数(新增/累积) 学习时间(新增/累积) 本周学习内容
目标 5000行 300小时
第2-4周 350/350 30/30 学习了Java的预备作业及其基本知识
第5周
第6周
原文地址:https://www.cnblogs.com/miao0512/p/6576591.html