学号 20175223 《Java程序设计》第 7 周学习总结
目录
目录
SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。教材学习内容总结
- 第十章要点:
- 要点1:String 类;
- 要点2:StringTokenizer 类;
- 要点3:Scanner 类;
- 要点4:StringBuffer 类;
- 要点5:Data 类与 Calendar 类;
- 要点6:Math 类、BigInteger 类和 Random 类;
- 要点7:日期的格式化、数字的格式化;
- 要点8:Class 类与 Console 类。
- 要点9:Pattern 类与 Matcher 类。
代码调试中的问题和解决过程
1. 编译运行时,跳过 Scanner.nextLine() 语句。
编译如下代码
import java.util.Scanner;
public class test {
public static void main(String args[]) {
System.out.println("Input the number of books:");
Scanner reader = new Scanner(System.in);
int num = reader.nextInt();
System.out.println("Input the book "+num+" information of bookName");
String name = reader.nextLine();
System.out.println("Input the book "+num+" information of bookWriter");
String writer = reader.nextLine();
}
}
结果如下:
Input the number of books:
1
Input the book 1 information of bookName
Input the book 1 information of bookWriter
- 问题1解决方案:
原因是:nextInt()结束的回车字符被nextLine()读取了,导致接下来的 name 无法录入,跳过一行,但下面的 writer 却能正常读写。
在第 5 行和第 6 行代码之间添加一条语句,即可正常读写:
reader.nextLine();
作用:吸收上个输入最后的回车字符(如下一个问题的 6、7 行代码)。
2. 费马素性检验程序:如何使用随机数。
- 问题2解决方案:
两种方法:
(1) 使用 import java.util.Random;
import java.util.Random;
......
int Num=new Random().nextInt(<最大值>)+<最小值>;
(2) 使用 Math.random()
int number = (int)(1+Math.random()*(max-min+1));
代码如下:
import java.util.Scanner;
public class Master {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.println("输入奇整数 n(n>=3):");
int number = reader.nextInt();
System.out.println("输入安全参数 t:");
int t = reader.nextInt();
int max=number-1;
int min=2;
int r=0;
for (int j=0,count=0; count<t; j++,count++) {
int b = (int)(1+Math.random()*(max-min+1));
System.out.println("bbbbbbbbb:"+b);
int moddle = b;
for (int i=0; i<number-2; i++) {
moddle = moddle*b;
r = moddle%number;
}
System.out.println("mmmm:"+moddle);
System.out.println("rrrrrr:"+r);
if (r!=1) {
System.out.println("n为合数。");
j=-2;
return;
}
else if (r==1) {
System.out.println("n为素数。");
}
else {
;
}
}
}
}
[代码托管]
- 代码提交过程截图:
- 代码量截图:
学习进度条
代码行数(新增/累积) | 博客量(新增/累积) | 学习时间(新增/累积) | 重要成长 | |
---|---|---|---|---|
目标 | 5000行 | 30篇 | 400小时 | |
第一周 | 322/322 | 1/1 | 23/23 | |
第二周 | 520/842 | 3/4 | 25/48 | |
第三周 | 458/1300 | 2/6 | 16/64 | |
第三周 | 914/2214 | 2/8 | 21/85 | |
第四周 | 685/2899 | 1/9 | 18/103 | |
第五周 | 663/3562 | 2/11 | 20/103 | |
第六周 | 746/3562 | 1/12 | 16/103 |
计划学习时间:20小时
实际学习时间:16小时
参考资料

更多精彩