JAVA理解逻辑程序的书上全部重要的习题
今天随便翻翻看以前学过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