今天隨便翻翻看以前學過JAVA理解邏輯程序的書上全部練習,為了一些剛學的學弟學妹,所以呢就把這些作為共享了.
希望對初學的學弟學妹有所幫助!
1 例子:升級“我行我素購物管理系統”,實現購物結算功能
2 代碼:
3
4 public class OneDay {
5 public static void main(String[] args) {
6 int shirtPrice=245;
7 int shoePrice=570;
8 int pADpRICE=320;
9 int ShoeNo=1;
10 int shirtNo=2;
11 int padNo=1;
12 double discount=0.8;
13 /*
14 * 計算消費總金額
15 *
16 * */
17 double finalPay=(shirtPrice*shirtNo+ shoePrice*ShoeNo+shoePrice+pADpRICE*padNo)*discount;
18 System.out.println("消費總金額:"+finalPay);
19 }
20 }
21
22
23 例子:升級“我行我素購物管理系統”,實現打印購物小票和計算積分功能
24 public class OneDay {
25 public static void main(String[] args) {
26 int shirtPrice=245;
27 int shoePrice=570;
28 int pADpRICE=320;
29 int ShoeNo=1;
30 int shirtNo=2;
31 int padNo=1;
32 double discount=0.8;
33 double finalPay=(shirtPrice*shirtNo+shoePrice*ShoeNo+shoePrice+pADpRICE*padNo)*discount;
34 double Sum=1800;
35 double SuM=Sum-finalPay;
36 //積分
37 int Point=(int)finalPay*3/100;
38 System.out.println("**********消費單***********");
39 System.out.println("購買物品\t單價\t個數\t金額");
40 System.out.println("T血\t¥245\t2\t¥490");
41 System.out.println("網球鞋\t¥570\t1\t¥570");
42 System.out.println("網球拍\t¥320\t1\t¥320\n");
43 System.out.println("折扣:\t"+discount);
44 System.out.println("消費總金額:"+finalPay);
45 System.out.println("實際交費\t¥"+Sum);
46 System.out.println("找錢\t¥"+SuM);
47 System.out.println("積分\t¥"+Point);
48 }
49 }
50
51 //我行我素,幸運抽獎
52 public class OneDay {
53 public static void main(String[] args) {
54 int cusNO;//客戶會員號
55 System.out.println("請輸入四位會員卡號:");
56 Scanner input=new Scanner(System.in);//導包
57 cusNO=input.nextInt();
58 //獲得 每位數字
59 int gewei=cusNO%10;
60 int shiwei=cusNO%10%10;
61 int baiwei=cusNO/100%10;
62 int qianwei=cusNO/1000;
63 int sum=gewei+shiwei+baiwei+qianwei;
64 System.out.println("會員卡號:"+cusNO+"各位之和:"+sum);
65 boolean isluck=sum>20;//boolean的判斷為true或false
66 System.out.println("是幸運客戶嗎?"+isluck);
67 }
68 }
69
70
71 //我行我素,判斷商品判斷商品折扣價格
72 public static void main(String[] args) {
73 int shirtPrice=245;
74 int shoePrice=570;
75 int padPrice=320;
76 //用戶輸入折扣
77 Scanner input=new Scanner(System.in);
78 System.out.println("請輸入折扣:");
79 double discount=input.nextDouble();
80 //計算商品的享受價格
81 double shirtPriceDis=shirtPrice*discount;
82 double shoePriceDis=shoePrice*discount;
83 double padPriceDis=padPrice*discount;
84 //判斷商品價格是否低於100
85 boolean shirtPricebool=shirtPriceDis<100;
86 boolean shoePricebool=shoePriceDis<100;
87 boolean padPricebool=padPriceDis<100;
88 System.out.println("T血折扣低於100嗎?"+shirtPricebool);
89 System.out.println("網球鞋折扣低於100嗎?"+shoePricebool);
90 System.out.println("網球拍折扣低於100嗎?"+padPricebool);
91
92
93 小明左右手分別拿出兩張牌
94 public static void main(String[] args) {
95 int num;
96 int num1;
97 System.out.println("輸出互換前手中的紙牌 :");
98
99 Scanner input=new Scanner(System.in);
100 System.out.println("左手中的紙牌:\n");
101 num=input.nextInt();
102 System.out.println("輸出右手的紙牌:");
103 num1=input.nextInt();
104 System.out.println("**************************************");
105 System.out.println("輸出互換後手中的紙牌 :");
106 int temp;
107 temp=num;
108 num=num1;//20
109 num1=temp;//10
110 System.out.println("左手中的紙牌:"+num);
111 System.out.println("輸出右手的紙牌:"+num1);
112 }
113 }
114
115
116 double sheshi=0;
117 int num=0;//循環次始值
118 double huashi;
119 do{
120 num++;
121 sheshi+=20;
122 huashi=sheshi*9/5+32;
123 System.out.println("攝氏溫度:"+sheshi+"度,華氏溫度:"+huashi+"度");
124 }while(num<=9);//循環次條件
125 System.out.println("結束");
126
127
128 public static void main(String[] args) {
129 Scanner input=new Scanner(System.in);
130 /*
131 * 產生隨機數
132 * */
133 int random=(int)(Math.random()*10);
134 System.out.println("************購物系統抽獎************\n");
135 System.out.println("請輸入4位會員號:");
136 int cusNo=input.nextInt();
137 /*
138 * 獲得百分位
139 * */
140 int baiwei=cusNo/100%10;
141 /*
142 * 判斷該會員是否是幸運會員
143 * */
144 if (baiwei==random)
145 System.out.println(cusNo+"是幸運客戶,獲得筆記本一個");
146
147 }else {
148 System.out.println(cusNo+"號客戶"+"謝謝");
149 }
150 }
151 public static void main(String[] args) {
152
153 Scanner input=new Scanner(System.in);
154 System.out.println("請輸入第一個整數");
155 int a=input.nextInt();
156 System.out.println("請輸入第二個整數");
157 int b=input.nextInt();
158 System.out.println("請輸入第三個整數");
159 int c=input.nextInt();
160 /*第一道排序*/
161 System.out.println("第一道排序");
162 if (a<b&&a<c) {
163 System.out.print(a);
164 System.out.print(b);
165 System.out.println(c);
166 }
167 /*第二道排序*/
168 System.out.println("第二道排序");
169 if (b<c) {
170 System.out.print(a);
171 System.out.print(b);
172 System.out.println(c);
173 }
174 System.out.println("每個變量的值");
175 System.out.println(a);
176 System.out.println(b);
177 System.out.println(c);
178 }
179 public static void main(String[] args) {
180 int price = 5000; // 機票的原價
181 int month; // 出行的月份
182 int type; // 頭等艙為1,經濟艙為2
183 Scanner input = new Scanner(System.in);
184 System.out.println("請輸入您出行的月份:1~12");
185 month = input.nextInt();
186 System.out.println("請問您選擇頭等艙還是經濟艙?頭等艙輸入1,經濟艙輸入2");
187 type = input.nextInt();
188
189 switch(month/10){
190 case 1:
191 case 2:
192 case 3:
193 if (type == 1) // 頭等艙
194 {
195 System.out.println("您的機票價格為:"+ price * 0.6);
196 }
197 else if (type == 2) // 經濟艙
198 {
199 System.out.println("您的機票價格為:"+ price * 0.3);
200 }
201 break;
202 case 0:
203 case 4:
204 case 5:
205 case 6:
206 case 7:
207 case 8:
208 case 9:
209 if (type == 1) // 頭等艙
210 {
211 System.out.println("您的機票價格為:"+price * 0.9);
212 }
213 else if (type == 2) // 經濟艙
214 {
215 System.out.println("您的機票價格為:"+ price * 0.8);
216 }
217 break;
218 default:
219 System.out.println("請正確輸入成績!");
220 break;
221 }
222
223
224 public static void main(String[] args) {
225 Scanner input=new Scanner(System.in);
226 System.out.println("*******************計算器****************");
227 int result=0;
228 char opt='*'; //定義操作符
229 System.out.println("請輸入第一個操作數:");
230 if (input.hasNextInt() == true) {
231 int num1 = input.nextInt();
232 System.out.println("請輸入第二個操作數:");
233 if (input.hasNextInt() == true) {
234 int num2 = input.nextInt();
235 switch (opt) {
236 case '+':
237 result=num1+num2;
238 break;
239 case '-':
240 result=num1-num2;
241 break;
242 case '*':
243 result=num1*num2;
244 break;
245 case '/':
246 result=num1/num2;
247 break;
248 default:
249 System.out.println("輸入錯誤。");
250 break;
251 }
252 System.out.println("計算結果:"+num1+""+opt+num2+"="+result);
253 }else {
254 System.out.println("請輸入正確的數字!");
255 }
256 } else {
257 System.out.println("請輸入正確的數字!");
258 }
259 public static void main(String[] args) {
260 Scanner input=new Scanner(System.in);
261 System.out.println("*******************系統*************");
262 System.out.println("1.進入");
263 System.out.println("2.退出");
264 System.out.println("請輸入數字");
265 /*
266 * 通過調用Scanner對象的hasNextInt(),來判斷輸入的數字是否合法
267 * */
268 if (input.hasNextInt()==true) {
269 int num=input.nextInt();
270 switch (num) {
271 case 1:
272 System.out.println("don't go");
273 break;
274 case 2:
275 System.out.println("let go");
276 break;
277 default:
278 System.out.println("錯誤");
279 break;
280 }
281 }
282
283 public static void main(String[] args) {
284 //方法一:
285 /*100以內的偶數之和
286 * */
287 int num=0;
288 int sum=0;
289 while (num<=100) {
290 if (num%2==0) {
291 sum+=num;
292 }
293 num++;
294 }
295 System.out.println(sum);
296 //方法二:
297 while (num<=100) {
298 sum+=num;
299 num+=2;
300 }
301 System.out.println(sum);
302
303
304 }
305 public static void main(String[] args) {
306 Scanner input=new Scanner(System.in);
307 System.out.println("請選擇編號:\n 1.T血,2.網球鞋,3.網球拍");
308
309
310 double price=0.0;
311 String name="";
312 int goodsno=0;
313 String answer="y";
314 while ("y".equals(answer)) {
315 System.out.println("請輸入編號:");
316 goodsno=input.nextInt();
317 switch (goodsno) {
318 case 1:
319 name="djs";
320 price=245.0;
321 break;
322 case 2:
323 name="fd";
324 price=570.0;
325 break;
326 case 3:
327 name="res";
328 price=320.0;
329 break;
330 default:
331 break;
332 }
333 System.out.println(name+"\t"+"¥"+price+"\t");
334 System.out.println("是否繼續?(y/n)");
335 answer=input.next();
336 }
337 System.out.println("程序結束");
338 }
339 public static void main(String[] args) {
340 //01.列出商品清單
341 System.out.println("1.T恤 2.網球鞋 3.網球拍");
342 //02.肯定要用到循環
343 String answer="";
344 //定義折扣
345 double discount=0.8;
346 //保存總金額
347 double money=0;
348 Scanner input=new Scanner(System.in);
349
350 do{
351 System.out.println("請輸入商品編號");
352
353 int proNo=input.nextInt();
354 System.out.println("請輸入商品數量");
355 int proNum=input.nextInt();
356 String proName="";
357 double proPrice=0;
358 switch (proNo) {
359 case 1:
360 //列出第一個商品的信息
361 //名稱
362 proName="T恤";
363 //價格
364 proPrice=25;
365 break;
366 case 2:
367 proName="網球鞋";
368 proPrice=260;
369 break;
370 case 3:
371 proName="網球拍";
372 proPrice=200;
373 break;
374 default:
375 break;
376 }
377 //03.輸出當前所購買商品的詳情
378 double sum=proPrice*proNum;
379 money=money+sum;
380 System.out.println(proName+"\t¥"+proPrice+"\t數量"+proNum+"\t小計"+sum);
381 System.out.println("是否繼續?(y/n)");
382 answer=input.next();
383 }while(answer.equals("y"));
384 //出了循環,意味著用戶要結賬,離開商店
385 System.out.println("折扣:"+discount);
386 //應付金額
387 double yingfuMoney=money*discount;
388 System.out.println("應付金額:"+yingfuMoney);
389 System.out.println("請輸入您的支付金額:");
390 int userMoney=input.nextInt();
391 //找零
392 double lastMoney=userMoney-yingfuMoney;
393 //最好判定一下
394 if (userMoney>=yingfuMoney) {
395 System.out.println("找零"+lastMoney);
396 }
397
398 //用戶錄入一堆數字,拎出最大和最小值
399 Scanner input=new Scanner(System.in);
400 System.out.println("爺爺,請您輸入一個數字呗");
401 int num=input.nextInt();
402 //最大值
403 int max=num;
404 //最小值
405 int min=num;
406 while(num!=0){
407 if(num>max){
408 max=num;
409 }
410 if(num<min){
411 min=num;
412 }
413 System.out.println("爺爺,請您輸入一個數字呗");
414 num=input.nextInt();
415 }
416 System.out.println("最大值是"+max+"\t最小值是"+min);
417
418
419
420
421 public static void main(String[] args) {
422 Scanner input=new Scanner(System.in);
423 int cource=0;
424 int number=0;//班級總人數
425 int sum=0;//大於80分的人數
426 System.out.println("總人數:");
427 number =input.nextInt();
428 for (int i =0; i <number; i++) {
429 System.out.println("輸入第"+(i+1)+"位成績");
430 cource=input.nextInt();
431 if (cource<80) {
432 continue;
433 }
434 sum++;
435 }
436 System.out.println("80分以上的人數"+sum);
437 double rate=(double)sum/number*100;
438 System.out.println("比例:"+rate+"%");
439 }
440
441 public static void main(String[] args) {
442 Scanner input=new Scanner(System.in);
443 int sum=0;
444 int coure=0;
445 int avg=0;
446 boolean isnegi=false;
447 //Scanner input=new Scanner(System.in);
448 for (int i =0; i <5; i++) {
449 System.out.println("請輸入5門課程的"+(i+1)+"門課的成績:");
450 coure=input.nextInt();
451 if (coure<0) {
452 isnegi=true;
453 break;
454 }
455 sum+=coure;
456 }
457 if (isnegi) {
458 System.out.println("成績輸入錯誤");
459 }else {
460 avg=sum/5;
461 System.out.println("總成績:"+sum);
462 System.out.println("平均成績:"+avg);
463 }
464
465
466 public static void main(String[] args) {
467 Scanner input=new Scanner(System.in);
468 int pwd=123456;//密碼
469 String name="jim";//戶名s
470 for (int i = 0; i <3; i++) {
471 System.out.println("請輸入用戶名:");
472 String Rname=input.next();
473 System.out.println("請輸入密碼:");
474 int Rpwd=input.nextInt();
475 if (Rname.equals(name)&&Rpwd==pwd) {
476 System.out.println("歡迎登陸系統");
477
478 }else {
479 System.out.println("輸入錯誤!您還有"+(3-(i+1))+"次機會");
480 System.out.println("對不起!"+((i+1))+"次錯誤!");
481 continue;
482
483 }
484 }
485 int rabbit;//兔子
486 int chicken;//小雞
487 for(rabbit=1;rabbit<14;rabbit++){
488 chicken=35-rabbit;
489 if((rabbit*4+chicken*2)==94)
490 System.out.println("兔子:"+rabbit+"\t小雞:"+chicken);
491 }
492
493
494 for (int i = 1; i <=100; i++) {
495
496 if (i%3==0) {
497 System.out.println("Flip");
498 }
499 else {
500 System.out.println(i);
501 }
502 if (i%5==0) {
503 System.out.println("Flop");
504 }
505 else {
506 System.out.println(i);
507 }
508 if (i%3==0&&i%5==0) {
509 System.out.println("FlipFlop");
510 }else {
511 System.out.println(i);
512 }
513 }
514
515
516 int men;
517 int women;
518 for( men=0;men<=10;men++){
519 for( women=0;women<=30;women++){
520 if(30-men-women >= 0){
521 int count = 3*men + 2*women + 1*(30-men-women) ;
522 if(count == 50){
523 System.out.println("男人:" + men+",女人:"+women+",小孩:" +(30-men-women)) ;
524 }
525 }
526 }
527 }
528
529
530 public static void main(String[] args) {
531 Scanner input=new Scanner(System.in);
532 /*
533 *選擇游戲
534 *
535 * */
536 System.out.println("歡迎進入青鳥迷你戲平台");
537 System.out.println("");
538 System.out.println("請選擇你喜愛的游戲");
539 System.out.println("----------------------------");
540 System.out.println("1.斗地主");
541 System.out.println("2.斗牛");
542 System.out.println("3.泡泡龍");
543 System.out.println("4.連連看");
544 System.out.println("-----------------------------");
545 System.out.println("請選擇,輸入數字:");
546 int num = input.nextInt();
547 switch (num) {
548 case 1:
549 System.out.println("您已進入斗地主房間!");
550 break;
551 case 2:
552 System.out.println("您已進入斗牛房間!");
553 break;
554 case 3:
555 System.out.println("您已進入泡泡龍房間!");
556 break;
557 case 4:
558 System.out.println("您已進入連連看房間!");
559 break;
560 }
561 /*
562 * 玩游戲升級
563 *
564 * */
565 int score = 0;
566 int Num = 1;
567 String answer;
568 do {
569 System.out.println("您正在玩" + Num + "局,輸入您的成績為:");
570 score = input.nextInt();
571 if (score > 80) {
572 }
573 Num++;
574 if (Num > 5) {
575 System.out.println("游戲結束!!");
576 System.out.println("恭喜你,通過一級");
577
578 } else {
579 System.out.println("繼續玩下一局嗎?(yes/no)");
580 answer = input.next();
581 if (answer.equals("no")) {
582 System.out.println("您已中途退出游戲");
583 System.out.println("對不起,您未能晉級,繼續加油呀!");
584 break;
585 } else {
586 System.out.println("進入下一局");
587 }
588 }
589 } while (Num <= 5);
590
591 /*玩游戲並支付游戲幣
592 *
593 * */
594 int money;
595 double discount1 = 0.5;
596 double discount2 = 0.8;
597 int m1 = 10;
598 int m2 = 20;
599 System.out.println("請您選擇玩的游戲類型:");
600 System.out.println("1.牌類");
601 System.out.println("2.休閒競技類");
602 int num1 = input.nextInt();
603 System.out.println("請您輸入游戲時長:");
604 int time = input.nextInt();
605 switch (num1) {
606 case 1:
607 if (time > 10) {
608 System.out.println("您玩的是牌類游戲,時長是:" + time + "小時,可以享受5折優惠");
609 money = (int) (time * m1 * discount1);
610 System.out.println("您需支付" + money + "個游戲幣");
611
612 } else {
613 System.out.println("您玩的是牌類類游戲,時長是:" + time + "小時,可以享受8折優惠");
614 money = (int) (time * m1 * discount2);
615 System.out.println("您需支付" + money + "個游戲幣");
616 }
617 break;
618 case 2:
619 if (time > 10) {
620 System.out.println("您玩的是休閒類游戲,時長是:" + time + "小時,可以享受5折優惠");
621 money = (int) (time * m2 * discount1);
622 System.out.println("您需支付" + money + "個游戲幣");
623
624 } else {
625 System.out.println("您玩的是休閒類游戲,時長是:" + time + "小時,可以享受8折優惠");
626 money = (int) (time * m2 * discount2);
627 System.out.println("您需支付" + money + "個游戲幣");
628 }
629 break;
630 }
631 /*統計游戲點擊率
632 *
633 * */
634 int j = 0;
635 int num2 =0;
636 for (int i = 1; i <= 4; i++) {
637 System.out.println("請輸入第" + i + "個游戲的點擊率:");
638 num2 = input.nextInt();
639 if (num2 <= 100) {
640 continue;
641 }
642 j++;
643 }
644 System.out.println("點擊率大於100的游戲數是:" + j);
645 double rate = (double)j/ 4 * 100;
646 System.out.println("點擊率大於100的游戲所占的比例是:" + rate + "%");
647 /*添加用戶信息
648 *
649 * */
650 int a=0,age=0,b=0;
651 System.out.println("請輸入要登錄用戶的數量:");
652 int num3=input.nextInt();
653 for (int i=0;i<num3;i++){
654 System.out.println("請輸入用戶編號<4位整數>:");
655 a=input.nextInt();
656 System.out.println("請輸入用戶年齡:");
657 age=input.nextInt();
658 System.out.println("請輸入用戶積分:");
659 b=input.nextInt();
660 if(age<10){
661 System.out.println("很抱歉,你年齡不適合玩游戲\n錄入失敗");
662 }else{
663 System.out.println("您輸入的會員信息是:");
664 System.out.println("用戶編號"+a+"\t用戶年齡"+age+"\t用戶積分"+b);
665 }
666 }
667
668 }
669
670 public static void main(String[] args) {
671
672 Scanner input=new Scanner(System.in);
673 System.out.println("請輸入整數:");
674 int in=input.nextInt();
675 int result=1;
676 System.out.print(in+"!=");
677 for (int i = 1; i<=in; i++) {
678 System.out.print(i);
679 if (i<in) {
680 System.out.print("*");
681
682 }
683 result=result*i;
684
685 }
686 System.out.print("="+result);
687 }
688
689
690
691
692 public static void main(String[] args) {
693 Scanner input=new Scanner(System.in);
694 int num = 0; //個數
695 int sum = 0; //和
696 for(int i = 0;i <= 100;i ++) {
697 if(i % 7!= 0) {
698 num ++;
699 sum = sum+i;
700 System.out.print(i + "\t");
701 if(num % 4 == 0)
702 System.out.println();
703 }
704 }
705 System.out.println();
706 System.out.println("總和為:"+sum);
707
708
709 Scanner input=new Scanner(System.in);
710
711 int scores []=new int[5];
712 int sum=0;
713
714 System.out.println("請輸入五位同學的成績:");
715 for (int i = 0; i < scores.length; i++) {
716 scores[i]=input.nextInt();
717 sum+=scores[i];//成績累加
718 }
719 System.out.print("平均分:"+sum/scores.length);
720
721
722
723 int scores []=new int[]{90,80,100,95,90};
724 System.out.println("請修改第三位同學的成績為98:");
725 scores[2]=98;//數組下標
726 System.out.println("修改後,五位同學的成績:");
727 for (int i = 0; i < scores.length; i++) {
728 System.out.print(scores[i]+" ");
729 }
730
731
732 public static void main(String[] args) {
733 Scanner input=new Scanner(System.in);
734 double sum=0;
735 double scores[]=new double[5];
736 for (int i = 0; i < scores.length; i++) {
737 System.out.print("請輸入第"+(i+1)+"筆金額的記錄:");
738 scores[i]=input.nextDouble();
739 sum+=scores[i];
740 }
741 System.out.println("序號\t\t"+"金額(元)");
742 for (int i = 0; i < scores.length; i++) {
743 System.out.print((i+1)+"\t\t");
744 System.out.println(scores[i]);
745 }
746 System.out.println("總金額:\t\t"+sum);
747
748
749 public static void main(String[] args) {
750 Scanner input=new Scanner(System.in);
751 int scores []=new int[5];
752 System.out.println("請輸入5位同學的成績:");
753 //錄入成績
754 for (int i = 0; i < scores.length; i++) {
755 scores[i]=input.nextInt();
756 }
757 Arrays.sort(scores);//排序
758 System.out.println("學員成績按升序排序:");
759 for (int i = 0; i < scores.length; i++) {
760 System.out.println(scores [i]+" ");
761 }
762
763
764 public static void main(String[] args) {
765 Scanner input=new Scanner(System.in);
766 int scores []=new int[5];
767 int max=0;
768 System.out.println("請輸入5位同學的成績:");
769 //錄入成績
770 for (int i = 0; i < scores.length; i++) {
771 scores[i]=input.nextInt();
772 }
773
774 for (int i = 0; i < scores.length; i++) {
775 if (scores[i]>max) {
776 max=scores[i];
777 }
778 }
779 System.out.println("最高分:"+max);
780 }
781
782 public static void main(String[] args) {
783 Scanner input=new Scanner(System.in);
784 int [] list=new int [6];
785 list[0]=99;
786 list[1]=95;
787 list[2]=92;
788 list[3]=89;
789 list[4]=69;
790 list[5]=49;
791 int index=list.length;//保存新增成績的位置
792 System.out.println("請輸入新增成績:");
793 int num=input.nextInt();//輸入要插入的數據
794 //找到新元素插入的位置
795 for (int i = 0; i < list.length; i++) {
796
797 if (num>list[i]) {
798 index=i;
799 break;
800 }
801 }
802 //元素後移
803 for (int i =list.length-1 ; i >index ; i--) {
804 list[i]=list[i-1];//index下標開始的元素後移一個位置
805 }
806 list[index]=num;
807 System.out.println("插入成績的下標:"+index);
808
809 System.out.println("插入後成績信息是:");
810 for (int i = 0; i < list.length; i++) {
811 System.out.println(list[i]+"\t");
812 }
813 }
814
815 public static void main(String[] args) {
816 Scanner input=new Scanner(System.in);
817 String[]num=new String[]{"a","c","u","b","e","p","f","z"};
818 System.out.print("原字符序列:");
819 for (int i = 0; i < num.length; i++) {
820 System.out.print(num[i]+" ");
821 }
822 Arrays.sort(num);
823 System.out.println();//換行
824 System.out.print("升序排序後:");
825 for (int i = 0; i < num.length; i++) {
826 System.out.print(num[i]+" ");
827 }
828 System.out.println();//換行
829 System.out.print("逆序輸出為:");
830 //逆序,則從最後的哪一個元素排在第一位
831 for (int i = num.length-1; i >=0 ; i--) {
832 System.out.print(num[i]+" ");
833 }
834
835 Scanner input=new Scanner(System.in);
836 String[]num=new String[]{"a","c","u","b","e","p","f","z"};
837 System.out.print("原字符序列:");
838 for (int i = 0; i < num.length; i++) {
839 System.out.print(num[i]+" ");
840 }
841 Arrays.sort(num);
842 System.out.println();//換行
843 System.out.print("升序排序後:");
844 for (int i = 0; i < num.length; i++) {
845 System.out.print(num[i]+" ");
846 }
847 System.out.println();//換行
848 System.out.print("逆序輸出為:");
849 //逆序,則從最後的哪一個元素排在第一位
850 for (int i = num.length-1; i >=0 ; i--) {
851 System.out.print(num[i]+" ");
852 }
853 int index=num.length;//待插入的字符的位置
854 System.out.println();
855 System.out.print("待插入的字符:");
856 String way=input.next();//輸入要插入的位置
857 //找到要插入字符的位置
858 for (int i = 0; i < num.length; i++) {
859 if (way.equalsIgnoreCase(num[i])) {
860 index=i;
861 break;
862 }
863 }
864 //元素後移
865 for (int i =num.length-1; i >index; i--) {
866 num[i]=num[i-1];
867 }
868 num[index].equals(way);//插入數據
869 System.out.println("插入字符的下標:"+index);
870 System.out.println("插入後字符的信息是:");
871 for (int i = 0; i < num.length; i++) {
872 System.out.println(num[i]+"\t");
873 }
874
875 Scanner input=new Scanner(System.in);
876 System.out.println("請輸入4家店的價格");
877 int[]num=new int[4];
878 for (int i = 0; i < num.length; i++) {
879 System.out.print("第"+(i+1)+"店的價格:");
880 num[i]=input.nextInt();
881 }
882 int min=num[0];
883 int index=0;
884 for (int j = 0; j < num.length; j++) {
885 if (num[j]<min) {
886 min=num[j];
887 index=j;
888 }
889
890 }
891 System.out.print("最低價格:"+min);
892 System.out.println("且它在數組中的原始位置(下標)是:" + index);
893 }
894 }
895
896
897 }
898
899 Scanner input=new Scanner(System.in);
900 String []num= new String[5];
901 System.out.println("請輸入5句話");
902 for (int i = 0; i < num.length; i++) {
903 System.out.print("第"+(i+1)+"句話:");
904 num[i]=input.next();
905 }
906 System.out.println();
907 System.out.println("逆序輸出的5句話:");
908 for (int i =num.length-1; i>=0 ; i--) {
909 System.out.print(num[i]+" ");
910 }
911 int[] points = { 18, 25, 7, 36, 13, 2, 89, 63 };
912 System.out.println("8位顧客的積分:");
913 for (int i = 0; i < points.length; i++) {
914 System.out.print(points[i] + "\t");
915 }
916 System.out.println();
917 int min = 0;
918 int index = 0;
919 min = points[0];
920 for (int i = 1; i < points.length; i++) {
921 if (points[i] < min) {
922 min = points[i];
923 index = i;
924 }
925 }
926 System.out.println("其中最低積分:" + min);
927 System.out.println("且它在數組中的原始位置(下標)是:" + index);
928 }
929
930 Scanner input=new Scanner(System.in);
931 int nums[] = new int[10];
932 int a = 0;
933 int b = 0;
934 int c = 0;
935 int d = 0;
936 System.out.println("請輸入10個數:");
937 for (int i = 0; i < nums.length; i++) {
938 nums[i] = input.nextInt();
939
940 switch (nums[i]) {
941 case 1:
942 a++;
943 break;
944 case 2:
945 b++;
946 break;
947 case 3:
948 c++;
949 break;
950 default:
951 d++;
952 break;
953 }
954
955 }
956 System.out.println("數字1的個數:"+a);
957 System.out.println("數字2的個數:"+b);
958 System.out.println("數字3的個數:"+c);
959 System.out.println("非法數字的個數:"+d);
960
961
962 Scanner input=new Scanner(System.in);
963
964 int []array=new int[]{1,3,-1,5,-2};
965 System.out.println("原數組的為:");
966 for (int i = 0; i < array.length; i++) {
967 System.out.print(array[i]+ " ");
968 }
969 System.out.println();
970 int newarray[]=new int[5];
971 for (int i = array.length - 1; i >= 0; i--) {
972 if (array[i] < 0) {
973 continue;
974 }
975 if (array[i] > 0) {
976 newarray[array.length - i - 1] = array[i];
977 }
978 }
979 System.out.println("");
980 System.out.println("逆序並處理後的數組為:");
981 for (int i = 0; i < newarray.length; i++) {
982 System.out.print(newarray[i]+" ");
983 }
984 }
985 public static void main(String[] args) {
986 Scanner input=new Scanner(System.in);
987 int [] score=new int[4];
988 int classNum=3;
989 double Sum=0.0;//成績總和
990 double []average=new double[classNum];//平均成績數組
991 //循環輸入學員成績
992 for (int i = 0; i <classNum; i++) {
993 Sum=0.0;//成績總和歸零
994 System.out.println("請輸入第"+(i+1)+"個班的成績");
995 for (int j = 0; j < score.length; j++) {
996 System.out.print("第"+(j+1)+"個學員的成績:");
997 score[j]=input.nextInt();
998 Sum=Sum+score[j];
999 }
1000 average[i]=Sum/score.length;//計算平均分
1001 System.out.println("第"+(i+1)+"個班級參賽學員的總成績:"+Sum);
1002 System.out.print("第"+(i+1)+"個班級參賽學員的平均分是:"+average[i]+" \n");
1003 }
1004
1005 }
1006
1007 public static void main(String[] args) {
1008 Scanner input=new Scanner(System.in);
1009
1010
1011 int [] score=new int[4];
1012 int classNum=3;
1013 double Sum=0.0;//成績總和
1014 int count=0;//記錄80分以上的人數
1015 double []average=new double[classNum];//平均成績數組
1016 //循環輸入學員成績
1017 for (int i = 0; i <classNum; i++) {
1018 Sum=0.0;//成績總和歸零
1019 System.out.println("請輸入第"+(i+1)+"個班的成績");
1020 for (int j = 0; j < score.length; j++) {
1021 System.out.print("第"+(j+1)+"個學員的成績:");
1022 score[j]=input.nextInt();
1023 Sum=Sum+score[j];
1024 if (score[j]<85) {//成績小於85,則跳出本次循環
1025 continue;
1026 }
1027 count++;
1028 }
1029 average[i]=Sum/score.length;//計算平均分
1030 System.out.println("第"+(i+1)+"個班級參賽學員的總成績:"+Sum);
1031 System.out.print("第"+(i+1)+"個班級參賽學員的平均分是:"+average[i]+" \n");
1032 System.out.println("成績85分以上的人數:"+count+"人");
1033 }
1034
1035 Scanner input=new Scanner(System.in);
1036 System.out.println("請輸入直角三角形的行數:");
1037 int Rownum=0;
1038 Rownum=input.nextInt();
1039 for (int i = 1; i <=Rownum; i++) {//行數
1040 for (int j = 1; j <=i*2-1; j++) {//星數
1041 System.out.print("☆");
1042 }
1043 System.out.println();
1044 }
1045 }
1046 Scanner input=new Scanner(System.in);
1047 System.out.println("請輸入倒直角三角形的行數:");
1048 int Rownum=0;
1049 Rownum=input.nextInt();
1050 for (int i = 1; i <=Rownum; i++) {//行數
1051 for (int j = 1; j <=10-i; j++) {
1052 System.out.print("*");
1053 }
1054 System.out.println("");
1055 }
1056
1057 }
1058 Scanner input=new Scanner(System.in);
1059 System.out.println("請輸入等腰三角形的行數:");
1060 int Rownum=0;
1061 Rownum=input.nextInt();
1062 for (int i = 1; i <=Rownum; i++) {//行數
1063 for (int j = 0; j <Rownum-i; j++) {//空格數
1064 System.out.print(" ");
1065 }
1066 for (int k = 1; k <=2*i-1; k++) {//星星數
1067 System.out.print("*");
1068 }
1069 System.out.println("");
1070
1071
1072 }
1073 public static void main(String[] args) {
1074 Scanner input=new Scanner(System.in);
1075 int Rownum=9;
1076 for (int i = 1; i <=Rownum; i++) {//行數
1077 for (int j = 1; j <=i; j++) {//空格數
1078 System.out.print(j+"*"+i+"="+j*i+" ");
1079 }
1080 System.out.println("");
1081
1082
1083 }
1084 public static void main(String[] args) {
1085 Scanner input=new Scanner(System.in);
1086 int count=0;//計數器
1087 String choice="";//顧客選擇是否離開
1088 for (int i = 0; i <5; i++) {
1089 System.out.println("歡迎光臨第"+(i+1)+"家專賣店");
1090 for (int j = 0; j <3; j++) {
1091 System.out.println("要離開嗎?(y/n)");
1092 choice=input.nextLine();
1093 //如果離開,則跳出,進入下一家店
1094 if ("y".equals(choice)) {
1095 break;
1096 }
1097 System.out.println("買了一件衣服");
1098 count++;
1099 }
1100 System.out.println("離店結賬");
1101 }
1102 System.out.println("總共買了"+count+"件衣服");
1103 choice=input.nextLine();
1104
1105
1106 int way = 1; //買法
1107 int k = 0; //雛雞數
1108 for(int i=1;i<=20;i++){ //公雞數
1109 for(int j=1;j<=33;j++){ //母雞數
1110 k = 100-i-j; //一共100只雞
1111 if(k%3 == 0 && (5*i+3*j+k/3 == 100)){//雛雞數是3的倍數,總計100文錢
1112 System.out.print("買法 " + way++ + ": ");
1113 System.out.println("公雞: " +i+ " 母雞:" +j+ " 雛雞:" +k);
1114
1115 }
1116 }
1117 }
1118 Scanner input=new Scanner(System.in);
1119
1120 int[] score = new int[4];//從零開始,0,1,2,3數 ,//成績數組
1121 int classnum = 3; //班級數目
1122 double sum = 0.0; //成績總和
1123 double average = 0.0; //平均成績
1124 int count = 0; //記錄85分以上學員人數
1125
1126 //循環輸入學員成績
1127
1128 for(int i = 0; i < classnum; i++){
1129 System.out.println("請輸入第" + (i+1) + "個班級的成績");
1130 for(int j = 0; j < score.length; j++){
1131 System.out.print("請輸入第" + (j+1) + "個學員的成績:");
1132 score[j] = input.nextInt();
1133 if(score[j] < 85){ //成績小於85,則跳出本輪循環
1134 continue;
1135 }
1136 sum = sum + score[j]; //成績85分以上才進行累加
1137 count++;
1138 }
1139 }
1140 average = sum / count; //所有成績85分以上的學員的平均分
1141 System.out.println("所有成績85分以上的學員的平均分為:" + average);
1142
1143 Scanner input=new Scanner(System.in);
1144 String pass = ""; //保存用戶輸入密碼
1145 int amount = 0; //取款金額
1146 String password = "111111"; //用戶密碼
1147 int count = 0; //記錄密碼輸入次數
1148 boolean isPass = false; //密碼是否通過驗證
1149 while(count < 3 && !isPass){
1150 System.out.print("請輸入密碼:");
1151 pass = input.next();
1152 if(!password.equals(pass)){
1153 count++;
1154 continue;
1155 }
1156 isPass = true; //密碼通過驗證
1157 System.out.print("請輸入金額:");
1158 amount = input.nextInt();
1159 while(amount>0){
1160 if(amount<=1000 && amount%100==0){
1161 System.out.println("您取了" +amount+ "元");
1162 System.out.println("交易完成,請取卡!");
1163 break; //完成交易,退出
1164 }else{
1165 System.out.print("您輸入金額的金額不合法,請重新輸入:");
1166 amount = input.nextInt();
1167 continue; //繼續讓用戶輸入金額
1168 }
1169 }
1170 }
1171 if(!isPass){ //用戶輸入了3次錯誤密碼
1172 System.out.print("密碼錯誤,請取卡!");
1173 }
1174 Scanner input=new Scanner(System.in);
1175 int rows = 0; //菱形的行數
1176 System.out.print("請輸入菱形行數:");
1177 rows = input.nextInt();
1178
1179 while(rows%2 == 0){
1180 System.out.print("請輸入奇數:");
1181 rows = input.nextInt();
1182 }
1183
1184 int n = (rows+1)/2;
1185 //打印菱形的上半部分
1186 for(int i = 1; i <= n; i++){//外層循環變量i控制行數
1187 for(int j = 1; j <= n-i; j++){//內層循環變量j控制該行空格數
1188 System.out.print(" ");
1189 }
1190 for(int k = 1; k <= 2*i-1; k++){//內層循環變量k控制該行*號數
1191 System.out.print("*");
1192 }
1193 System.out.print("\n");
1194 }
1195 //打印菱形的下半部分
1196 for(int i = n-1; i >= 1; i--){
1197 for(int j = 1; j <= n-i; j++){
1198 System.out.print(" ");
1199 }
1200 for(int k = 1; k <= 2*i-1; k++){
1201 System.out.print("*");
1202 }
1203 System.out.print("\n");
1204 }
1205 Scanner input = new Scanner(System.in);
1206 String answer = "";
1207 boolean a = false;
1208 boolean b = false;
1209 do {
1210 System.out.println("*********歡迎**********");
1211 System.out.println("1.注冊");
1212 System.out.println("2.登錄");
1213 System.out.println("3.抽獎");
1214 System.out.println("請輸入:");
1215 int num = input.nextInt();
1216 switch (num) {
1217 case 1:
1218 System.out.println("您選擇的是注冊");
1219 System.out.println("請填寫個人注冊信息");
1220 System.out.println("用戶名:");
1221 String name = input.next();
1222 System.out.println("密碼:");
1223 int nums = input.nextInt();
1224 int random = (int) (Math.random() * 9000) + 1000;
1225 System.out.println("用戶名\t密碼\t會員號");
1226 System.out.println(name + "\t" + nums + "\t" + random);
1227 a = true;
1228 System.out.println("是否繼續(y/n)");
1229 answer = input.next();
1230 break;
1231 case 2:
1232 if (a == true) {
1233 System.out.println("您選擇的是登錄");
1234 System.out.println("請輸入您的用戶名:");
1235 name = input.next();
1236 System.out.println("請輸入密碼:");
1237 nums = input.nextInt();
1238 System.out.println("歡迎您" + name);
1239 b = true;
1240 } else {
1241 System.out.println("請您先注冊,親");
1242 }
1243 System.out.println("是否繼續(y/n)");
1244 answer = input.next();
1245 break;
1246 case 3:
1247 int random1 = (int) (Math.random() * 9000) + 1000;
1248 if (b == true) {
1249 System.out.println("您選擇的是抽獎");
1250 System.out.println("請輸入您的卡號:");
1251 int kahao = input.nextInt();
1252 System.out.println("本日的幸運數字為");
1253 for (int i = 0; i < 5; i++) {
1254 random1 = (int) (Math.random() * 9000) + 1000;
1255 System.out.print(random1+"\t");
1256 }
1257 if (kahao == random1) {
1258 System.out.println("恭喜您,您中獎了");
1259 System.out.println("是否繼續(y/n)");
1260 answer = input.next();
1261
1262 } else {
1263 System.out.println("對不起!您不是本日的幸運會員.");
1264 System.out.println("是否繼續(y/n)");
1265 answer = input.next();
1266
1267 }
1268
1269 } else {
1270 System.out.println("請先登錄,親");
1271 System.out.println("是否繼續(y/n)");
1272 answer = input.next();
1273
1274 }
1275 break;
1276 default:
1277 System.out.println("輸入錯誤!");
1278 System.out.println("是否繼續(y/n)");
1279 answer = input.next();
1280 break;
1281 }
1282
1283 } while (answer.equals("y"));
1284 System.out.println("退出");
1285
1286 public class School {
1287 /*
1288 * School類的屬性
1289 * */
1290 String schName;//名稱
1291 int classNumber;//教室數目
1292 int labNumber;//機房數目
1293 /*
1294 * School類的Show方法
1295 * */
1296 public void Show(){
1297 System.out.println(schName+"培訓中心\n"+"配備:"+
1298 classNumber+"教"+labNumber+"機");
1299
1300 public static void main(String[] args) {
1301 School sch=new School();
1302 System.out.println("*******初始化成員變量前*******");
1303 sch.Show();
1304 sch.schName="北京";
1305 sch.classNumber=20;
1306 sch.labNumber=30;
1307 System.out.println("\n**********初始化變量後**************");
1308 sch.Show();
1309
1310
1311 public class Student {
1312 /*Student的屬性
1313 * */
1314 String name;
1315 int age;
1316 String classNo;//班級
1317 String hobby;//愛好
1318 /*Student的方法
1319 * */
1320 public void showStudent(){
1321 System.out.println("姓名:"+name+"\n年齡:"+age+"\n就讀於:"+classNo+"\n愛好:"+hobby);
1322
1323 }
1324 }
1325 public static void main(String[] args) {
1326 Student stu=new Student();
1327 stu.name="小小";
1328 stu.age=15;
1329 stu.classNo="S1";
1330 stu.hobby="打籃球";
1331 stu.showStudent();
1332
1333 public class Visitor {
1334 String name;
1335 int age;
1336 public void Show(){
1337 Scanner input=new Scanner(System.in);
1338 while (!"n".equals(name)) {
1339 if (age>=18&&age<=60) {
1340 System.out.println(name+"的年齡為:"+age+",門票價格為:20元\n");
1341 }else {
1342 System.out.println(name+"的年齡為:"+age+",門票免費\n");
1343 }
1344 System.out.println("請輸入姓名:");
1345 name=input.next();
1346 if (!"n".equals(name)) {
1347 System.out.print("請輸入年齡:");
1348 age=input.nextInt();//給age屬性賦值
1349 }
1350 }
1351 System.out.println("退出程序");
1352 }
1353
1354 public static void main(String[] args) {
1355 //************Visitor**********
1356 Scanner input=new Scanner(System.in);
1357 Visitor v=new Visitor();
1358 System.out.println("請輸入姓名:");
1359 v.name=input.next();
1360 System.out.println("請輸入年齡:");
1361 v.age=input.nextInt();
1362 v.Show();
1363
1364 public class Administrator {
1365 public String name;
1366 public String password;
1367
1368 }
1369 public class OneDay {
1370 public static void main(String[] args) {
1371 //************Visitor**********
1372 Scanner input=new Scanner(System.in);
1373 Administrator as=new Administrator();
1374 as.name="admin";
1375 as.password="111111";
1376 //輸入舊密碼
1377 String chioce="";
1378 do {
1379 System.out.println("請輸入用戶名:");
1380 String nameinput=input.next();
1381 System.out.println("請輸入密碼");
1382 String pass=input.next();
1383 //判斷用戶輸入的用戶名是否正確
1384 if (as.name.equals(nameinput)&&as.password.equals(pass)) {
1385 System.out.println("\n請輸入新密碼:");
1386 as.password=input.next();
1387 System.out.println("修改密碼成功!!您的新密碼:"+pass);
1388
1389 }else {
1390 System.out.println("用戶名和密碼不匹配,您沒有權利更改信息");
1391 System.out.println("還想繼續嗎?請輸入(y/n)");
1392 chioce=input.next();
1393 }
1394 }while("y".equals(chioce));
1395
1396
1397 }
1398
1399
1400 public class Calcutor {
1401 public int num1;
1402 public int num2;
1403 public String yunsuan;
1404
1405 }
1406 Scanner input =new Scanner(System.in);
1407 Calcutor calculator=new Calcutor();
1408 System.out.println("請輸入操作數");
1409 calculator.num1=input.nextInt();
1410 System.out.println("請輸入運算符");
1411 calculator.yunsuan=input.next();
1412 char a=calculator.yunsuan.charAt(0);
1413 System.out.println("請輸入操作數");
1414 calculator.num2=input.nextInt();
1415 switch(a){
1416 case '+':
1417 System.out.println(calculator.num1+"+"+calculator.num2+"="+(calculator.num1+calculator.num2));
1418 break;
1419
1420
1421 public class Currentlor {
1422 public String time;
1423 public void show(){
1424
1425 }
1426 public String time2;
1427 public void show(){
1428
1429 }
1430 public static void main(String[] args) {
1431 Scanner input =new Scanner(System.in);
1432 Currentlor currentTime=new Currentlor();
1433 currentTime.time="2015年5月12日10點11分00秒";
1434 System.out.println(currentTime.time);
1435 currentTime.show();
1436 System.out.println("改後為");
1437 Domo domo=new Domo();
1438 domo.time2="2015年5月12日10點11分30秒";
1439 System.out.println(domo.time2);
1440 domo.show();
1441
1442
1443 public class Season {
1444 String spring(){
1445 return "\n該季節為春季";
1446 }
1447 String summer (){
1448 return "\n該季節為夏季";
1449 }
1450 String autumn(){
1451 return "\n該季節為秋季";
1452 }
1453 String winter(){
1454 return "\n該季節冬季";
1455 }
1456 }
1457
1458 public static void main(String[] args) {
1459 // TODO Auto-generated method stub
1460 Season sn=new Season();
1461 //獲取輸入
1462 /* 接收成績 */
1463 Scanner input = new Scanner(System.in);
1464 System.out.print("請輸入月份:");
1465 int mon = input.nextInt();
1466 switch(mon){
1467 case 1:
1468 case 2:
1469 case 3:
1470 System.out .println(sn.spring());
1471 break;
1472 case 4:
1473 case 5:
1474 case 6:
1475 System.out .println(sn.summer());
1476 break;
1477 case 7:
1478 case 8:
1479 case 9:
1480 System.out .println(sn.autumn());
1481 break;
1482 case 10:
1483 case 11:
1484 case 12:
1485 System.out .println(sn.winter());
1486 break;
1487 default:
1488 System.out .println("輸入有誤,請輸入1~12月份數。");
1489 break;
1490
1491 }
1492
1493 public class GuessMachine {
1494 String name ;
1495 double price;
1496
1497 /**
1498 * 初始化商品信息
1499 */
1500 public void initial(){
1501 int random = (int)(Math.random()*10); //產生隨機數
1502 switch(random){
1503 case 0:
1504 case 1:
1505 case 2:
1506 case 3:
1507 name = "公主電動車";
1508 price = 2000;
1509 break;
1510 case 4:
1511 case 5:
1512 case 6:
1513 name = "34寸純平彩電";
1514 price = 3000;
1515 break;
1516 case 7:
1517 case 8:
1518 case 9:
1519 name = "新飛冰箱";
1520 price = 3400;
1521 break;
1522 default:
1523 break;
1524 }
1525 }
1526
1527 /**
1528 * 猜測
1529 */
1530 public void guess(){
1531 System.out.print("\n請猜測\""+ name + "\"的價格: " );
1532 Scanner input = new Scanner(System.in);
1533 double guessPrice = input.nextDouble(); //從鍵盤獲取用戶猜的價格;
1534 boolean isCorrect = false;
1535 for(int i = 0; i<3; i++){ //允許最多猜4次
1536 if(guessPrice == price){ //判斷是否猜對,並給出提示信息
1537 System.out.println("猜對了!");
1538 isCorrect = true;
1539 break;
1540 }else if(guessPrice > price){
1541 System.out.println("再小點!");
1542 }else{
1543 System.out.println("再大點!");
1544 }
1545 System.out.print("\n再猜一次吧: ");
1546 guessPrice = input.nextDouble();
1547 }
1548 if(!isCorrect){
1549 System.out.println("\n4次內沒有猜對,下次努力吧!");
1550 }}
1551 public static void main(String[] args) {
1552 GuessMachine gm = new GuessMachine();
1553 gm.initial();
1554 gm.guess();
1555 }
1556
1557 public class Computer {
1558 public String nickName;
1559 public int fenshu;
1560 public int chuquan(){
1561 int num=(int)(Math.random()*3)+1;
1562 switch (num) {
1563 case 1:
1564 System.out.println("["+nickName+"]"+"您出的是剪刀");
1565 break;
1566 case 2:
1567 System.out.println("["+nickName+"]"+"您出的是石頭");
1568 break;
1569 case 3:
1570 System.out.println("["+nickName+"]"+"您出的是布");
1571 break;
1572 default:
1573 break;
1574 }
1575 return num;
1576 }
1577
1578 public class Judge {
1579 Computer computer = new Computer();
1580 Person person = new Person();
1581 int count = 0;
1582 String answer = "";
1583
1584 public void result() {
1585 System.out.println("游戲結束了,比賽結果如下");
1586 System.out.println(person.nickName + "\tVS" + computer.nickName);
1587 System.out.println("我們一共玩了" + count + "局");
1588 System.out.println("其中我贏了" + person.fenshu + "局");
1589 if (person.fenshu > computer.fenshu) {
1590 System.out.println("我戰勝了你");
1591 } else if (person.fenshu == computer.fenshu) {
1592 System.out.println("我們打了個平手");
1593
1594 } else {
1595 System.out.println("我輸了,其實我是讓你的");
1596 }
1597 }
1598
1599 public void getOnceResult() {
1600 Scanner input = new Scanner(System.in);
1601 System.out.println("您確定對戰嗎?(y/n)");
1602 answer = input.next();
1603 do {
1604 count++;
1605 int person1 = person.chuquan();
1606 int computer1 = computer.chuquan();
1607 if (person1 == 1 && computer1 == 2 || person1 == 2
1608 && computer1 == 3 || person1 == 3 && computer1 == 1) {
1609 System.out.println("不好意思,我贏了");
1610 person.fenshu++;
1611 } else if (person1 == computer1) {
1612 System.out.println("我們平局");
1613 } else {
1614 System.out.println("我輸了,你厲害");
1615 computer.fenshu++;
1616
1617 }
1618 System.out.println("繼續嗎?(y/n)");
1619 answer = input.next();
1620 } while (answer.equals("y"));
1621 }
1622
1623 public void menuList() {
1624 System.out.println("------------------歡迎進入天盡頭猜拳系統--------------");
1625 System.out.println("\n\t\t******************");
1626 System.out.println("\n\t\t*****游戲開始了 ******");
1627 System.out.println("\n\t\t*******************");
1628 System.out.println("出拳規則:1.剪刀 2.石頭 3.布");
1629 System.out.println("請選擇對方角色:1.上帝 2.耶稣 3.佛祖");
1630 Scanner input = new Scanner(System.in);
1631 int num = input.nextInt();
1632 System.out.println("請輸入您的大名:");
1633 person.nickName = input.next();
1634 switch (num) {
1635 case 1:
1636 computer.nickName = "上帝";
1637
1638 System.out.println(person.nickName + ",您選擇與上帝對戰");
1639
1640 break;
1641 case 2:
1642 computer.nickName = "耶稣";
1643 System.out.println(person.nickName + ",您選擇與耶稣對戰");
1644
1645 break;
1646 case 3:
1647 computer.nickName = "佛祖";
1648 System.out.println(person.nickName + ",您選擇與佛祖對戰");
1649 break;
1650 default:
1651 break;
1652 }
1653 }
1654
1655 public void Welcome() {
1656 menuList();
1657 getOnceResult();
1658 // 出了循環,打印結果
1659 if (answer.equals("n")) {
1660 result();
1661
1662 }
1663 public String nickName;
1664 public int fenshu;
1665 public int chuquan(){
1666 Scanner input=new Scanner(System.in);
1667 System.out.println("請出拳1.剪刀 2.石頭 3.布 ");
1668 int num=input.nextInt();
1669
1670 switch (num) {
1671 case 1:
1672 System.out.println(nickName+"您出的是剪刀");
1673 break;
1674 case 2:
1675 System.out.println(nickName+"您出的是石頭");
1676 break;
1677 case 3:
1678 System.out.println(nickName+"您出的是布");
1679 break;
1680 default:
1681 break;
1682 }
1683 return num;
1684 }
1685
1686 public static void main(String[] args) {
1687 Judge judge=new Judge();
1688 judge.Welcome();
1689 }
1690
1691
1692 public class Loan {
1693
1694 /**
1695 * 計算月供
1696 * @return money
1697 */
1698 public double loan(double loan,int yearchoice){
1699 double money=0;
1700
1701 if(yearchoice==1){
1702 money=(loan+loan*0.0603)/36;
1703 }else if(yearchoice==2){
1704 money=(loan+loan*0.0612)/60;
1705 }else{
1706 money=(loan+loan*0.0639)/240;
1707 }
1708 return money;
1709 }
1710
1711 /**
1712 * @param args
1713 */
1714 public static void main(String[] args) {
1715
1716 Loan l= new Loan();
1717 Scanner input = new Scanner(System.in);
1718 System.out .print("請輸入貸款金額:");
1719 double loan=input.nextDouble();
1720 System.out .print("請選擇貸款年限:1.3年(36個月) 2.5年(60個月) 3.20年(240個月):");
1721 int yc=input.nextInt();
1722 System.out.println("\n***月供為:"+l.loan(loan, yc));
1723 }
1724
1725
1726
1727 public class StudentBiz {
1728 /**
1729 * 求平均分
1730 * @param 一個學生
1731 */
1732 public double getAvg(Student stu){
1733 return stu.java+stu.c+stu.sql/3.0;
1734 }
1735 public class Student {
1736 public int java;
1737 public int c;
1738 public int sql;
1739
1740 }
1741 import java.util.Scanner;
1742 public class Test {
1743 public static void main(String[] args) {
1744 Scanner input=new Scanner(System.in);
1745 //實例化學生對象
1746 Student student=new Student();
1747 System.out.println("請輸入java的成績");
1748 student.java=input.nextInt();
1749 System.out.println("請輸入c的成績:");
1750 student.c=input.nextInt();
1751 System.out.println("請輸入sql的成績:");
1752 student.sql=input.nextInt();
1753
1754 StudentBiz sb=new StudentBiz();
1755 double avgScore=sb.getAvg(student);
1756 System.out.println("平均分:"+avgScore);
1757
1758 }
1759 }
1760 public class Triangle {
1761 /**
1762 * 三角形的判斷
1763 * @param a、b、c邊長
1764 * @return 是否是三角形
1765 */
1766 public boolean isTriangle(int a,int b,int c){
1767 boolean flag=false;
1768 if((a+b)>c &&(b+c)>a &&(a+c)>b){
1769 flag=true;
1770 }
1771 return flag;
1772 }
1773 /**
1774 * 三角形的形狀判斷
1775 * @param a、b、c邊長
1776 * @return 何種三角形
1777 */
1778 public String Shape(int a,int b,int c){
1779 String shape="";
1780
1781 if((a==b)&&(b==c)&&(c==a)){
1782 shape="等邊三角形";
1783 }else if((a==b)||(b==c)||(c==a)){
1784 shape="等腰三角形";
1785 }else {
1786 int A=a*a;
1787 int B=b*b;
1788 int C=c*c;
1789 if((A>B+C)||(B>A+C)||(C>A+B)){
1790 shape="鈍角三角形";
1791 }else if((A==B+C)||(B==A+C)||(C==A+B)){
1792 shape="直角三角形";
1793 }else{
1794 shape="銳角三角形";
1795 }
1796 }
1797 return shape;
1798 }
1799
1800 public class TestTriangle {
1801 public static void main(String[] args) {
1802
1803 Triangle t=new Triangle();
1804 boolean con=true;
1805 while(con){
1806 Scanner input = new Scanner(System.in);
1807 System.out .print("請輸入第一條邊:");
1808 int num1=input.nextInt();
1809 System.out .print("請輸入第二條邊:");
1810 int num2=input.nextInt();
1811 System.out .print("請輸入第三條邊:");
1812 int num3=input.nextInt();
1813 if(t.isTriangle(num1,num2,num3)){
1814 System.out .print("這是一個"+t.Shape(num1, num2, num3));
1815 }else{
1816 System.out .print("這不能構成三角形。");
1817 }
1818 System.out .print("\n繼續嗎?(y/n):");
1819 String choice=input.next();
1820 if(choice.equals("n")){
1821 con=false;
1822 System.out .print("謝謝使用!");
1823 }
1824 }
1825
1826 public class InputVerify {
1827 public static void main(String[] args) {
1828 Scanner input = new Scanner(System.in);
1829 boolean con = true;
1830 /*檢驗生日形式的有效性*/
1831 while(con){
1832 System.out.print("\n請輸入會員生日<月/日:00/00>: ");
1833 String day = input.next();
1834 if(day.indexOf('/')!=2){
1835 System.out.println("生日形式輸入錯誤!");
1836 }else{
1837 System.out.println("該會員生日是: " + day);
1838 con = false;
1839 }
1840 }
1841
1842 con = true;
1843 /*檢驗用戶密碼的有效性*/
1844 while(con){
1845 System.out.print("\n請輸入會員密碼<6~10位>: ");
1846 String psw = input.next();
1847 if(psw.length() < 6 || psw.length() > 10){
1848 System.out.println("密碼形式輸入錯誤!");
1849 }else{
1850 System.out.println("該會員的密碼是: " + psw);
1851 con = false;
1852 }
1853 }
1854 }
1855 public class StringExplorer {
1856 /**
1857 * 定位字符
1858 */
1859 public void locateChar(String full, char c) {
1860 System.out.print(c + "出現的位置是: ");
1861 for (int i = 0; i < full.length(); i++) {
1862 if (c == full.charAt(i)) {
1863 System.out.print(i + "\t");
1864 }
1865 }
1866 }
1867
1868 /**
1869 * 定位字符串
1870 */
1871 public void locateString(String full, String s) {
1872 int index = 0;
1873 int i = index;
1874 int length = full.length();
1875 System.out.print(s + "出現的位置是: ");
1876 do {
1877 index = full.indexOf(s, i);
1878 if (index == -1) {
1879 break;
1880 }
1881 System.out.print(index + " ");
1882 i = index + s.length();
1883 } while (i <= length);
1884 }
1885 */
1886 public static void main(String[] args) {
1887 // TODO Auto-generated method stub
1888 StringExplorer se = new StringExplorer();
1889 Scanner input = new Scanner(System.in);
1890 System.out.println("請輸入一段字符: ");
1891 String full = input.next();
1892 System.out.println("請輸入要查詢的字符串: ");
1893 String s = input.next();
1894 se.locateString(full, s);
1895 }
1896