《Java技术》第一次作业

时间:2023-03-09 06:24:56
《Java技术》第一次作业

(一)学习总结

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

(1)使用Scanner必须在开头加入import java.util.Scanner;Scanner in = new Scanner(System.in);字符串类型遇到空格或者回车就结束输入,需要用到nextline()

import java.util.Scanner;
public class Scan {
public static void main(String[] args) { Scanner scan = new Scanner(System.in); // 从键盘接收数据
System.out.println("输入数据:");
String str = scan.next();
System.out.println("输入的数据为:" + str);
Scanner sc = new Scanner(System.in);
System.out.println("输入数据:");
String s = sc.nextLine();
System.out.println("输入的数据为:" + s); }
}

输出结果

《Java技术》第一次作业

(2)在Scanner中使用int型,float型等,可以用固定格式Scanner in = new Scanner(System.in);int a = in.nextInt();float b = in.nextFloat();等等

import java.util.Scanner;
public class Scan {
public static void main(String[] args) { Scanner in = new Scanner(System.in);
System.out.print("输入数据:");
int a = in.nextInt();
System.out.println("int 输出数据"+a);
System.out.print("输入数据:");
float b = in.nextFloat();
System.out.println("float 输出数据"+b);
System.out.print("输入数据:");
double c = in.nextDouble();
System.out.println("double 输出数据"+c);
}
}

输出结果:

《Java技术》第一次作业

2.Random类和Math类的random()方法都能产生随机数,这两种方式有什么区别,各有什么特点呢?查阅JDK帮助文档,并举例加以说明。

(1)Math类的random()方法,方法生成的是一个double类型的范围在[0,1)的数字,不包括1,要想变成int型,需要进行转化;要想产生在(0~100),必须在其后面乘以相应的数字,则得到的就是 [0,100)之间随机double类型数字

public class Pu{
public static void main(String args[]){
double a=(int)(Math.random());
int b=(int)(Math.random()*100);
System.out.println("输入数据"+a);
System.out.println("输出数据"+a);
System.out.println("输入数据"+b);
System.out.println("输出数据"+b);
}
}

结果:

《Java技术》第一次作业

(2)开头必须有“import java.util.Random”,且Random类产生的随机数,.nextFloat(),.nextInt()等等,在括号里面输入多少,就产生多大范围的值

import java.util.Random;
public class Pu{
public static void main(String args[]){
Random sa=new Random();
int a;int b;
a=sa.nextInt(100);
b=sa.nextInt(50);
int sum=a+b;
System.out.println("输入数据:"+a+"输入数据:"+b);
System.out.println("输出数据:"+sum);
}
}

结果:

《Java技术》第一次作业

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”

原因: java中简单类型不能够精确的对浮点数进行运算,需要用BigDecimal类

正确代码

import java.math.BigDecimal;
public class Test {
public static void main(String args[]) {
BigDecimal a = new BigDecimal(0.1);
BigDecimal b = new BigDecimal(0.1);
BigDecimal c = new BigDecimal(0.1);
BigDecimal d = new BigDecimal(1);//任何一个数字都是除以1都是原数字
System.out.println(a.add(b).add(c));
//ROUND_HALF_UP是BigDecimal的一个常量,表示进行四舍五入
if(a.add(b).add(c).divide(d,2,BigDecimal.ROUND_HALF_UP).doubleValue()==0.3){
System.out.println("等于0.3");
}else {
System.out.println("不等于0.3");
}
}
}

结果

《Java技术》第一次作业

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

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

答:(1)关于精度问题不能灵活运用

(2)对于java与C语言的不同,有点转化不过来

(3)平常需要多写写代码,才能灵活运用java来编写程序,而不是将C语言与java混合在一起

(二)实验总结

实验内容:

1.看商品猜价格

(1)代码

import java.util.Scanner;
import java.util.Random; public class Price { public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Random rand = new Random(); int num = rand.nextInt(100);
System.out.println(num);
int n = 0; for (int i = 1; i <= 10; i++) {
int price = scan.nextInt();
if (num == price) {
System.out.println("输出结果:" + num);
} else if (num < price) {
System.out.println(" too big");
} else {
System.out.println(" too small");
}
n++;
System.out.println("次数" + n);
} }
}

(2)结果

《Java技术》第一次作业

(3)思路

猜大还是猜小,必须先产生一个随机数,用Random类来产生随机数,用Scanner类来进行输入数,然后将自己猜想的价格与随机产生的值进行比较,若自己猜出的值比随机产生的值小,则说太小了,若自己猜出的值比随机产生的值大,则说太大了,且一共有10次机会,若10次都没有才对则直接输出结果。

(4)问题 再输出猜了多少次时,不知道该写到哪里

解决方案 经过了多次尝试,才找到如何很好的输出自己比较了多少次

2.万年历

(1)代码

import java.util.Scanner;

public class Calender {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("年份");
int year = sc.nextInt();
System.out.println("月份");
int month = sc.nextInt();
printCalender(year, month);
} public static boolean isLeap(int year) {
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
return true;
} else {
return false;
}
} public static int days(int year, int month) {
int day = 0;
switch (month) {
case 1:
day = 31;
case 2:
if (isLeap(year)) {
day = 29;
} else {
day = 28;
}
case 3:
day = 31;
case 4:
day = 30;
case 5:
day = 31;
case 6:
day = 30;
case 7:
day = 31;
case 8:
day = 31;
case 9:
day = 30;
case 10:
day = 31;
case 11:
day = 30;
case 12:
day = 31;
}
return day;
} public static int totalDays(int year, int month) {
int sum = 0;
for (int i = 1990; i < year; i++) {
if (isLeap(year)) {
sum += 366; } else {
sum += 365;
}
}
for (int i = 1; i < month; i++) {
sum = sum + days(year, i);
}
return sum; } public static void printCalender(int year, int month) {
int weekday = (totalDays(year, month) + 1) % 7;
System.out.println("Sun\tMon\tTue\tWed\tThu\tFri\tSut\n");
for (int i = 1; i <= weekday; i++) {
System.out.print("\t");
}
for (int x = 1; x <= days(year, month); x++) {
System.out.print(x + "\t");
if ((x + weekday) % 7 == 0) {
System.out.println();
}
}
}
}

(2)结果

《Java技术》第一次作业

(3)思路

先判断是否为闰年,若是闰年,二月有29天,其他月份天数相同,一年有366天;否则,二月有28天,其他月份天数相同,一年有365天;将输入的年份和月份,计算出总天数加1,除7求余,可求出星期;根据(x + weekday) % 7 == 0可以求出每天对应的日期

(4)问题 不能够打印出正确的万年历,并且输出的日历格式不正确

原因 代码写的不正确,没有利用(x + weekday) % 7 == 0,而是直接写成了weekday % 7 == 0,导致结果出错

解决发案 从网上查找,和同学相互讨论

3.评分系统

(1)代码

import java.util.Scanner;
import java.util.Arrays;
public class Score {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int k[][] = new int[5][10];
System.out.println("请第评委给选手打分");
for (int i = 0; i < 5; i++)
for (int j = 0; j < 10; j++) {
k[i][j] = in.nextInt();
}
double[] score = new double[5];
score = average(k);
Arrays.sort(score);
System.out.println("选手的排名情况为"); for (int i = 4; i >= 0; i--) {
System.out.println(score[i]);
}
}
public static int Max(int k[][]) {
int a = 0;
int max = k[a][1];
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 10; j++) {
if (k[i][j] > max)
max = k[i][j];
}
}
return max;
}
public static int Min(int k[][]) {
int a = 0;
int min = k[a][1];
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 10; j++) {
if (k[i][j] < min)
min = k[i][j];
}
} return min;
}
public static double[] average(int k[][]) { double[] score = new double[5]; double[] average = new double[5]; for (int i = 0; i < 5; i++) { for (int j = 0; j < 10; j++) { score[i] += k[i][j];
} }
for (int i = 0; i < 5; i++) { score[i] = score[i] - Max(k);
score[i] = score[i] - Min(k);
average[i] = score[i] / 8;
}
return average;
}
}

(2)结果

《Java技术》第一次作业

(3)思路

先写出5位选手得分情况,用二维数组来做,从十个分数中选出最大值和最小值,将总得分减去最大值和最小值,然后除以8,得出平均值,然后按照从高到低排列

(4)

问题 没有定义Arrays,程序一直显示出错,在总成绩案从高到低排列时弄成了从低到高排列;

解决方案 从网上搜索,询问同学

(三)代码托管

码云提交历史截图

《Java技术》第一次作业