java第一次作业

(一)学习总结
1.在java中通过Scanner类完成控制台的输入,查阅JDK帮助文档,Scanner类实现基本数据输入的方法是什么?不能只用文字描述,一定要写代码,通过具体实例加以说明。

package test;

import java.util.Scanner;

public class Diyiti {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
       Scanner input=new Scanner(System.in);
       String line=input.next();
       System.out.println("你所输入的是"+line);
	}

}

2.Random类和Math类的random()方法都能产生随机数,这两种方式有什么区别,各有什么特点呢?查阅JDK帮助文档,并举例加以说明。
1.Random类产生的随机数,在其最大值范围内,按照概率均匀分布的。
2.Math.random()返回的只是从0到1之间的小数,如果要50到100,就先放大50倍,即0到50之间,这里还是小数,如果要整数,就要强制转换int。

package test;
import java.util.Random;
public class Dierti {

	public static void main(String[] args) {
		Random a=new Random();
		int num1=a.nextInt();
		double num2=Math.random();
		System.out.println("Random()产生的随机数"+num1);
		System.out.println("Math.random()产生的随机数"+num2);
	}
}

3.运行下列程序,结果是什么?查阅资料,分析为什么。

public class Test {
    public static void main(String args[]) { 
        double a = 0.1;
        double b = 0.1;
        double c = 0.1;
        if((a + b + c) == 0.3){
            System.out.println("等于0.3");
        }else {
            System.out.println("不等于0.3");
        }
    }     
}

为了处理精度损失的问题,可以使用java.math.BigDecimal类,查阅JDK帮助文档或教材p378,对上述程序进行修改。

运行结果

修改后程序

package test;
import java.math.BigDecimal;
import java.math.MathContext;
public class xiugai {
        public static void main(String args[]) { 
            double a = 0.1;
            double b = 0.1;
            double c = 0.1;
            if((a+ b + c) >0.3){
             System.out.println("等于0.3");
         }
            else {
             System.out.println("不等于0.3");
         }
     }     
 }

4.本次学习要点中其他需要总结的内容:因人而异,根据自己的学习情况,记录难掌握或难理解的内容,以及学有心得的内容。还存在哪些问题,也可以提出来,对于同学在博客中提出的问题,大家可以积极讨论,互帮互学。

(二)实验总结
实验内容:
1.看商品猜价格
1用随机数产生一个商品的价格;
2利用for循环进行价格比较;
2.万年历
1首先判断是否是闰年,闰年的2月份和平年的2月份的天数;
2判断天数为31天、30天的月份;
1.

package pi;
import java.util.*;
public class choujiang {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in=new Scanner(System.in);
		Scanner input=new Scanner(System.in);
		Random cai=new Random();//创建一个随机对象cai
		int x,a,b=0,c,huida=0,x=0;
		String str=null;
		x=cai.nextInt(100)+1;
		String an="n";
		do{
			
			jump:for(int i=0;i<=4;i++){
				System.out.print("请输入猜的价格");
				a=Integer.parseInt(input.next());
				b++;
				if(a>x){
					System.out.println("太大了");
				}
				else if(a<x){
					System.out.println("太小了");
				}
				else if(a==x){
					System.out.println("答对了");
					break jump;
				} 
			}
			System.out.println("您没有机会了");	
			System.out.println("您总共猜了"+(b)+"次");
			System.out.println("继续请输入n");
			an=input.next();
		}while("n".equals(an));
		
		
		if(b>10)
		{
			huida=0;
		}
		else if(b>=0&&b<=10){
		huida=100-10*b;
		}
		System.out.println("正确答案:"+x);
		System.out.println("分数:"+huida);
	}

}
package pi;

import java.util.*;

public class wannianli {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int year, month, x,sum=0;
		Scanner input = new Scanner(System.in);
		System.out.println("请输入年:");
		year = input.nextInt();
		System.out.print("请输入月份:");
		month = input.nextInt();
		int day = 0;
		int firstday = 0;
		int days = 0;
		
		
	if(isLeap(year)){
		System.out.println(""+year+"是闰年");
	}
	else{
		System.out.println(""+year+"是平年");
	}

	day=days(year,month);
	System.out.println(year+"年"+month+"月有"+day+"天");
	printCalender(year,month);
}
public static boolean isLeap(int year) { // 判断闰年
		
		if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
			return true;
		}
		else
			return false;
	}
public static int days(int year, int month) { // 判断某月天数
		 int days=0;
		 if (month == 2) {
				if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
					days = 29;
				} 
				else {
					days = 28;
				}
			} 
			else if (month == 4 || month == 6 || month == 9 || month == 11) {
				days = 30;
			} 
			else {
				days = 31;
			}
		 return days;
	 }
public static void printCalender(int year, int month) { // 计算天数差距
		int x,sum=0;
		int day=0;
		for (int i = 1900; i < year; i++) {
			if ((i % 4 == 0 && i % 100 != 0) || (i % 400 == 0)) {
				day = day + 366;
			} else {
				day = day + 365;
			}
		}
		int beforedays = 0;
		for (int i = 1; i < month; i++) {
			if (i == 2) {
				if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
					sum += 29;
				} 
				else {
					sum += 28;
				}
			}
			else {
				if (i == 4 || i == 6 || i == 9 || month == 11) {
					sum += 30;
				}
				else {
					sum += 31;
				}
			}
		}
		int days = 0;
		if (month == 2) {
			if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
				days = 29;
			} 
			else {
				days = 28;
			}
		} 
		else if (month == 4 || month == 6 || month == 9 || month == 11) {
			days = 30;
		} 
		else {
			days = 31;
		}
		System.out.println("");
		int temp =sum % 7;
		/* 输出 */
		System.out.println("星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六");
		for (int i = 1; i < temp; i++) {
			System.out.print("\t");
		}
		for(int i=1;i<=days;i++){

			if(sum%7==0){
				System.out.print(i+"\n");
			}
			else{
				System.out.print(i+"\t");
			}
			sum+=1;
		}
		System.out.println("");
		day=sum+day; 
		System.out.println(""+year+"年"+month+"月距1900年1月1日"+day+"天");
	}
}
posted @ 2018-03-18 23:01  呦呵  阅读(133)  评论(0编辑  收藏  举报