修复我的班级名称和Java中的一种方法

时间:2014-05-02 10:57:03

标签: java class methods

我知道这些必须是基本错误,但我不确定如何修复它们。

我将班级名称改为Interface&现在Java有问题。

另外,在我的switch语句中,我试图调用enterData方法,但是我在这一行以及此行上都收到错误...“private static void enterData()”< - - 它说这条线上缺少“令牌”?

我正在尝试从案例0调用一个方法,但它无效。

import java.util.Scanner;

    public class Interface { 
        private void run() 
        {

            Scanner console = new Scanner(System.in);
            Store store1 = new Store(); // MUST DO THIS

            int demandRate, option, end;
            double setupCost, unitCost, inventoryCost;
            double sellingPrice, optimalOrder;
            String name;

            do {
                System.out.println("Enter product data (0), Show product data (1), Show product strategy (2), Exit program (9).");
                option = console.nextInt();
                switch(option)
                {
                case 0: enterData();
                        break;
                case 1:
                        break;
                case 2:
                        break;
                case 9: System.out.println("You chose to exit the program.");
                        break;
                default: System.out.println("Please choose a valid option.");
                }
            } while (option != 9);

            private static void enterData()
            {
                System.out.println("Product name between 3 & 10 characters long: ");
                name = console.nextLine();
                while ((name.length() < 3) || (name.length() > 10)) {
                    System.out.println("Please put in a name between 3 & 10 characters long.");
                    name = console.nextLine();
                }           
                name = name.toLowerCase();

                System.out.println("Demand rate: ");
                demandRate = console.nextInt();
                while (demandRate <= 0) {
                    System.out.println("Please put in a positive integer.");
                    demandRate = console.nextInt();
                }

                System.out.println("Setup cost: ");
                setupCost = console.nextDouble();
                while (setupCost <= 0) {
                    System.out.println("Please put in a positive number.");
                    setupCost = console.nextInt();
                }

                System.out.println("Unit cost: ");
                unitCost = console.nextDouble();
                while (unitCost <= 0) {
                    System.out.println("Please put in a positive number.");
                    unitCost = console.nextInt();
                }

                System.out.println("Inventory cost: ");
                inventoryCost = console.nextDouble();
                while (inventoryCost <= 0) {
                    System.out.println("Please put in a positive number.");
                    inventoryCost = console.nextInt();
                }

                System.out.println("Selling price: ");
                sellingPrice = console.nextDouble();
                while (sellingPrice <= 0) {
                    System.out.println("Please put in a positive integer.");
                    sellingPrice = console.nextInt();
                }
               }
        }

    public static void main(String[] args) { 
     Interface intFace = new Interface(); 
     intFace.run(); 
        } 
    } 

3 个答案:

答案 0 :(得分:1)

您无法在其他方法中定义方法。 将您的代码更改为:

public class Interface {
    private void run() 
    {

        Scanner console = new Scanner(System.in);
        Store store1 = new Store(); // MUST DO THIS

        int demandRate, option, end;
        double setupCost, unitCost, inventoryCost;
        double sellingPrice, optimalOrder;
        String name;

        do {
            System.out.println("Enter product data (0), Show product data (1), Show product strategy (2), Exit program (9).");
            option = console.nextInt();
            switch(option)
            {
            case 0: enterData();
                    break;
            case 1:
                    break;
            case 2:
                    break;
            case 9: System.out.println("You chose to exit the program.");
                    break;
            default: System.out.println("Please choose a valid option.");
            }
        } while (option != 9);
    }
    private static void enterData()
    {
        int demandRate, option, end;
        double setupCost, unitCost, inventoryCost;
        double sellingPrice, optimalOrder;
        Scanner console = new Scanner(System.in);

        System.out.println("Product name between 3 & 10 characters long: ");
        String name = console.nextLine();
        while ((name.length() < 3) || (name.length() > 10)) {
            System.out.println("Please put in a name between 3 & 10 characters long.");
            name = console.nextLine();
        }           
        name = name.toLowerCase();

        System.out.println("Demand rate: ");
        demandRate = console.nextInt();
        while (demandRate <= 0) {
            System.out.println("Please put in a positive integer.");
            demandRate = console.nextInt();
        }

        System.out.println("Setup cost: ");
        setupCost = console.nextDouble();
        while (setupCost <= 0) {
            System.out.println("Please put in a positive number.");
            setupCost = console.nextInt();
        }

        System.out.println("Unit cost: ");
        unitCost = console.nextDouble();
        while (unitCost <= 0) {
            System.out.println("Please put in a positive number.");
            unitCost = console.nextInt();
        }

        System.out.println("Inventory cost: ");
        inventoryCost = console.nextDouble();
        while (inventoryCost <= 0) {
            System.out.println("Please put in a positive number.");
            inventoryCost = console.nextInt();
        }

        System.out.println("Selling price: ");
        sellingPrice = console.nextDouble();
        while (sellingPrice <= 0) {
            System.out.println("Please put in a positive integer.");
            sellingPrice = console.nextInt();
        }
       }


    public static void main(String[] args) { 
     Interface intFace = new Interface(); 
     intFace.run(); 
     } 

}

答案 1 :(得分:0)

尝试制作单独的方法并将这些字段设为全局。像这样的东西

import java.util.Scanner;

public class Interface {

    int demandRate, option, end;
    double setupCost, unitCost, inventoryCost;
    double sellingPrice, optimalOrder;
    String name;

    private void run() {

        Scanner console = new Scanner(System.in);
        Store store1 = new Store(); // MUST DO THIS
        do {
            System.out
                    .println("Enter product data (0), Show product data (1), Show product strategy (2), Exit program (9).");
            option = console.nextInt();
            switch (option) {
            case 0:
                enterData(console);
                break;
            case 1:
                break;
            case 2:
                break;
            case 9:
                System.out.println("You chose to exit the program.");
                break;
            default:
                System.out.println("Please choose a valid option.");
            }
        } while (option != 9);
    }

    private void enterData(Scanner console) {
        System.out.println("Product name between 3 & 10 characters long: ");
        name = console.nextLine();
        while ((name.length() < 3) || (name.length() > 10)) {
            System.out
                    .println("Please put in a name between 3 & 10 characters long.");
            name = console.nextLine();
        }
        name = name.toLowerCase();

        System.out.println("Demand rate: ");
        demandRate = console.nextInt();
        while (demandRate <= 0) {
            System.out.println("Please put in a positive integer.");
            demandRate = console.nextInt();
        }

        System.out.println("Setup cost: ");
        setupCost = console.nextDouble();
        while (setupCost <= 0) {
            System.out.println("Please put in a positive number.");
            setupCost = console.nextInt();
        }

        System.out.println("Unit cost: ");
        unitCost = console.nextDouble();
        while (unitCost <= 0) {
            System.out.println("Please put in a positive number.");
            unitCost = console.nextInt();
        }

        System.out.println("Inventory cost: ");
        inventoryCost = console.nextDouble();
        while (inventoryCost <= 0) {
            System.out.println("Please put in a positive number.");
            inventoryCost = console.nextInt();
        }

        System.out.println("Selling price: ");
        sellingPrice = console.nextDouble();
        while (sellingPrice <= 0) {
            System.out.println("Please put in a positive integer.");
            sellingPrice = console.nextInt();
        }
    }

    public static void main(String[] args) {
        Interface intFace = new Interface();
        intFace.run();
    }
}

答案 2 :(得分:-2)

接口是java中的某种抽象类定义关键字。 您可以使用关键字为大写字母命名,但严重的是,不要这样做。

并且您没有调用方法,而是在另一种方法中实现它。你应该再次在java中编写和调用方法;)