我想知道为什么我的 Int for Expenses and Difference 输出不正确?

I am wondering why my output for Int for Expenses and Difference isn't coming out right?

我想知道为什么我的输出没有按照我认为的方式出现。它仅适用于这两个整数。
前两个是正确的。

这是我的代码:

import java.util.Scanner;

public class BarkingLot {
  public static void main(String[] args) {
    Scanner Scanner = new Scanner(System.in);
    int x = 20;
    int y = 25;
    int Small = 0;
    int Large = 0;
    int Revenue = ((Small * x) + (Large * y));
    int Food = ((Small + Large) * (2));
    int Facility = 30;
    int Expenses = (Food + Facility);
    int Difference = (Revenue - Expenses);

    System.out.println("Enter number of small dogs: ");
    Small = Scanner.nextInt();
    System.out.println("Enter number of large dogs: ");
    Large = Scanner.nextInt();

    if ((Small + Large) <= 8) {
      System.out.println("Revenue is " + ((Small * x) + (Large * y)));
      System.out.println("Food = " + ((Small + Large) * (2)));
      System.out.println("Expenses = " + (Food + Facility));
      System.out.println("Difference = " + (Revenue - Expenses));

    } else
      System.out.println("The number of dogs has exceeded the facility limit.");
  }
}

您扫描 smalllarge 的值 AFTER 计算 ExpensesDifference 的值。尝试重新排序它们,如下所示:

import java.util.Scanner;

public class BarkingLot {
  public static void main(String[] args) {
    Scanner Scanner = new Scanner(System.in);
    int x = 20;
    int y = 25;
    int Small = 0;
    int Large = 0;

    System.out.println("Enter number of small dogs: ");
    Small = Scanner.nextInt();
    System.out.println("Enter number of large dogs: ");
    Large = Scanner.nextInt();

    int Revenue = ((Small * x) + (Large * y));
    int Food = ((Small + Large) * (2));
    int Facility = 30;
    int Expenses = (Food + Facility);
    int Difference = (Revenue - Expenses);

    if ((Small + Large) <= 8) {
      System.out.println("Revenue is " + ((Small * x) + (Large * y)));
      System.out.println("Food = " + ((Small + Large) * (2)));
      System.out.println("Expenses = " + (Food + Facility));
      System.out.println("Difference = " + (Revenue - Expenses));

    } else
      System.out.println("The number of dogs has exceeded the facility limit.");
  }
}

此外,java 中的变量名不应大写,以免与 类 混淆。您看到 Scanner CLASS 是如何被 Scanner 变量隐藏(使用相同的名称)的吗?

记住:类 - 大写。其他一切(方法、字段、变量),小写。

在您实际从用户那里获得这些值之前,您正在从变量 SmallLarge 初始化 Food

System.out.println("Enter number of small dogs: ");
Small = Scanner.nextInt();
System.out.println("Enter number of large dogs: ");
Large = Scanner.nextInt();
int Food = ((Small+Large)*2);

然后会正确设置 Food。对于您的打印输出,请确保您使用的是变量本身(即 Food),而不是重新计算变量代表什么(即 ((Small+Large)*2))。