《Java技术》第1次作业

(一)学习总结

1.在java中通过Scanner类完成控制台的输入,查阅JDK帮助文档,Scanner类实现基本数据输入的方法是什么?


    Scanner year1 = new Scanner(System.in);
    int year = year1.nextInt();        //输入年份

2.Random类和Math类的random()方法都能产生随机数,这两种方式有什么区别,各有什么特点呢?

1.Math类的random()方法
        int num=(int)(Math.random()*n);                         //返回大于等于0小于n之间的随机数  
        int num0=m+(int)(Matn.randon()*n);                   //返回大于等于m小于m+n(不包括m+n)之间的随机数 
    经过改动也可以用于产生随机字符(比如字母)
        (char)('a'+Math.random()*('z'-'a'+1)                   //随机生成a~z之间的字符
        (char)(cha1+Math.random()*(cha2-cha1+1));   //随机生成cha1~cha2的字符

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

不等于0.3;
因为计算机对数据做处理的时候会先把十进制数转化为二进制数,而0.1转化为二进制数后产生的是一个无限循环小数,而double是有限的,三个无限循环的小数相加,double只能取其结果的一部分,而后面无限长的那一部分就会丢失掉,所以结果会不等于0.3;
为了处理精度损失的问题,可以使用java.math.BigDecimal类

   BigDecimal d = new BigDecimal(1.0 / 3.0);            //定义一个无限小数
   DecimalFormat df = new DecimalFormat("0.00"); //定义数据格式,保留两位小数
   String s = df.format(d);                                         //通过定义的数据格式得到字符串,s的值为"0.33"

4.本次学习要点中其他需要总结的内容:

sort(a,a+10)//从小到大排序,对起始地址为a,连续的10个数据进行排序;
return 后面不能接break;

(二)实验总结

实验内容:

1.看商品猜价格

import java.util.*;

public class Jiage2 {

	public static void main(String[] args) {
		int i,sum=0,j=0;
		Scanner price = new Scanner(System.in);
		Random rand = new Random();
		int b = rand.nextInt(100);
		String str1 = new String("yes");
		Scanner str = new Scanner(System.in);
		String str2 = new String("yes");
		while (str2.equals(str1)) {
			System.out.println("接下来进行猜价格,你一共有五次机会:");
			go: for (i = 0; i < 5; i++)
			// while(price.hasNextInt())
			{
				j++;
				int a = price.nextInt();
				if (a == b) {
					System.out.println("恭喜你猜对了,你的得分为" + ((5 - i) * 20) + "次");
					sum+=(5 - i) * 20;
					break go;
				} else if (a > b) {
					System.out.println("猜大了,你还有" + (5 - i - 1) + "次机会");
				} else if (a < b) {
					System.out.println("猜小了,你还有" + (5 - i - 1) + "次机会");
				}
			}
			if (i == 5) {
				System.out.println("游戏结束,你的得分为0,实际价格为" + b);
			}
			System.out.println("是否继续游戏,如果要继续请输入yes,退出输入no");
			str2 = str.next();
		}
		System.out.println("你一共猜了"+j+"次"+"你的总分为"+sum);
	}

}

2.万年历

import java.util.*;

public class Wannianli {

	public static void main(String[] args) {
		Scanner year1 = new Scanner(System.in);
		Scanner month1 = new Scanner(System.in);
		System.out.println("请输入年份和月份:");
		int year = year1.nextInt();
		int month = month1.nextInt();
		printCalender(year, month);
	}

	public static boolean isLeap(int x) {
		if ((x % 4 == 0 && x % 100 != 0) || x % 400 == 0)
			return true;
		else
			return false;
	}

	public static int days(int x, int y) {
		int a = 0;
		switch (y) {
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			a = 31;
			break;
		case 2:
			if (isLeap(x))
				a = 29;
			else
				a = 28;
			break;
		case 4:
		case 6:
		case 9:
		case 11:
			a = 30;
		}
		return a;

	}

	public static int totaIDdays(int x, int y) {
		int days1 = 0;
		for (int i = 1900; i < x; i++) {
			if (isLeap(i))
				days1 += 366;
			else
				days1 += 365;
		}
		for (int j = 1; j < y; j++)
			days1 += days(x, j);
		return days1;
	}

	public static int wEEK(int x, int y) {
		int days = totaIDdays(x, y);
		System.out.println("距离1900年1月1日一共有" + days + "天");
		return (days % 7) + 1;
	}

	public static void printCalender(int year, int month) {
		if (isLeap(year))
			System.out.println("今年是闰年");
		else
			System.out.println("今年是平年");
		int week = wEEK(year, month);
		System.out.println("星期日	星期一	星期二	星期三	星期四	星期五	星期六");
		int monthday = days(year, month);
		int x = 0;
		if (week != 7)
			for (int i = 0; i < week; i++) {
				System.out.print("	");
				x++;
			}
		for (int i = 1; i <= monthday; i++) {
			System.out.printf("%3d	", i);
			x++;
			if (x % 7 == 0)
				System.out.println();
		}
	}
}

问题: return 后面接了break,导致报错;
解决方法:去掉原有位置的break或者将要返回的值存到一个变量中,在结尾返回这个变量;

3.评分系统

import java.util.*;

public class pingfen {

	public static void main(String[] args) {
		int score[][] = { { 3, 5, 7, 9, 10, 10, 5, 8, 10, 5 }, { 10, 1, 5, 6, 4, 10, 3, 1, 5, 7 },
				{ 5, 6, 8, 10, 4, 10, 7, 5, 10, 5 }, { 8, 5, 6, 7, 9, 10, 4, 5, 3, 5 },
				{ 7, 8, 6, 4, 10, 9, 2, 3, 1, 4 } };
		float index[][] = new float[5][2];
		for (int i = 0; i < 5; i++)
			Arrays.sort(score[i]);
		int max[] = new int[5];
		int min[] = new int[5];
		float ave[] = new float[5];
		for (int i = 0; i < 5; i++)
			max[i] = maxData(score[i]);
		for (int i = 0; i < 5; i++)
			min[i] = minData(score[i]);
		for (int i = 0; i < 5; i++)
			ave[i] = aveData(score[i]);
		for (int i = 0; i < 5; i++) {
			index[i][0] = i;
			index[i][1] = ave[i];
		}
		Arrays.sort(ave);
		for (int i = 4; i >= 0; i--) {
			to: for (int j = 4; j >= 0; j--) {
				if (ave[i] == index[j][1]) {
					index[j][1] = -1;
					System.out.println("学号" + (index[j][0] + 1) + "得分" + ave[i]);
					break to;
				}
			}

		}

	}

	public static int maxData(int score[]) {
		return score[4];
	}

	public static int minData(int score[]) {
		return score[0];
	}

	public static float aveData(int score[]) {
		float sum = 0;
		for (int i = 1; i < 4; i++)
			sum += score[i];
		return sum / 8.0f;
	}
}

问题1:sort能对排平均分排序,但是却不能保留该得分是属于那一名同学的;
解决方法:把学号与对应的平均分存到一个新数组里面(数组index),将之前排好序的平均分输出之前先和index数组进行比较,找出该得分学生的学号(为了避免有得分相同的同学,当该学号同学被调用之后,将其在indxe数组里的得分改为-1),然后将学号和得分一起输出;

原文地址:https://www.cnblogs.com/w123/p/8548848.html