menu1中的Menu2 in scanner:如何订购所有案例?

问题描述 投票:0回答:1

3个问题:

  1. Menu2的扫描仪下输入Menu1:如何订购所有可能的案例,包括例外?
  2. 如何在扫描仪中分离菜单值和形状值:while / if或其他方式?
  3. 为什么printCalc()函数在if下调用时不起作用?

import java.util。*;

公共类ScannerShape {

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();
        }
    }
}

}

java arraylist java.util.scanner
1个回答
0
投票

这个版本正在运行:

import 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");
    }
}

}

© www.soinside.com 2019 - 2024. All rights reserved.