调用方法、参数和参数

calling methods, parameters and arguments

这可能是个愚蠢的问题。我的以下代码看起来不错,但在输出中,它并不期望来自我的测试场景的结果。代码如下:

 import java.util.Scanner;
  public class PartyPlannerLab {
                      public static Scanner input = new Scanner(System.in);

    public static int getGuestCount(int guests) {
        while(true) {
            System.out.print("Enter number of guests: ");
            guests = input.nextInt();
            if (guests >= 1 && guests <= 100)
                break;
            else 
                System.out.println("The guest count must be at least 1, but does not exceed 100. Please enter again.");
        }
        return guests;
    }
    public static int getSlicesPerPerson(int slicesPerPerson) {
        while(true) {
        System.out.print("Enter number of slices per person: ");
        slicesPerPerson = input.nextInt();
        if (slicesPerPerson >= 1 && slicesPerPerson <= 8)
            break;
        else
            System.out.println("The pizza slice count must be at least 1, but does not exceed 8. Please try again.");
        }
        return slicesPerPerson;
    }
    public static double computeRoomCost(int guests, double roomCost) {
        if (guests <= 30)
            roomCost = 100.00;
        else
            roomCost = 200.00;
        return roomCost;
    }
    public static double computeSodaCost(double sodaCost, int guests) {
        sodaCost = guests * 1.50;
        return sodaCost;

    }
    public static void printSummary(int guests, double roomCost, double sodaCost, double pizzaCost) {
        System.out.println("Total Guests: " + guests);
        System.out.println("RoomCost: $" + roomCost);
        System.out.println("SodaCost: $" + sodaCost);
        System.out.println("PizzaCost: $" + pizzaCost);
        System.out.println("Total Cost: $" +(roomCost + sodaCost + pizzaCost));
    }
    public static void main(String[] args) {
        int guests = 0;
        int slicesPerPerson = 0;
        double roomCost = 0.0;
        double sodaCost = 0.0;
        double pizzaCost = 0.0;
        getGuestCount(guests);
        getSlicesPerPerson(slicesPerPerson);
        computeRoomCost(guests, roomCost);
        computeSodaCost(sodaCost, guests);
        printSummary(guests, roomCost, sodaCost, pizzaCost);

        input.close();
    }


    }

一个输出如下:

Enter number of guests: 10
Enter number of slices per person: 2
Total Guests: 0
RoomCost: [=11=].0
SodaCost: [=11=].0
PizzaCost: [=11=].0
Total Cost: [=11=].0

您没有得到输出的原因是您在 main 方法中初始化的值没有随着您正在进行的方法调用而更新。

Below code might solve the problem you are facing -

 public static void main(String[] args) {
    int guests = 0;
    int slicesPerPerson = 0;
    double roomCost = 0.0;
    double sodaCost = 0.0;
    double pizzaCost = 0.0;
    guests = getGuestCount(guests);
    slicesPerPerson = getSlicesPerPerson(slicesPerPerson);
    roomCost = computeRoomCost(guests, roomCost);
    sodaCost = computeSodaCost(sodaCost, guests);
    printSummary(guests, roomCost, sodaCost, pizzaCost);

    input.close();
}

注意-方法中不需要传递参数getGuestCount & getSlicesPerPerson 因为输入取自 I/O

您没有使用 getGuestCountgetSlicesPerPerson 等的 return 值

那些方法return一个值,这基本上意味着你可以把它们当作一个值来使用。 input.nextInt return 也是一个值,这就是为什么你可以把它放在 = 的右边。

在方法内部,getGuestCount似乎改变了传入的guests的值,但这个改变实际上不会反映到调用方,因为 Java 是按值传递的。你有点丢掉传入的值。

事实上,只有当参数通过引用传递时,你的方法才会按原样工作,这样方法就可以修改传入的变量。但这是不可能的在 Java。请参阅 this post 以了解按值传递和按引用传递之间的区别。

在 Java 中重写方法的正确方法是 return 值(他们已经在这样做,但您没有使用 return 值),并删除无关参数。

public static int getGuestCount() {
    int guests;
    while(true) {
        System.out.print("Enter number of guests: ");
        guests = input.nextInt();
        if (guests >= 1 && guests <= 100)
            break;
        else 
            System.out.println("The guest count must be at least 1, but does not exceed 100. Please enter again.");
    }
    return guests;
}
public static int getSlicesPerPerson() {
    int slicesPerPerson;
    while(true) {
    System.out.print("Enter number of slices per person: ");
    slicesPerPerson = input.nextInt();
    if (slicesPerPerson >= 1 && slicesPerPerson <= 8)
        break;
    else
        System.out.println("The pizza slice count must be at least 1, but does not exceed 8. Please try again.");
    }
    return slicesPerPerson;
}
public static double computeRoomCost(int guests) {
    double roomCost;
    if (guests <= 30)
        roomCost = 100.00;
    else
        roomCost = 200.00;
    return roomCost;
}
public static double computeSodaCost(int guests) {
    double sodaCost = guests * 1.50;
    return sodaCost;
}

这就是你 "make use of the return values" 的方式:不要传入你希望方法修改的变量,而是将其放在赋值语句中 = 的左侧:

    guests = getGuestCount();
    slicesPerPerson = getSlicesPerPerson();
    roomCost = computeRoomCost(guests);
    sodaCost = computeSodaCost(guests);