Java添加方法

时间:2017-07-14 06:10:33

标签: java class methods

大家好我是java的新手,我发现了一些逻辑错误。首先,我正在尝试向我的班级添加方法,但它似乎并没有起作用。当我运行并且我输入0时它没有显示错误但它打印出另一个"输入你的选择",然后当我输入0时它显示无效输入。另一个错误是我如何进行下一步?

  import java.util.Scanner;

 public class OnlineOrder {
 public static void main(String args[]) {
    String item;
    int choice = 0;
    int quantity = 0;
    String choiceFood = "";
    float price = 0;
    Scanner sc = new Scanner(System.in);
    readChoice();
    checkChoice(choice);
    processOrder(choiceFood, price, quantity);
    System.out.println("Enter quantitiy of ");

    while (choice <= 1 || choice >= 4) {
        switch (choice) {
        case 1:
            choiceFood = "Hamburger";
            price = 1.50f;
            break;

        case 2:
            choiceFood = "Cheeseburger";
            price = 2.50f;
            break;

        case 3:
            choiceFood = "French Fries";
            price = 2.00f;
            break;

        case 4:
            choiceFood = "Soft Drinks";
            price = 1.95f;
            break;

        default:
            System.out.println("Invalid");
            sc.close();
            break;

        }
    }

    System.out.println("Are you a student? (Yes/No) : ");
    String input = sc.next();
    char ynStudent = input.charAt(0);
    double discount = 0;
    if (ynStudent == 'y' || ynStudent == 'Y') {
        discount = 0.9;
    }
    float totalPrice = price * quantity;
    totalPrice = (float) (totalPrice * discount);

    System.out.println("You have ordered " + quantity + " " + choiceFood);
    System.out.print("You have to pay total of $");
    System.out.printf("%.2f", totalPrice);
}

public static int readChoice() {
    int choice = 0;
    Scanner sc = new Scanner(System.in);

    System.out.println("Item                             Price");
    System.out.println("====                             ===== ");
    System.out.println("1. Hamburger                     1.50");
    System.out.println("2. Cheeseburger                  2.50");
    System.out.println("3. French Fries                  2.00");
    System.out.println("4. Soft Drinks                   1.95");
    System.out.println("Enter your choice(1,2,3 or 4): ");
    choice = sc.nextInt();

    return choice;
}

public static String processOrder(String choiceFood, double price, int choice) {

    switch (choice) {
    case 1:
        choiceFood = "Hamburger";
        price = 1.50f;
        break;

    case 2:
        choiceFood = "Cheeseburger";
        price = 2.50f;
        break;

    case 3:
        choiceFood = "French Fries";
        price = 2.00f;
        break;

    case 4:
        choiceFood = "Soft Drinks";
        price = 1.95f;
        break;

    default:
        System.out.println("Invalid");
        break;

    }

    return choiceFood;
}

public static int checkChoice(int choice) {
    Scanner sc = new Scanner(System.in);

    do {
        System.out.println("Enter your choice (1,2,3 or 4:): ");
        choice = sc.nextInt();
        if (choice < 1 || choice > 4) {
            System.out.println("Invalid Input");

        }
    } while (choice >= 1 || choice <= 4);

    return choice;
}

}

3 个答案:

答案 0 :(得分:1)

public class OnlineOrder {

public static void main(String args[]) {
    String item;
    int choice = 0;
    int quantity = 0;
    String choiceFood = "";
    float price = 0;
    Scanner sc = new Scanner(System.in);
    readChoice();
    choice = checkChoice();
    processOrder(choiceFood, price, quantity);
    System.out.println("Enter quantitiy of ");


        switch (choice) {
            case 1:
                choiceFood = "Hamburger";
                price = 1.50f;
                break;

            case 2:
                choiceFood = "Cheeseburger";
                price = 2.50f;
                break;

            case 3:
                choiceFood = "French Fries";
                price = 2.00f;
                break;

            case 4:
                choiceFood = "Soft Drinks";
                price = 1.95f;
                break;

            default:
                System.out.println("Invalid");
                sc.close();
                break;


    }

    System.out.println("Are you a student? (Yes/No) : ");
    String input = sc.next();
    char ynStudent = input.charAt(0);
    double discount = 0;
    if (ynStudent == 'y' || ynStudent == 'Y') {
        discount = 0.9;
    }
    float totalPrice = price * quantity;
    totalPrice = (float) (totalPrice * discount);

    System.out.println("You have ordered " + quantity + " " + choiceFood);
    System.out.print("You have to pay total of $");
    System.out.printf("%.2f", totalPrice);
}

public static void readChoice() {

    Scanner sc = new Scanner(System.in);

    System.out.println("Item                             Price");
    System.out.println("====                             ===== ");
    System.out.println("1. Hamburger                     1.50");
    System.out.println("2. Cheeseburger                  2.50");
    System.out.println("3. French Fries                  2.00");
    System.out.println("4. Soft Drinks                   1.95");


}

public static String processOrder(String choiceFood, double price, int choice) {

    switch (choice) {
        case 1:
            choiceFood = "Hamburger";
            price = 1.50f;
            break;

        case 2:
            choiceFood = "Cheeseburger";
            price = 2.50f;
            break;

        case 3:
            choiceFood = "French Fries";
            price = 2.00f;
            break;

        case 4:
            choiceFood = "Soft Drinks";
            price = 1.95f;
            break;

        default:
            System.out.println("Invalid");
            break;

    }

    return choiceFood;
}

public static int checkChoice() {
    int choice = 0;
    Scanner sc = new Scanner(System.in);

    while (choice >= 1 || choice <= 4) {
        System.out.println("Enter your choice (1,2,3 or 4:): ");
        choice = sc.nextInt();
        if (choice < 1 || choice > 4) {
            System.out.println("Invalid Input");

        }
        else {
            break;
        }
    } ;

    return choice;
}

}

希望这适合你。顺便说一下,你不要求在代码中的任何地方量化项目。

答案 1 :(得分:0)

你写的程序有一些错误。

您没有使用readChoice()方法的返回值分配选择变量。

choice = readChoice();

而不是readChoice();

还要更改checkChoice()方法,以确保它显示所有无效选项(如0

)的消息
public static int checkChoice(int choice) {
Scanner sc = new Scanner(System.in);
if (choice < 1 || choice > 4) {
        System.out.println("Invalid Input");

}else{
        return choice;
}
do {
    System.out.println("Enter your choice (1,2,3 or 4:): ");
    choice = sc.nextInt();
    if (choice < 1 || choice > 4) {
        System.out.println("Invalid Input");

    }
} while (choice < 1 || choice > 4);

return choice;

}

假设您要求用户输入选择,直到他输入有效选项。

再次将checkChoice()的值重新分配给变量选项。

choice = checkChoice(choice);

答案 2 :(得分:-1)

当您输入0并输入一段时间时,您没有案例条件 你只能使用defult,因此它无效