当前位置: 首页 > 知识库问答 >
问题:

扫描仪中的menu1下的Menu2:如何对所有病例进行排序?

后凯捷
2023-03-14
    null
public static void printCalc() {
    ArrayList<Shape> list = new ArrayList <Shape> ();
    for (int i = 0; i < list.size(); i++) {
        System.out.println(list.get(i)+"Area: "+list.get(i).calcArea()+" Circumference: "+list.get(i).calcCircumference());
    }
}
public static void printMenu1() {
    System.out.println( "1. Add new shape\r\n" + 
            "2. List all shapes\r\n" +
            "3. Sum all circumferences\r\n" +
            "4. Sum all areas\r\n" +
            "5. Find biggest circumference\r\n" +
            "6. Find biggest area\n" +
            "7. Exit" +
            "Enter a number from the list above");
}
public static void printMenu2() {
    System.out.println( "1. Square\r\n" + 
            "2. Rectangle\r\n" + 
            "3. Circle\r\n" + 
            "4. Right triangle\r\n" + 
            "Enter a number from the list above");
}
public static void main (String[] args) {       
    int i = 0; 
    int val, val1 = 0;

    @SuppressWarnings("resource")
    Scanner sc = new Scanner(System.in);
    printMenu1();
    while (true) {          
        val=sc.nextInt();
        if (val == 7) {
            System.out.println("Exit");
            break;                           
        }                     
        if (val == 1)
            printMenu2();
        val=sc.nextInt();   

        if (val == 1)
            System.out.println("Enter Square width");
        val=sc.nextInt();
        ArrayList<Shape> list = new ArrayList <Shape> ();
        list.add(new Square(val));
        printCalc();

        if (val == 2) {
            System.out.println("Enter Rectangle height");
            val=sc.nextInt();
            System.out.println("Enter Rectangle width");
            val=sc.nextInt();
            val1=sc.nextInt();
            list.add(new Rectangle(val,val1));
            printCalc();
        }
        if (val == 3) {
            System.out.println("Enter Circle radius");
            val=sc.nextInt();
            list.add(new Circle(val));
            printCalc();
        }

        if (val == 4) {
            System.out.println("Enter Right triangle height");
            val=sc.nextInt();
            System.out.println("Enter Right triangle width");
            val1=sc.nextInt();
            list.add(new Triangle(val,val1));
            printCalc();
        }
        if (val1 == 2) {
            System.out.println("2. List all shapes");
            printCalc();
            val1=sc.nextInt();
        }   
        if (val1 == 3) {
            System.out.println("3. Sum all circumferences");

            for (int i1 = 0; i1 < list.size(); i1++) {
                for (int i2 = 0; i2 < list.size(); i2++) {
                    list.get(i1).calcCircumference();
                }
            }
            System.out.println(list.get(i)+" Circumference: "+ list.get(i).calcCircumference());    
            val1=sc.nextInt();
        }   
        if (val1 == 4) {
            System.out.println("4. Sum all areas");
            val1=sc.nextInt();
        }
        if (val1 == 5) {
            System.out.println("5. Find biggest circumference");
            val1=sc.nextInt();
        }   
        if (val1 == 6) {
            System.out.println("6. Find biggest area");
            val1=sc.nextInt();
        }
    }
}

共有1个答案

吕飞翼
2023-03-14

此版本正在工作:

导入java.util.*;

公共类ShapesScanner{

public static void main (String[] args) {       
    boolean shouldExit = false;

    while (!shouldExit) {
        printMainMenu(); 
        int menuSelection = sc.nextInt();

        if (shapes.size() == 0 && menuSelection != 1) {
            System.out.println("Error: Shape or parameter was not selected or selected no one;");           
            continue;
        }
        switch (menuSelection) {
        case 1:
            printAddShapeMenu();
            addShape(sc.nextInt());
            break;

        case 2: {                   
            for (Shape sh : shapes) {  
                sh.toString();              // Print List of all entered Shapes;
            }
        } break;

        case 3: {
            double sum = 0;
            for (Shape sh : shapes) {
                sum += sh.calcCircumference(); 
            }
            System.out.println("Sum all circumferences: "+ sum + "\n");
        } break;

        case 4: {
            double sum = 0;
            for (Shape sh : shapes) {
                sum += sh.calcArea();
            }               
            System.out.println("Sum all areas: "+ sum + "\n");
        } break;

        case 5: {               
            double max = Double.MIN_VALUE;
            double min = Double.MAX_VALUE;
            for (Shape sh : shapes) {
                double num = sh.calcCircumference();
                min = Math.min(min, num);
                max = Math.max(max, num);
            } 
            System.out.println("Biggest circumference: " + max + "\n");
            System.out.println("Min circumference: " + min + "\n");
        } break;    

        case 6: {               
            double max = Double.MIN_VALUE;
            double min = Double.MAX_VALUE;
            for (Shape sh : shapes) {
                Double num = sh.calcArea();
                min = Math.min(min, num);
                max = Math.max(max, num);
            } 
            System.out.println("Biggest area: " + max + "\n");
            System.out.println("Min area: " + min + "\n");
        } break;

        case 7: {
            System.out.println("Exit");
            shouldExit = true;
        }
        break;
        }
    }
}
// printMainMenu(), printAddShapeMenu(), addShape(int shapeNum) - print menus, add Shapes, enter the Shape parameters and save;  
private static Scanner sc = new Scanner(System.in);
private static ArrayList<Shape> shapes = new ArrayList<Shape>();    // Save all entered Shapes;

public static void printMainMenu() {
    System.out.println( "\n1. Add new shape\n" + 
            "2. List all shapes\n" +
            "3. Sum all circumferences\n" +
            "4. Sum all areas\n" +
            "5. Find biggest circumference\n" +
            "6. Find biggest area\n" +
            "7. Exit" + "\nEnter a number from the list above\n");
}
public static void printAddShapeMenu() {
    System.out.println( "\n1. Square\n" + 
            "2. Rectangle\n" + 
            "3. Circle\n" + 
            "4. Right triangle\n" + 
            "Enter a number from the list above\n");
}
public static void addShape(int shapeNum) { 

    switch (shapeNum) {
    case 1: {
        System.out.println("Enter width");
        double width = sc.nextDouble();
        shapes.add(new Square(width));                              // Save all Square parameters;
        break;
    }
    case 2: {
        System.out.println("Enter width and height");
        double width = sc.nextDouble();
        double height = sc.nextDouble();
        shapes.add(new Rectangle(height,width));                    // Save all Rectangle parameters;
        break;  
    }
    case 3: {
        System.out.println("Enter radius");
        double radius = sc.nextDouble();
        shapes.add(new Circle(radius));                             // Save all Circle parameters;
        break;
    }
    case 4:
        System.out.println("Enter width and height");
        double width = sc.nextDouble();
        double height = sc.nextDouble();
        shapes.add(new Triangle(width, height));                    // Save all Triangle parameters;
        break;
    default:
        System.out.println("Invalid shape specified");
    }
}
 类似资料:
  • 问题内容: 我的程序中有一个扫描仪,可以读取文件的一部分并将其格式化为HTML。当我读取文件时,我需要知道如何使扫描仪知道它在一行的末尾,然后开始写入下一行。 这是代码的相关部分,让我知道是否遗漏了什么: 我发现了有关,但是我仍然不知道如何确定何时到达终点。 问题答案: 如果只想使用Scanner,则需要创建一个临时字符串,将其实例化到数据网格的nextLine()(因此它仅返回跳过的行),并创建

  • 在分离标记之前,我试图检查扫描仪是否有多个标记。我目前正在尝试使用扫描仪。hasNext()(我的扫描仪的名字是sc)。如果用户输入“string int”,那么我不想打印“enter age”或“enter grade” 目前,无论输入是什么,我的程序都会跳过if语句。如果我把它拿走!然后,即使输入是一行,这些行也会打印出来。他们似乎也打印后,下一个是我发现奇怪的。我将非常感谢您的帮助!

  • 我想读取一个文本文件,并将每一行放入一个字符串(字符串数组)。然而,这需要扫描文件两次,一次是为了找出有多少行,另一次是为了创建一个这样大小的字符串数组。但它抛出了一个错误,重置方法似乎不起作用。 这是相关的代码片段。

  • 我想从标准输入中读取一个双数,但是我总是得到这个异常: Java . util . inputmismatchexception 如果输入是整数,那没问题,但是如果是双精度的,我会得到异常。 线程“main”Java . util . inputmismatchexception Java . util . scanner . throw for(scanner . Java:864)Java .

  • 声纳扫描仪无法完成扫描。我检查了日志,我看到扫描卡在一个文件上,如下所示 信息:977/6093已分析文件,当前文件:C:\Projects\ABC\src\main\java。通用域名格式。化学机械抛光。rpt。汇报JAVA 扫描仪会一直打印此消息,扫描永远不会完成。这是最近的一个问题。我检查了该文件的历史记录,该文件没有任何更改。我最近更新了Sonar中的Java插件。我的服务器配置如下 So