1. 程式人生 > 其它 >Java作業基礎知識梳理--2020-12-03

Java作業基礎知識梳理--2020-12-03

Java中常用的賦值運算子
+= 將該運算子左邊的數值加上右邊的數值, 其結果賦值給左邊變數本身
-= 將該運算子左邊的數值減去右邊的數值, 其結果賦值給左邊變數本身
*= 將該運算子左邊的數值乘以右邊的數值, 其結果賦值給左邊變數本身
/= 將該運算子左邊的數值整除右邊的數值, 其結果賦值給左邊變數本身
%= 將該運算子左邊的數值除以右邊的數值後取餘,其結果賦值給左邊變數本身

Java註釋:
分為單行註釋,多行註釋(塊註釋),文件註釋
註釋可以提高程式碼可讀性,方便後期程式碼維護,
方便程式設計師間的交流溝通生成幫助文件。
同時註釋不能長篇大論,也不能過於簡單。

switch語句
switch中的值只能是整數、列舉、字元、字串

不可使用long、float、double、boolean作為它的資料型別

交換三個值(程式碼):

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int a = scan.nextInt();
int b = scan.nextInt();
int tmp = a;
a = b;
b = tmp;
System.out.println("a = " + a);
System.out.println("b = " + b);

}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
求三個數中的最值(程式碼):

public static void main(String[] args) {
int a = 4;
int b = 2;
int c = 3;
int max = 0;
int min = 0;
if (a > b && a > c){
max = a;
}
else if (b > a && b > c){
max = b;
}
else
max = c;
if (a < b && a < c) {
min = a;

}
else if (b < a && b < c) {
min = b;
}
else
min = c;
System.out.println("max = " + max);
System.out.println(“min = " + min);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
編寫程式數一下 1到 100 的所有整數中出現多少個數字9
public static void main(String[] args) {
int count = 0;
for (int i = 1; i < 100; i++) {
if(i % 10 == 9) {
count++;
}else if(i / 10 == 9) {
count++;
}
}
System.out.println(count);
}
1
2
3
4
5
6
7
8
9
10
11
輸出 1000 - 2000 之間所有的閏年
public static void main(String[] args) {
for(int year = 1000;year <= 2000;year++){
if((year % 4 == 0 && year % 100 != 0) || year % 400 0){
System.out.println(year);
}
}
}
1
2
3
4
5
6
7
列印 1 - 100 之間所有的素數
public static void main(String[] args) {
int count = 0;
for (int i = 2; i < 101; i++) {
for (int j = 2; j <= i; j++) {
if (i % j == 0 && i != j)
break;
if (j == i) {
if (count % 5 == 0)
System.out.println();
System.out.print(j + " “);
count++;
}
}
}
System.out.println(”\n素數有" + count + “個”);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
給定一個數字,判定一個數字是否是素數
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int num = scanner.nextInt();
for (int j = 0; j <= num; j++) {
int i = 2;
for (; i <= Math.sqrt(j); i++) {
if (num % i == 0) {
System.out.println(“不是素數”);
break;
}
}
if (i > Math.sqrt(num)) {
System.out.println(“是素數”);
break;
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
根據輸入的年齡, 來打印出當前年齡的人是少年(低於18), 青年(19-28), 中年(29-55), 老年(56以上)(這道題好像有瑕疵,沒得滿分,有緣人幫忙看看)
public static void main(String[] args) {
while(true){
System.out.println(“請輸入要判斷的年齡”);
Scanner in=new Scanner(System.in);
int age=in.nextInt();
if(age<0){
System.out.println(“輸入有誤,重新輸入”);
continue;
}
if(age>=0&&age<=18){
System.out.println(“少年”);
break;
}
if(age>=19&&age<=28){
System.out.println(“少年”);
break;
}
if(age>=29&&age<=55){
System.out.println(“中年”);
break;
}
else{
System.out.println(“老年”);
break;
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
列印 X 圖形
public static void printX(int m) {
for (int x = 1; x <= m; x++) {
for (int y = 1; y <= m; y++) {
if (x == y || x + y == m + 1) {
System.out.print("*");
} else {
System.out.print(" “);
}
}
System.out.println();
}
}
public static void main(String[] args) {
printX(10);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
完成猜數字遊戲 ,使用者輸入數字,判斷該數字是大於,小於,還是等於隨機生成的數字,等於的時候退出程式。
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Random random = new Random();
int ranNum = random.nextInt(100) + 1;
while (true){
int num = scanner.nextInt();
if (num < ranNum) {
System.out.println(“小”);
}else if (num > ranNum) {
System.out.println(“大”);
}else{
System.out.println(”
”);
break;
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
求出0~999之間的所有“水仙花數”並輸出。(“水仙花數”是指一個三位數,其各位數字的立方和確好等於該數本 身,如;153=1+5+3?,則153是一個“水仙花數“。)
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
for (int i = 1; i <= n ; i++) {
int count = 0;
int tmp = i ;
while (tmp != 0) {
count++;
tmp = tmp/10;
}
tmp = i;
int sum = 0;
while (tmp != 0) {
sum += Math.pow(tmp%10,count);
tmp = tmp/10;
}
if(sum == i) {
System.out.println(i);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
計算1/1-1/2+1/3-1/4+1/5 …… + 1/99 - 1/100 的值 。
public class Main {
public static double func(int i,int j) {
double ret = 1.0/i - 1.0/j;
return ret;
}
public static void main(String[] args) {
double sum = 0.0;
for (int i = 1; i < 100 ; i+=2) {
sum = sum+ func(i,i+1);
}
System.out.println(sum);
}
1
2
3
4
5
6
7
8
9
10
11
12
求兩個正整數的最大公約數
public static void main(String[] args) {
int a = 9;
int b = 18;
int c = 0;
while (a % b != 0) {
c = a %b;
a = b;
b = c;
}
System.out.println©;
}

1
2
3
4
5
6
7
8
9
10
11
12
求一個整數,在記憶體當中儲存時,二進位制1的個數。
public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);
int num = scanner.nextInt();
int count = 0;
for(int i = 0;i < 32;i++) {
    if(((num >> i) & 1) == 1) {
        count++;
    }
}
System.out.println(count);

}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
獲取一個數二進位制序列中所有的偶數位和奇數位, 分別輸出二進位制序列
public static void main(String[] args) {
int num = 14;
odd(num);
System.out.println();
even(num);
}
public static void odd(int num) {
for (int i = 30; i >= 0; i = i - 2) {
if ((num & (1 << i)) != 0) {
System.out.print("1 ");
} else {
System.out.print("0 ");
}
}
}
public static void even(int num) {
for (int i = 31; i >= 0; i = i - 2) {
if ((num & (1 << i)) != 0) {
System.out.print("1 ");
} else {
System.out.print(“0 “);
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
編寫程式碼模擬三次密碼輸入的場景。 最多能輸入三次密碼,密碼正確,提示“登入成功”,密碼錯誤, 可以重新輸 入,最多輸入三次。三次均錯,則提示退出程式
public static void main(String[] args) {
int count = 3;
Scanner scanner = new Scanner(System.in);
while (count != 0) {
String passWorld = scanner.nextLine();
if(passWorld.equals(“123456”)) {
System.out.println(“登入成功!”);
}else {
count–;
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
輸出一個整數的每一位,如:123的每一位是1 , 2 , 3
public static void print (int n) {
if(n < 0) {//變成整數。
System.out.println(”-”);
n = -n;
}
if(n>9) {
print(n/10);//遞迴
}
System.out.println(n%10);//列印個位數
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
print(n);
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
輸出nn的乘法口訣表,n由使用者輸入。
public static void main(String[] args) {
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i ; j++) {
System.out.print(i+"
"+j + “=” + i*j +" ");
}
System.out.println();
}
}
1
2
3
4
5
6
7
8
over!

我像個憨批研究了半天為什麼我的表情包下面有水印…絕了(過了考試周智商就沒有了)

在這裡插入圖片描述

在這裡插入圖片描述