(一)学习总结
1. 在 java 中通过 Scanner 类完成控制台的输入, 查阅 JDK 帮助文档, Scanner 类实现基本数据输入的方法是什么?
从键盘输入需要用 Scanner 类, 需要导入头文件 import.java.util.Scanner; nextInt(): 读取整形数据 nextDouble(): 读取双精度数据
next{"\\d{4}-\\d{2}-d{2}$"} 在 Scanner 类中没有专门的日期格式输入操作, 需要自己编写验证, 手工转换
next(): 读取输入的下一个单词 nextLine(): 读取输入的下一行内容 构造 Scanner 对象, 并与 System.in 关联
import java.util.Scanner; public class ScannerDemo01{ public static void main(String[] args){ Scanner sca = new Scanner(System.in); System.out.println("请输入数据:"); String str =sca.next(); System.out.println("输入的数据为:"+str); } } 运行结果为:
2.Random 类和 Math 类的 random()方法都能产生随机数, 这两种方式有什么区别, 各有什么特点呢? 查阅 JDK 帮助文档, 并举例加以说明
Random 类的随机方法是设定随机种子的, 如: new Random().nextInt(10), 这里的 10 就是随机种子, 根据随机数的原理, 相同随机种子在相同的随机次数中产生的随机数是一样的 Math 类的 radom 方法返回的是 0~1 之间的小数, 如果想得到 1~100 之间的数则需要扩大 100 倍, 但是仍然是小数, 可以加 (int) 强制转换
import java.util.Random; public class RandomDemo{ public static void main(String args[]){ Random r = new Random(); int x=r.nextInt(100); \\ 产生 0~100 的随机整数 System.out.println(); } } import java.util.Random; public class Randoma{ public static void main(String args[]){ double a = Math.random()*100; \\ 产生 1~100 的随机小数 int b = (int)(Math.random()*100); \\ 产生 1~100 的随机整数 int c = 10+(int)(Math.random()*100); \\ 产生一个 10 到 10+100 之间的整数 System.out.println(a); System.out.println(b); System.out.println(c); } } 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"); } } } 运行结果:
若:
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"); } System.out.println(a+b+c); } } 则运行结果为:
产生误差, 所以不相等
4. 本次学习要点中其他需要总结的内容
Math 类是数学操作类, 提供的一系列数学操作方法, 包括求绝对值, 三角函数等, 在 Math 类中, 提供的一切方法都是静态方法, 所以直接由类名称调用, Math 类中也有 radom 方法, 只是不大写, 返回的是 01 之间的随机小数, 不同于 Radom 方法
(二)实验总结
1. 看商品猜价格
程序设计思路: 1. 首先要先产生一个随机数, 作为产品的价格 2. 进入循环, 从键盘输入猜的价格, 如果猜对了, 跳出循环, 如果猜错了, 提示猜大了还是猜小了, 如果猜的次数超过十次, 则结束
问题 1: 运行进入死循环 原因: 没有跳出循环语句 解决方案: 加上 break
程序:
- import java.util.Scanner; import java.util.Random; public class Guesstheprice {
- public static void main(String[] args) { // TODO Auto-generated method stub Scanner scan = new Scanner(System.in); int lage =0; int cishu = 0; int suiji = new Random().nextInt(100); for(int i=0;i<10;i++){ int price=scan.nextInt(); if(price<suiji){ System.out.println("您猜小了"); cishu++; } if(price>suiji){ System.out.println("您猜大了"); cishu++; } if(price==suiji){ System.out.println("恭喜您, 猜对了"); cishu++; lage++; System.out.println("商品的价格为"+suiji); System.out.println("您一共猜了"+cishu+"次"); break; } } if(lage==0){ System.out.println("您已经猜了十次, 商品的价格为"+suiji); } }
} 扩展: 程序设计思路: 1. 首先要先产生一个随机数, 作为产品的价格 2. 进入循环, 输入猜的价格, 第一次才对分数为 100, 两次才对分数为 80, 以此类推若五次都没有猜中或者五次之内猜中, 则给出价格和分数 3. 一次游戏循环完毕后, 则选择是否继续进行下一轮游戏 问题 1: 一直卡在 选择是否继续进行下一轮游戏 不会比较字符串
原因: 基础知识还不够牢固 解决方案: 查阅资料, 问同学, 解决了这个问题 程序:
import java.util.Scanner; import java.util.Random; public class Caijiage { public static void main(String[] args) { // TODO Auto-generated method stub Scanner scan = new Scanner(System.in); Scanner str2 = new Scanner(System.in); String str = new String("yes"); String str1 = new String("yes"); int lage =0; int grade = 0; int suiji = new Random().nextInt(100); while (str.equals(str1)){ for(int i=1;i<=5;i++) { int price=scan.nextInt(); if(price<suiji){ System.out.println("您猜小了"); System.out.println("您还有"+(5-i)+"次猜数机会"+"\n"); grade = (5-(i-1))*20; } if(price>suiji){ System.out.println("您猜大了"); System.out.println("您还有"+(5-i)+"次猜数机会"+"\n"); grade = (5-(i-1))*20; } if(price==suiji){ System.out.println("恭喜您, 猜对了"); lage++; grade = (5-(i-1))*20; System.out.println("商品的价格为"+suiji); System.out.println("您一共猜了"+i+"次"); System.out.println("您的得分为:"+grade); break; } if(i==5){ System.out.println("您已经猜了五次, 商品的价格为"+suiji); System.out.println("您的得分为: 0"); } } System.out.println("是否继续游戏? 是请输入 yes 否则输入 no"); str1 = str2.next(); } } } 2. 万年历
程序设计思路: 先判断是闰年还是平年, 再计算一共有多少天, 判断每个月有多少天, 计算出从 1900 一月一日到今天有多少天, 再判断这个月是从周几开始的 问题 1: 每个方法在哪使用搞不清楚, 还有一个月从周几开始的有点蒙圈 原因: 思路不清晰
代码:
- import java.util.Scanner;
- public class Wannianli { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("请输入从 1900 年到现在的一个年份"); Scanner scan = new Scanner(System.in); int year = scan.nextInt(); System.out.println("请输入 1-12 月的一个月份"); int month = scan.nextInt(); System.out.println("星期日 \ t 星期一 \ t 星期二 \ t 星期三 \ t 星期四 \ t 星期五 \ t 星期六"); 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 day2 = 0; if (month == 2) { if (isLeap(year)) { day2 = 29; } else { day2 = 28; } } else { switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: day2 = 31; break; case 4: case 6: case 9: case 11: day2 = 30;
- } } return day2; }
- public static int totalDays(int year, int month) { // 计算某年某月之前距离 1900 年 1 月 1 日的总天数 int m, all = 0, day2; for (int i = 1900; i <year; i++) { if (isLeap(year)) { all += 366; } else { all += 356; } } for (m = 1; m < month; m++) { day2 = days(year, month); all = all + day2; } return all; }
- public static void printCalender(int year, int month) { // 输出某年某月日历的方法 int j = 0; int s = totalDays(year, month); int n = (s % 7) + 1; for (int i = 1; i <= n; i++) { System.out.println("\t"); j++; } for (int k = 1; k <= days(year, month); k++) { System.out.println(k + "\t"); j++; if (j % 7 == 0) { System.out.println("\n"); } } } (三)代码托管
- package ww;
- import java.util.Scanner;
- public class w {
- private static int grades[];
- public static void main(String[] args) {
- grades = new int [5];
- for(int x=0;x<5;x++) {
- double[] scores = new double[10];
- Scanner input = new Scanner(System.in);// 扫描器用于读取控制台输入
- for (int i = 0; i < scores.length; i++) {// 输入分数
- System.out.print("请输入第" + (i + 1) + "位评委的分数, 满分十 10 分");
- String temp = input.nextLine().trim();
- scores[i] = Double.parseDouble(temp);
- }
- getScore(scores);// 对分数进行处理的函数
- }
- }
- private static void getScore(double[] scores) {
- // 对分数进行排序
- double temp;
- for (int i = 0; i < scores.length - 1; i++) {
- for (int j = i + 1; j < scores.length; j++) {
- if (scores[i]> scores[j]) {
- temp = scores[i];
- scores[i] = scores[j];
- scores[j] = temp;
- }
- }
- }
- // 去掉一个高分, 一个低分后的平均分
- double sum =0;
- for (int i = 1; i < scores.length-1; i++) {
- sum+=scores[i];
- }
- double pjf = sum/(scores.length-2);
- System.out.println("去掉一个最高分:"+scores[scores.length-1]);
- System.out.println("去掉一个最低分:"+scores[0]);
- System.out.println("最后的平均分是:"+String.format("%.2f",pjf));// 平均分保留 2 位小数
- }
- }
来源: http://www.bubuko.com/infodetail-2537915.html