(一)学习总结
1.在java中通过Scanner类完成控制台的输入,查阅JDK帮助文档,Scanner类实现基本数据输入的方法是什么?不能只用文字描述,一定要写代码,通过具体实例加以说明。
import java.util.*;
public class ABC {
public static void main(String[] args) {
int a;
Scanner input = new Scanner(System.in);
a = input.nextInt();
System.out.println(a);
}
这就是输入一个int型的数,如果想要输入一个double型的数就把next后面的int改成double。如果要输入一个字符串,next后面就不用写东西。
2.Random类和Math类的random()方法都能产生随机数,这两种方式有什么区别,各有什么特点呢?查阅JDK帮助文档,并举例加以说明。
Math类:int x=(int)(Math.random()*x+y); 就是产生一个x-y之间的数。
Random类:
Random rand = new Random();
int x=rand.nextInt(x)+y;
就是产生一个x-y之间的数。
3.运行下列程序,结果是什么?查阅资料,分析为什么。
输出结果是不等于0.3,因为在计算double类型数据时会产生误差。
比较两个double类型的数据相等时,看二者相减是否无限趋近于0.
public class ABC {
public static void main(String args[]) {
double a = 0.1;
double b = 0.1;
double c = 0.1;
if((a + b + c)-0.3<1e-6){
System.out.println("等于0.3");
}else {
System.out.println("不等于0.3");
}
}
}
(二)实验总结
实验内容:
1.看商品猜价格
import java.util.*;
public class ABC {
public static void main(String[] args) {
int x = 0, a = 0, i = 0, j = 0, k = 0, n = 0;
String t;
Scanner input = new Scanner(System.in);
ww: while (true) {
x = (int) (Math.random() * 100);
k = 100;
for (i = 5; i > 0; i--) {
System.out.println("请输入商品价格,还有" + i + "次机会");
a = input.nextInt();
if (a == x) {
System.out.println("猜对了");
j += k;
break;
}
if (a > x) {
System.out.println("猜大了");
k -= 20;
}
if (a < x) {
System.out.println("猜小了");
k -= 20;
}
if (i == 1) {
System.out.println("正确答案是" + x);
}
}
n++;
System.out.println("是否进行下一轮?输入yes进行下一轮,输入no退出游戏");
while (true) {
t = input.next();
if (t.equals("yes")) {
System.out.println("进行下一轮");
break;
} else if (t.equals("no")) {
System.out.println("退出游戏");
break ww;
} else {
System.out.println("输入错误,请重新输入");
}
}
}
System.out.println("一共猜了" + n + "次,总得分为" + j);
}
}
2.万年历
import java.util.*;
public class ABC {
public static void main(String[] args) {
int year, year1, month, sum, sum1, sum2, i, n, zong = 0, h = 0;
Scanner input = new Scanner(System.in);
while (true) {
System.out.println("请输入年份");
year = input.nextInt();
System.out.println("请输入月份");
month = input.nextInt();
if (year < 1900 || month < 1 || month > 12) {
System.out.println("输入错误,请重新输入");
} else {
break;
}
}
sum1 = 0;
sum2 = 0;
year1 = year;
for (year1 = year1 - 1; year1 >= 1900; year1--) {
if (year1 % 4 == 0 && year1 % 100 != 0 || year1 % 400 == 0) {
sum1 = sum1 + 366;
} else {
sum1 = sum1 + 365;
}
}
for (i = 1; i < month; i++) {
if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12) {
sum2 = sum2 + 31;
} else if (i == 4 || i == 6 || i == 9 || i == 11) {
sum2 = sum2 + 30;
} else if (i == 2) {
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
sum2 = sum2 + 29;
} else {
sum2 = sum2 + 28;
}
}
}
sum = sum1 + sum2;
System.out.println("星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六");
switch (sum % 7) {
case 0:
System.out.print("\t");
h = 2;
break;
case 1:
System.out.print("\t\t");
h = 3;
break;
case 2:
System.out.print("\t\t\t");
h = 4;
break;
case 3:
System.out.print("\t\t\t\t");
h = 5;
break;
case 4:
System.out.print("\t\t\t\t\t");
h = 6;
break;
case 5:
System.out.print("\t\t\t\t\t\t");
h = 7;
break;
case 6:
h = 1;
break;
}
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
zong = 31;
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
zong = 30;
} else if (month == 2) {
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
zong = 29;
}
else {
zong = 28;
}
}
for (n = 1; n <= zong; n++, h++) {
System.out.print(n + "\t");
if (h % 7 == 0) {
System.out.println(" ");
}
}
}
}
3.评分系统
import java.util.*;
public class ABC {
public static void main(String[] args) {
int a[][] = new int[5][10];
int b[][] = new int[5][8];
int i = 0, j = 0, k = 0, t = 0;
double n, x, y;
double c[] = new double[5];
int d[] = new int[5];
Scanner input = new Scanner(System.in);
for (i = 0; i <= 4; i++) {
for (j = 0; j <= 9; j++) {
System.out.println("请第" + (j + 1) + "位评委给第" + (i + 1) + "号选手打分");
a[i][j] = input.nextInt();
}
}
for (i = 0; i <= 4; i++) {
for (j = 0; j <= 9; j++) {
for (k = 0; k <= 8; k++) {
if (a[i][k] > a[i][k + 1]) {
t = a[i][k];
a[i][k] = a[i][k + 1];
a[i][k + 1] = t;
}
}
}
}
for (i = 0; i <= 4; i++) {
for (j = 0; j <= 7; j++) {
b[i][j] = a[i][j + 1];
}
}
for (i = 0; i <= 4; i++) {
n = 0;
x = 0;
for (j = 0; j <= 7; j++) {
n++;
x += b[i][j];
}
c[i] = x / n;
d[i] = i + 1;
}
for (i = 0; i <= 4; i++) {
for (k = 0; k <= 3; k++) {
if (c[k] < c[k + 1]) {
y = c[k];
c[k] = c[k + 1];
c[k + 1] = y;
t = d[k];
d[k] = d[k + 1];
d[k + 1] = t;
}
}
}
for (i = 0; i <= 4; i++) {
System.out.println("第" + (i + 1) + "名是第" + d[i] + "号选手,得分为" + c[i]);
}
}
}