JAVA 基础学习记录

Java 基础

1 显示时间(DisplayTime)

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
28
29
30
31
32
33
34
35

import java.util.Scanner;



public class DisplayTime {



public static void main(String[] args) {

try (Scanner in = new Scanner(System.in)) {

int seconds = in.nextInt();

int hour = seconds/3600;

int leftSeconds = seconds - 3600*hour;

int min = leftSeconds/60;

int sec = leftSeconds%60;

System.out.printf("%02d:%02d:%02d",hour,min,sec);

}

}

}

//hour*100/100+":"+min*100/100+":"+sec*100/100



2 华氏温度转摄氏温度(FahrenheitToCelsius)

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
28
29
30
31

import java.util.Scanner;



public class FahrenheitToCelsius {



public static void main(String[] args) {

try (// TODO Auto-generated method stub

Scanner input = new Scanner(System.in)) {

System.out.printf("请输入华氏温度\n");

double fahrenheit = input.nextDouble();

double celsius = (fahrenheit - 32)*(5.0/9);

System.out.printf("摄氏温度为:%.2f\n",celsius);

}

}



}

3 分段计算水费(Water)

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
28
29
30
31
32
33
34
35
36
37
38
39

import java.util.Scanner;



public class Water {



public static void main(String[] args) {

try (// TODO Auto-generated method stub

Scanner inpute = new Scanner(System.in)) {

System.out.println("请输入x的值:");

double x = inpute.nextDouble();

if(x <= 15) {

System.out.printf("%.2f\n",4.0*x/3);

}else {

System.out.printf("%.2f\n",2.5*x-10.5);

}

}



}



}

4 冒泡排序(BubbleSort)

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59

package homework;



import java.util.Scanner;



public class BubbleSort {



public static void main(String[] args) {

int []a = new int [10];

System.out.println("请输入10个无序数");

try (Scanner input = new Scanner (System.in)) {

for(int i=0;i<a.length;i++) {

int value = input.nextInt();

a[i] = value;

}

}

for(int i=0;i<a.length;i++) {

for(int j=i+1;j<a.length;j++) {

if(a[i]>a[j]) {

int temp = a[i];

a[i]=a[j];

a[j]=temp;

}

}

}

for(int i=0;i<a.length;i++) {

System.out.printf(a[i] + " ");

}

}

}

5 求两个数的最大公约数(CommonDivisor)

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

package homework;



import java.util.Scanner;



public class CommonDivisor {



public static void main(String[] args) {

System.out.println("请输入两个数:");

try (Scanner input = new Scanner (System.in)) {

int n1=input.nextInt();

int n2=input.nextInt();

System.out.println(commondivisor(n1,n2));

}

}

public static int commondivisor(int n1,int n2) {

int temp=0;

for(int i=1;i<=n1 && i<=n2;i++) {

if(n1%i == 0 && n2%i ==0) {

temp=i;

}

}

return temp;

}

}

6 生成 0~99 随机数(RandomDemo)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

package homework;

import java.util.Random;

public class RandomDemo {



public static void main(String[] args) {

Random r = new Random();

int number = r.nextInt(100);

System.out.println(number);

}



}

7 判断回文串(CheckPalindrome)

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65

package homework;



import java.util.Scanner;



public class CheckPalindrome {



public static void main(String[] args) {

System.out.println("请输入一个字符串:");

try (Scanner input = new Scanner(System.in)) {

String s0 = input.next();

if(s0.length() % 2 == 0) {

StringBuilder s1 = new StringBuilder(s0.substring(0, s0.length()/2));

StringBuilder s2 = new StringBuilder(s0.substring(s0.length()/2, s0.length()));

s2.reverse();

if(String.valueOf(s1).equals(String.valueOf(s2))) {

System.out.println("这是一个回文串");

}else {

System.out.println("这不是一个回文串");

}

}else {

StringBuilder s1 = new StringBuilder(s0.substring(0, s0.length()/2));

StringBuilder s2 = new StringBuilder(s0.substring(s0.length()/2 + 1, s0.length()));

s2.reverse();

if(String.valueOf(s1).equals(String.valueOf(s2))) {

System.out.println("这是一个回文串");

}else {

System.out.println("这不是一个回文串");

}

}

}

}

}

8 记事本(NoteBook)

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86

package notebook;



import java.util.ArrayList;



public class NoteBook {



private ArrayList<String> notes = new ArrayList<String>();

public void add(String s) {

notes.add(s);

}



public int getSize() {

return notes.size();

}



public String getNote(int index) {

return notes.get(index);

}



public void removeNote(int index) {

notes.remove(index);

}



public String[] list() {

String[] a = new String[notes.size()];

notes.toArray(a);

return a;

}



public static void main(String[] args) {

NoteBook notebook = new NoteBook();

notebook.add("NO.1");

System.out.println(notebook.getNote(0));

notebook.add("No.2");

System.out.println(notebook.getNote(1));

notebook.removeNote(1);

System.out.println(notebook.getSize());

String[] a = notebook.list();

for(int i=0; i<notebook.getSize(); i++) {

System.out.println(a[i]);

}

}

}