chapter1
复制代码
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241import java.util.*; public class chapter1 { //希腊字母+折半查找 void a0(){ System.out.println("hi"); char A='你',B='我',C='他'; System.out.println("汉字'你'在unicode表中的位置:"+(int)A); System.out.println("汉字'我'在unicode表中的位置:"+(int)B); System.out.println("汉字'他'在unicode表中的位置:"+(int)C); // int firstLetter, lastLetter; char firstG = 'α', lastG = 'ω'; //获取首字母与末字母的值 firstLetter = (int)firstG; lastLetter = (int)lastG; System.out.println("希腊字母表: "); for(int i = firstLetter; i <= lastLetter; ++i) { char greekLetter; greekLetter = (char)i; System.out.print(" "+greekLetter); } System.out.println(); // int startPosition = 0, endPosition = 0; char cSrart = 'α', cEnd = 'ω'; startPosition = (int) cSrart; endPosition = (int) cEnd; System.out.println("希腊字母表:"); System.out.println("小写:"); for (int i = startPosition; i <= endPosition; i++) { char c = ''; c = (char) i; System.out.print(" " + c); if ((i - startPosition + 1) % 10 == 0) { System.out.println(""); } } System.out.println(); cSrart = 'Α'; cEnd = 'Ω'; startPosition = (int) cSrart; endPosition = (int) cEnd; System.out.println("大写:"); for (int i = startPosition; i <= endPosition; i++) { char c = ''; c = (char) i; System.out.print(" " + c); if ((i - startPosition + 1) % 10 == 0) { System.out.println(""); } } } //折半查找 void a1(){ System.out.println("hi"); char A='你',B='我',C='他'; System.out.println("汉字'你'在unicode表中的位置:"+(int)A); System.out.println("汉字'我'在unicode表中的位置:"+(int)B); System.out.println("汉字'他'在unicode表中的位置:"+(int)C); int firstLetter, lastLetter; char firstG = 'α', lastG = 'ω'; //获取首字母与末字母的值 firstLetter = (int)firstG; lastLetter = (int)lastG; System.out.println("希腊字母表: "); for(int i = firstLetter; i <= lastLetter; ++i) { char greekLetter; greekLetter = (char)i; System.out.print(" "+greekLetter); } System.out.println(); int startPosition = 0, endPosition = 0; char cSrart = 'α', cEnd = 'ω'; startPosition = (int) cSrart; endPosition = (int) cEnd; System.out.println("希腊字母表:"); System.out.println("小写:"); for (int i = startPosition; i <= endPosition; i++) { char c = ''; c = (char) i; System.out.print(" " + c); if ((i - startPosition + 1) % 10 == 0) { System.out.println(""); } } System.out.println(); cSrart = 'Α'; cEnd = 'Ω'; startPosition = (int) cSrart; endPosition = (int) cEnd; System.out.println("大写:"); for (int i = startPosition; i <= endPosition; i++) { char c = ''; c = (char) i; System.out.print(" " + c); if ((i - startPosition + 1) % 10 == 0) { System.out.println(""); } } //折半查找 System.out.println(); int start=0,end,middle; int a[]={12,45,67,89,123,-45,67}; int N=a.length; //长度7 for (int i=0;i<N;i++) { //选择法排序数组、从小到大排序 for (int j=i+1;j<N;j++) { if (a[j]<a[i]){ int t=a[j]; a[j]=a[i]; a[i]=t; } } } System.out.println(a[1]); //12 Scanner scanner=new Scanner(System.in); System.out.println("输入整数,程序判断该整数是否在数组中:"); int number= scanner.nextInt(); //要找的数 int count=0; end=N; //数组长度7、涉及0/start和a.length/end需注意 middle=(start+end)/2; //折半3.5 while(number!=a[middle]){ //number不等于中间数 if (number>a[middle]){ // number大于中间数 start=middle; //在后半段进行折半,start变大 } else if(number<a[middle]){ // number小于中间数 end=middle; //在前半段进行折半,end变小 } middle=(start+end)/2; //更新比较数据域 count++; //比较一次加一次,折半一次 if(count>N/2){ break; //注意是count和N/2的关系来判断是否跳出循环 } } if(count>N/2){ System.out.printf("%d不在数组中n",number); }else{ System.out.println(number+"是数组中的第"+(middle+1)+"个元素"); } System.out.println((char)20302); } //1!+2!+...+10! void a2(){ int sum = 0; for (int i = 1; i <= 10; i++) { int a = 1; for (int j = 1; j<= i; j++) { a *= j; //a=a*j; } sum += a; //sum=sum+a; } System.out.println("1!+2!+...+10!= " + sum); } //求100以内的全部素数 void a3(){ int i,j; System.out.println("100以内的全部素数: "); for (i = 2; i <= 100; i++) { for (j = 2; j <= i / 2; j++) { if (i % j == 0){ break; } } if (j > i / 2) { System.out.print(" " + i); } } System.out.println(); } //分别用do-while和for循环计算1+1/2!+1/3!+1/4!+···的前20项和 void a4(){ int i = 1, j = 1; double sum = 0; do { i *= j; sum += 1.0 / i; j ++; }while (j <= 20); System.out.println("do-while循环"); System.out.println("1 + 1/2! + 1/3! + 1/4! + ···+的前20项和 = " + sum); System.out.println(); } void a5(){ double sum = 0; int i = 1; for (int j = 1; j <= 20; j++) { i *= j; sum += 1.0 / i; } System.out.println("for循环"); System.out.println("1 + 1/2! + 1/3! + 1/4! + ···+的前20项和 = " + sum); System.out.println(); } //一个数如果恰好等于它的因子之和,这个数就称为完数,求1000之内所以的完数 void a6(){ int i, j,sum;//i表示要判断的数,j表示因子,sum表示因子之和 System.out.print("1000之内的所有完数:"); /*判断i能否被j整除,能的话j即为因子 因子不包括自身*/ for (i = 2; i <= 1000; i++) { sum = 1; for (j = 2; j <= i / 2; j++) { if (i % j == 0) { sum += j; } } if (sum == i) { System.out.print(" " + i); } } System.out.println(); } //使用for循环语句计算8+88+888+···前10项之和 void a7(){ long t = 8; long sum = 8; for (int i = 1; i < 10; i++) { t = t * 10 + 8; sum += t; } System.out.println("8+88+888+···前10项之和= " + sum); System.out.println(); } //输出满足1+2+3+···+n<8888的最大正整数n void a8(){ int sum = 0; int i = 1; while ((sum + i)< 8888) { sum += i; i++; } System.out.println("满足1+2+3+···+n<8888的最大正整数n = " + (i - 1)); System.out.println(); } public static void main(String[] args){ chapter1 demo=new chapter1(); demo.a0(); System.out.println(); } }
chapter2
复制代码
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
68package demo1; //import java.util.*; import java.util.Scanner; public class chapter2 { public static void three(){ Scanner scan =new Scanner(System.in); System.out.print("输入三角形的三边:"); int a = scan.nextInt(); int b = scan.nextInt(); int c = scan.nextInt(); float s =(a+b+c)/2f; float S = (float)Math.sqrt(s*(s-a)*(s-b)*(s-c)); if (a+b>c && b+c>a && a+c>b){ System.out.println("三角形的面积是:"+S); } else{ System.out.println("不构成三角形"); } } public static void score(){ Scanner scan = new Scanner(System.in); System.out.print("输入分数:"); int score = scan.nextByte(); int n = score/10; switch (n){ case 10: System.out.print("该同学等级A"); break; case 9: System.out.println("该同学等级A"); break; case 8: System.out.println("该同学等级B"); break; case 7: System.out.println("该同学等级C"); break; case 6: System.out.println("该同学等级D"); break; case 5: case 4: case 3: case 2: case 1: case 0: System.out.println("该同学等级E"); } } public static void number(){ Scanner scan =new Scanner(System.in); System.out.print("输入数字:"); int number = scan.nextInt(); String s=String.valueOf(number); System.out.println("我是"+s.length()+"位数"); //System.out.println((number+"").length()); for (int i=0; i<s.length(); i++){ System.out.print(s.charAt(i)); //charAt(i)方法 返回str串处于i位置上的字符 System.out.print(" "); } System.out.println(); for (int i=s.length()-1; i>=0; i--){ System.out.print(s.charAt(i)); //charAt(i)方法 返回str串处于i位置上的字符 } } public static void main(String[] args){ three(); score(); number(); System.out.println(); } }
chapter3
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18/* * 一个卖花女卖鲜花,晴天时每天可卖20朵鲜花,雨天时每天可卖出12朵鲜花。 * 有一段时间连续几天共卖出了112朵鲜花,平均每天卖出14朵。 * 请计算出有几个晴天几个雨天。 * */ public class demo1 { public static void main(String[] args){ int i = 112/14; //总天数i int x =0,y=0; // 晴天x,雨天y while(20*x+12*(i-x)!=112) { //晴天卖花数20x,雨天卖花数12(i-x) x++; y=i-x; } System.out.println("晴天有"+x+"天"); System.out.println("雨天有"+y+"天"); } }
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22/* * 一个数如果恰好等于它的因子之和,这个数就称为完数。 * 编写应用程序求1000之内的所有完数。 * */ public class demo2 { public static void main(String[] args){ int i,j; for(i = 1;i<=1000;i++){ int sum; sum =0; for(j = 1; j < i; j++) { if(i%j ==0) { sum +=j; } } if(i == sum){ System.out.println(" "+i); } } } }
复制代码
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
30import java.util.Random; import java.util.Scanner; /* * 猜数字小游戏: * 要求:由计算机设想一个100以内的整数([1,100]的随机数); * 用户从键盘输入数字进行猜测,若猜大或猜小了则给出提示并重新输入; * 若猜中了,则提示“恭喜你猜中了!”,同时输出猜测的次数! * */ public class demo3 { public static void main(String[] args) { Random random=new Random(); int rand=random.nextInt(100)+1; System.out.print("输入数字进行猜测:"); Scanner input=new Scanner(System.in); while(true) { int i=input.nextInt(); if(i==rand) { System.out.println("猜对了"); return; } else if(i<rand) { System.out.println("猜小了"); } else { System.out.println("猜大了"); } } } }
chapter3
复制代码
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
72import java.util.Random; import java.util.Scanner; /* * 1、将十进制数转换为二进制输出。 * 2、 打印输出杨辉三角形的前N行。 * 3、15 个猴子选大王,其规则为:猴子依次1-7循环报数,报到7的猴子被淘汰,直到最后一只猴子成为大王。问:哪只猴子会成为大王? * 4、利用随机数产生一个4位(各位上的数字互不相同)的随机数验证码。 * */ public class demo4 { // 1、将十进制数转换为二进制输出。 public static String covertBin(int num) { String binStr = ""; //辗转相除 余数倒序拼接 直到商为1时跳出循环 while (num != 1) { binStr = num % 2 + binStr; num /= 2; } return "1" + binStr; } // 2、 打印输出杨辉三角形的前N行。 public static void demo2(){ Scanner input =new Scanner(System.in); System.out.print("输入n:"); int n=input.nextInt(); int[][] a = new int[n][n]; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { if (j < i) { a[i][j] = 1; if (j == 0) { a[i][j] = 1; } else { a[i][j] = a[i - 1][j - 1] + a[i - 1][j]; } } else { a[i][j] = 1; } } for (int i = 0; i < n; i++) { for (int k = 1; k <= n - i; k++) System.out.printf(""); for (int j = 0; j <= i; j++) { System.out.printf("%3d ", a[i][j]); } System.out.printf("n"); } } // 3、15 个猴子选大王,其规则为:猴子依次1-7循环报数,报到7的猴子被淘汰,直到最后一只猴子成为大王。问:哪只猴子会成为大王? public static void demo3(){ } // 4、利用随机数产生一个4位(各位上的数字互不相同)的随机数验证码。 public static void demo4(){ Random r=new Random(); int tag[]={0,0,0,0,0,0,0,0,0,0}; String four=""; int temp=0; while(four.length()!=4){ temp=r.nextInt(10);//随机获取0~9的数字 if(tag[temp]==0){ four+=temp; tag[temp]=1; } } System.out.println(four); } public static void main(String[] args){ System.out.println("这个数的二进制是:"+covertBin(12)); demo4(); demo2(); } }
最后
以上就是要减肥鸡最近收集整理的关于Java实验课系列的全部内容,更多相关Java实验课系列内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复