【JavaSE04】Java中循环语句for,while,do···while-练习2

旧城等待, 2022-06-18 07:26 294阅读 0赞

1.编写一个剪子石头布对战小程序

该法是穷举法:将所有情况列出来

  1. import java.util.*;
  2. public class Game{
  3. public static void main(String[] args){
  4. Scanner input=new Scanner(System.in);
  5. System.out.println("经典小游戏:剪刀石头布");
  6. System.out.println("游戏规则:1:剪刀;2:石头;3:布");
  7. System.out.println("您的起始分数为:10分");
  8. int score=10;
  9. System.out.println("请输入要大战几百回合:");
  10. int count=input.nextInt();
  11. //外层我出的什么
  12. for(int i=1;i<=count;i++){
  13. System.out.println("请出拳:");
  14. //玩家出拳
  15. int fist=input.nextInt();
  16. //电脑出拳
  17. int computer=(int)(Math.random()*3)+1;
  18. //考虑电脑产生随机数的原理,可以扩大取值范围,但只使用中间的三个数值
  19. //如变为1—9,但只用456,此时需要将规则换为:4:剪刀;5:石头;6:布
  20. //只需要在switch的外层加一个if判断条件,限制computer取值:computer>=4&&computer<=6;即可
  21. //与电脑PK比较
  22. switch(fist){
  23. case 1:
  24. if(computer==1){
  25. System.out.println("你们打平了,电脑出的剪刀");
  26. }else if(computer==2){
  27. System.out.println("你输了,电脑出的石头!");
  28. score--;
  29. }else{
  30. System.out.println("你赢了,电脑出的布!");
  31. score++;
  32. }
  33. break;
  34. case 2:
  35. if(computer==1){
  36. System.out.println("你赢了,电脑出的剪刀!");
  37. score++;
  38. }else if(computer==2){
  39. System.out.println("你们打平了,电脑出的石头!");
  40. }else{
  41. System.out.println("你输了,电脑出的布!");
  42. score--;
  43. }
  44. break;
  45. case 3:
  46. if(computer==1){
  47. System.out.println("你输了,电脑出的剪刀");
  48. score--;
  49. }else if(computer==2){
  50. System.out.println("你赢了,电脑出的石头!");
  51. score++;
  52. }else{
  53. System.out.println("你们打平了,电脑出的布!");
  54. }
  55. break;
  56. }
  57. }
  58. System.out.println("您最后的得分为:"+score);
  59. }
  60. }

方法二:只需要比较

  1. import java.util.Scanner;
  2. public class Test6{
  3. public static void main(String[] args){
  4. //做一个剪刀石头布的对战小程序
  5. //1代表剪刀,2代表石头,3代表布
  6. Scanner input=new Scanner(System.in);
  7. int computer=(int)(Math.random()*3)+1;
  8. //方案一:下面是无限循环,还以优化,由用户开控制玩的局数
  9. //考虑电脑产生随机的概率问题,可以将取值范围变为1-5,我们用2,3,4来代表特定的含义,遇到1和5就舍弃
  10. for(;;){
  11. System.out.println("来玩剪刀石头布吧!1代表剪刀,2代表石头,3代表布,请输入:");
  12. int pk=input.nextInt();
  13. if((pk==1&&computer==3)||(pk==3&&computer==1)){
  14. if(pk>computer){
  15. System.out.println("你出的:"+pk+",电脑出的:"+computer+",你赢了");
  16. }else{
  17. System.out.println("你出的:"+pk+",电脑出的:"+computer+",电脑赢了");
  18. }
  19. }else if(pk==computer){
  20. System.out.println("你出的:"+pk+",电脑出的:"+computer+",你们不分上下");
  21. }else{
  22. if(pk<computer){
  23. System.out.println("你出的:"+pk+",电脑出的:"+computer+",你赢了");
  24. }else{
  25. System.out.println("你出的:"+pk+",电脑出的:"+computer+",电脑赢了");
  26. }
  27. }
  28. }
  29. //最笨的方法:将9种情况用if···else罗列出来
  30. }
  31. }

2.要求循环录入2个班的学员成绩

假设每个班都有3个学员,依次录入,统计超过90分的学员人数,以及这批超过90分的学员平均分。
这里写图片描述

  1. import java.util.Scanner;
  2. public class Test5{
  3. public static void main(String[] args){
  4. //要求循环录入2个班的学员成绩,假设每个班都有3个学员,
  5. //依次录入,统计超过90分的学员人数,以及这批超过90分的学员平均分。
  6. Scanner input=new Scanner(System.in);
  7. int score=0;
  8. int count=0;
  9. double sum=0;
  10. //外层为班级
  11. for(int i=1;i<=2;i++){
  12. //内层为班中的学员
  13. for(int j=1;j<=3;j++){
  14. System.out.println("请输入"+i+"班第"+j+"个学员成绩:");
  15. score=input.nextInt();
  16. if(score>90){
  17. count++;
  18. sum+=score;
  19. }
  20. }
  21. }
  22. System.out.println("两个班超过90的学员人数:"+count);
  23. //对于没有人超过90情况的处理
  24. if(count==0){
  25. System.out.println("两个班超过90的学员成绩平均分为:0");
  26. }else{
  27. System.out.println("两个班超过90的学员成绩平均分为:"+sum/count);
  28. }
  29. }
  30. }

发表评论

表情:
评论列表 (有 0 条评论,294人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Javaの while 循环语句练习

    编写一个程序后,要求从键盘输入数字 1/2/3 后,可显示抽奖得到的奖品;如果输入其 它数字或字符显示“没有奖品。”。 编译源程序, 在命令提示符窗口运行程序,然后分别按