鱼C论坛

 找回密码
 立即注册
查看: 3368|回复: 1

[已解决]小白求助 如何用array和enum写下面这个程序

[复制链接]
匿名鱼油
匿名鱼油  发表于 2021-9-13 22:37:11 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 匿名 于 2021-9-15 11:56 编辑

程序是下面的样子
命令不区分大小写
列表不包含100个值

  1. import java.util.Scanner;

  2. public class ArrayEnum {
  3.     public static void main(String[] args){
  4.         //填写程序
  5.         final Scanner scanner = new Scanner(System.in);
  6.         
  7.         while(ture){
  8.             final Command command = getCommand(scanner); //Command is enum
  9.             if(command == Command.QUIT){
  10.                 System.out.println("Bye!");
  11.                 break;
  12.             }
  13.             switch (command){
  14.                 case ADD:
  15.                     final int newValue = getValue(scanner);
  16.                     value[index] = newValue;
  17.                     index++;
  18.                     break;
  19.                 case LIST:
  20.                     printList(values,index);
  21.                     break;
  22.                 case SUM:
  23.                     System.out.println(getSum(value,index));
  24.                     break;
  25.                 //填写程序
  26.             }
  27.         }
  28.         scanner.close();
  29.     }
  30.    //填写程序
  31. }
复制代码

最后的输出结果
  1. ADD 10
  2. add20
  3. LIST
  4. 10 20
  5. Sum
  6. 30
  7. Add 30
  8. suM
  9. 60
  10. Max
  11. 30
  12. Min
  13. 10
  14. Summ
  15. Invail Command
  16. list
  17. 10 20 30
  18. Quit
  19. Bye!
复制代码
最佳答案
2021-9-15 11:26:38
枚举类:
  1. package excel;

  2. public enum Command {
  3.     ADD,LIST,SUM,QUIT,MAX,MIN;
  4. }
复制代码

代码:
  1. package excel;

  2. import java.util.Arrays;
  3. import java.util.Scanner;

  4. /**
  5. * 使用array和enum填写代码
  6. */
  7. public class Test {
  8.     static int index = 0;
  9.     public static void main(String[] args) {
  10.         final Scanner scanner = new Scanner(System.in);
  11.         int[] value = new int[100];

  12.         while (true) {
  13.             final Command command = getCommand(scanner); //Command is enum
  14.             if (command == Command.QUIT) {
  15.                 System.out.println("Bye!");
  16.                 break;
  17.             }
  18.             switch (command) {
  19.                 case ADD:
  20.                     final int newValue = getValue(scanner);
  21.                     value[index] = newValue;
  22.                     index++;
  23.                     break;
  24.                 case LIST:
  25.                     printList(value.clone(), index);
  26.                     break;
  27.                 case SUM:
  28.                     System.out.println(getSum(value.clone(), index));
  29.                     break;
  30.                 case MAX:
  31.                     System.out.println(getMax(value.clone()));
  32.                     break;
  33.                 case MIN:
  34.                     System.out.println(getMin(value.clone()));
  35.                     break;
  36.                 default:
  37.                     break;
  38.             }
  39.         }
  40.         scanner.close();
  41.     }

  42.     private static int getMax(int[] value) {
  43.         Arrays.sort(value);
  44.         return value[value.length-1];
  45.     }
  46.     private static int getMin(int[] value) {
  47.         Arrays.sort(value);
  48.         return value[100-index];
  49.     }

  50.     private static int getSum(int[] value, int index) {
  51.         int sum = 0;
  52.         for (int i = 0; i < index; i++) {
  53.             sum += value[i];
  54.         }
  55.         return sum;
  56.     }

  57.     private static void printList(int[] value, int index) {
  58.         for (int i = 0; i < index; i++) {
  59.             System.out.print(value[i]+" ");
  60.         }
  61.         System.out.println();
  62.     }

  63.     private static int getValue(Scanner scanner) {
  64.         return scanner.nextInt();
  65.     }

  66.     private static Command getCommand(Scanner scanner) {
  67.         String next = scanner.next();
  68.         String s = next.toUpperCase();
  69.         switch (s) {
  70.             case "ADD":
  71.                 return Command.ADD;
  72.             case "LIST":
  73.                 return Command.LIST;
  74.             case "SUM":
  75.                 return Command.SUM;
  76.             case "QUIT":
  77.                 return Command.QUIT;
  78.             case "MAX":
  79.                 return Command.MAX;
  80.             case "MIN":
  81.                 return Command.MIN;
  82.             default:
  83.                 break;
  84.         }
  85.         return null;
  86.     }
  87. }
复制代码

效果图:
图片2.png
屏幕截图 2021-09-13 233651.png
回复

使用道具 举报

发表于 2021-9-15 11:26:38 | 显示全部楼层    本楼为最佳答案   
枚举类:
  1. package excel;

  2. public enum Command {
  3.     ADD,LIST,SUM,QUIT,MAX,MIN;
  4. }
复制代码

代码:
  1. package excel;

  2. import java.util.Arrays;
  3. import java.util.Scanner;

  4. /**
  5. * 使用array和enum填写代码
  6. */
  7. public class Test {
  8.     static int index = 0;
  9.     public static void main(String[] args) {
  10.         final Scanner scanner = new Scanner(System.in);
  11.         int[] value = new int[100];

  12.         while (true) {
  13.             final Command command = getCommand(scanner); //Command is enum
  14.             if (command == Command.QUIT) {
  15.                 System.out.println("Bye!");
  16.                 break;
  17.             }
  18.             switch (command) {
  19.                 case ADD:
  20.                     final int newValue = getValue(scanner);
  21.                     value[index] = newValue;
  22.                     index++;
  23.                     break;
  24.                 case LIST:
  25.                     printList(value.clone(), index);
  26.                     break;
  27.                 case SUM:
  28.                     System.out.println(getSum(value.clone(), index));
  29.                     break;
  30.                 case MAX:
  31.                     System.out.println(getMax(value.clone()));
  32.                     break;
  33.                 case MIN:
  34.                     System.out.println(getMin(value.clone()));
  35.                     break;
  36.                 default:
  37.                     break;
  38.             }
  39.         }
  40.         scanner.close();
  41.     }

  42.     private static int getMax(int[] value) {
  43.         Arrays.sort(value);
  44.         return value[value.length-1];
  45.     }
  46.     private static int getMin(int[] value) {
  47.         Arrays.sort(value);
  48.         return value[100-index];
  49.     }

  50.     private static int getSum(int[] value, int index) {
  51.         int sum = 0;
  52.         for (int i = 0; i < index; i++) {
  53.             sum += value[i];
  54.         }
  55.         return sum;
  56.     }

  57.     private static void printList(int[] value, int index) {
  58.         for (int i = 0; i < index; i++) {
  59.             System.out.print(value[i]+" ");
  60.         }
  61.         System.out.println();
  62.     }

  63.     private static int getValue(Scanner scanner) {
  64.         return scanner.nextInt();
  65.     }

  66.     private static Command getCommand(Scanner scanner) {
  67.         String next = scanner.next();
  68.         String s = next.toUpperCase();
  69.         switch (s) {
  70.             case "ADD":
  71.                 return Command.ADD;
  72.             case "LIST":
  73.                 return Command.LIST;
  74.             case "SUM":
  75.                 return Command.SUM;
  76.             case "QUIT":
  77.                 return Command.QUIT;
  78.             case "MAX":
  79.                 return Command.MAX;
  80.             case "MIN":
  81.                 return Command.MIN;
  82.             default:
  83.                 break;
  84.         }
  85.         return null;
  86.     }
  87. }
复制代码

效果图:
图片2.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-20 04:19

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表