笔记汇总:《Java面向对象程序设计》学习笔记
这些题其实都非常滴简单,相信大伙能够立刻就秒了吧
import java.util.Scanner;
public class Blank1 {
public static void main(String[] args) {
Scanner sc = new Scanner(____(1)____);
int count = 0, t;
int x = sc.nextInt();
sc.close();
t = x;
while (t != 0) {
count++;
____(2)____;
}
System.out.println(x + "是" + count + "位数");
}
}
答案:
(1) System.in
(2) t = t / 10
class AddOver {
public ____(3)____ {
return a + b;
}
public int add(int a, int b, int c) {
return a + b + c;
}
}
class Blank2 {
public static void main(String[] args) {
AddOver a = ____(4)____;
System.out.println(a.add(1, 2));
System.out.println(a.add(1, 2, 3));
}
}
答案:
(3) int add(int a, int b)
(4) new AddOver()
class Point {
int x, y;
public ____(5)____(int x, int y) {
this.x = x;
this.y = y;
}
public Point getPoint() {
Point temp = new Point(0, 0);
temp.x = x;
temp.y = y;
return ____(6)____;
}
public void setPoint(____(7)____) {
this.x = s.x;
this.y = s.y;
}
}
public class Blank3 {
public static void main(String[] args) {
Point a = new Point(3, 4);
Point b = new Point(0, 0);
b = a.getPoint();
Point c = new Point(0, 0);
c.setPoint(b);
}
}
答案:
(5) Point
(6) temp
(7) Point s
public class Blank4 {
public static void main(String[] args) {
String fileName = "D:/Hello.txt", line;
try {
BufferedReader in = new BufferedReader(____(8)____);
line = in.readLine();
while (____(9)____) {
System.out.println(line);
line = ____(10)____;
}
in.close();
} catch (Exception e) {
System.out.println("Problem reading " + fileName);
}
}
}
答案:
(8) new FileReader(fileName)
(9) line != null
(10) in.readLine()
public class Ex1 {
public ____(11)____ String getXh() {
String[] xhs = {"1","2","3","4","5","6","7"};
int index = ____(12)____; // 生成 0 ~ 6 之间的随机数
return xhs[index];
}
public static void main(String[] args) {
System.out.println("随机抽取的学号为: " + ____(13)____);
}
}
答案:
(11) static
(12) (int)(Math.random()*7)
(13) getXh()
import java.util.Date;
class TimeThread implements ____(14)____ {
public void run() {
while (true) {
Date currentTime = new Date();
try {
____(15)____; // 休眠1秒钟
} catch (Exception e) {
System.out.println(e.toString());
}
System.out.println(Thread.currentThread().getName() + ":" + currentTime);
}
}
}
public class Ex2 {
public static void main(String[] args) {
String[] names = { "first", "second", "third" };
TimeThread myThread = new TimeThread();
for (int i = 0; i < 3; i++) {
Thread thread = new Thread(myThread, names[i]);
____(16)____; // 启动线程
}
}
}
答案:
(14) Runnable
(15) Thread.sleep(1000)
(16) thread.start()
public class Ex3 {
public static void main(String[] args) {
int a, b, c;
for (a = 0; ____(17)____; a++) {
for (b = 0; ____(18)____; b++) {
c = 100 - a - b;
if ((3 * a + 5 * b + c / 3 == 100) && (____(19)____)) {
System.out.println("公鸡:" + a + " 母鸡:" + b + " 小鸡:" + c);
}
}
}
}
}
答案:
(17) a <= 33
(18) b <= 20
(19) c % 3 == 0
public class Ex4 {
public static void main(String[] args) throws IOException {
long t = System.currentTimeMillis();
BufferedWriter fw = new BufferedWriter(____(20)____);
for (int i = 1; i <= 100000; i++) {
____(21)____(i + "\n");
}
fw.close();
t = System.currentTimeMillis() - t;
System.out.println("Time elapsed: " + t + "ms");
}
}
答案:
(20) new FileWriter("D:\\Hello.txt")
(21) fw.write
public class StringExample {
public static void main(String[] args) {
String s1 = new String("2012");
String s2 = new String("100.50");
int x = ____(21)____; // 将 s1 转换为 int 类型
double y = ____(22)____; // 将 s2 转换为 double 类型
double z = x + y;
String s3 = ____(23)____; // 将 z 转换为字符串
StringBuffer sbr = new StringBuffer("Thingking");
String s4 = new String("in Java");
____(24)____; // 将 s4 连接在 sbr 的后面
System.out.println(sbr.toString()); // 显示为 Thingking in Java
}
}
答案:
(22) Integer.parseInt(s1)
(23) Double.parseDouble(s2)
(24) String.valueOf(z) 或 z + ""
(25) sbr.append(s4)
public class ArraySort {
public static void main(String[] args) {
int[] a = new int[] { 21, 34, 211, 15, 92, 68, 89, 794, 11, 863 };
int temp;
for (int i = 0; i < 10; i++) {
for (int j = 0; j < ____(26)____; j++) {
if(a[j] > a[j + 1]) {
temp = a[j];
____(27)____;
____(28)____;
}
}
}
for (int i = 0; i < a.length; i++) {
System.out.println(a[i] + " ");
}
}
}
答案:
(26) a.length - 1 - i 或 9 - i
(27) a[j] = a[j + 1]
(28) a[j + 1] = temp
public class TGS {
public static void main(String[] args) {
for (int i = 10; i <= 999; i++) {
if (____(29)____ || ____(30)____) {
System.out.println(i);
}
}
}
}
答案:
(29) i * i % 100 == i
(30) i * i % 1000 == i
public class Exam1 {
public static void main(String[] args) {
____(31)____; // 定义整型变量 sum
for (int i = 2; i <=100;) {
sum += i;
____(32)____;
}
System.out.println("1-100 之间偶数的和是:" + sum);
}
}
答案:
(31) int sum = 0
(32) i = i + 2 或 i += 2
public class Exam2 {
static void factorial(int n) {
long m = 1;
for (int x = 1; x <= n; ____(33)____) {
____(34)____;
System.out.println(x + "!=" + m);
}
}
public static void main(String[] args) {
factorial(9);
}
}
答案:
(33) x++
(34) m = m * x 或 m *= x
class PrintThread extends ____(35)____ {
public PrintThread(String str) {
____(36)____; // 调用父类的构造方法
}
public void run() {
for (int i = 1; i <= 100; i++) {
if (i % 3 == 0) {
System.out.println(this.getName() + ": " + i);
}
try {
____(37)____; // 休眠 1500 毫秒
} catch (Exception e) {
System.out.println(e.toString());
}
}
}
}
public class Exam5 {
public static void main(String[] args) {
PrintThread myThread = new PrintThread("PrintThread");
____(38)____; // 启动线程
}
}
答案:
(35) Thread
(36) super(str)
(37) sleep(1500)
(38) myThread.start()
public class Exam4 {
public static void main(String[] args) {
int[] dpm = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int month = 5;
int day = 1;
for (int i = 0; ____(39)____; i++) {
day = day + dpm[i];
}
day = day % 5;
if (____(40)____) {
System.out.println("1"); // 表示打鱼
} else {
System.out.println("0"); // 表示晒网
}
}
}
答案:
(39) i < month 或 i < 5
(40) day > 0 && day <= 3
public class JModify {
public static void f(int n) {
int i = 2;
while (n > 1) {
____(41)____ {
System.out.println(i);
n /= i;
} else {
____(42)____;
}
}
}
public static void main(String[] args) {
int n = 100;
f(n);
}
}
答案:
(41) if(n % i == 0)
(42) i++
public class RandomTel {
public ____(43)____ String getTel() {
String[] tels = {"138****8080","189****6666","133****1234","139****9999",};
int index = ____(44)____; // 用Math类中的方法生成0 ~ 3 之间的随机数
return tels[index];
}
public static void main(String[] args) {
System.out.println("随机幸运手机号为:" + ____(45)____);
}
}
答案:
(43) static
(44) (int)(Math.random()*4)
(45) getTel()
public class HotelDoor {
public static void main(String[] args) {
boolean[] a = new boolean[101];
final int N = 101;
int i,j;
for (i = 1; i < N; i++) {
____(46)____; // 第 1 个服务员将所有房间设置为打开状态
}
for (i = 2; i < N; i++) {
for (____(47)____; j < N; j++) {
if(j % i == 0) {
____(48)____; // 执行相反处理
}
}
}
for (i = 1; i < N; i++) {
if(a[i] == true) {
System.out.println(i + " "); // 显示打开状态的房间编号
}
}
}
}
答案:
(46) a[i] = true
(47) j = i
(48) a[j] = !a[j]
import java.util.Scanner;
public class PrimeExam {
public static void main(String[] args) {
Scanner sc = new Scanner(____(49)____);
int flag = 0;
int x = sc.____(50)____;
int y = (int)Math.sqrt(x);
for (int i = 2; i <= y; i++) {
if(____(51)____) {
System.out.println("不是素数");
flag = 1;
break;
}
}
if (____(52)____) {
System.out.println("是素数");
}
}
}
答案:
(49) System.in
(50) nextInt()
(51) x % i == 0
(52) flag == 0