Java 自动假设方程指南

Java 自动假设方程指南,java,Java,在这段代码中,我试图使int“Small”在输入整数“Large”后自动假定。我为它设置的等式是:Small+Large必须=

在这段代码中,我试图使int“Small”在输入整数“Large”后自动假定。我为它设置的等式是:Small+Large必须=<8,所以当他们为“Large”输入5时,“Small”将输出3。代码确实编译成功,但不管我输入多少,输出都会自动显示“狗的数量超过了设施限制”。我的公式有问题吗?我不知道我做错了什么

这是我的代码:

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 Large = 0;
    int Small = (8 - Large);
    int quit = 0;

    while (quit == 0){
     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("Expenses = " + (Food + Facility));
      System.out.println("Difference = " + (Revenue - Expenses));

    } else
      System.out.println("The number of dogs has exceeded the facility limit.");
  }
}
}
import java.util.Scanner;
公共级停车场{
公共静态void main(字符串[]args){
扫描仪=新的扫描仪(System.in);
int x=20;
int y=25;
int-Large=0;
小整数=(8-大);
int-quit=0;
while(quit==0){
System.out.println(“输入大型狗的数量:”);
大=Scanner.nextInt();
国际收入=((小*x)+(大*y));
国际食品=((小+大)*(2));
国际设施=30;
国际费用=(食品+设施);
内部差异=(收入-费用);

if((Small+Large)问题是您将Small初始化为早期,因此在if语句中始终保持8,在if语句中检查Small+Large是否小于或等于8

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 Large = 0;
    int Small;
    int quit = 0;

    while (quit == 0){
     System.out.println("Enter number of large dogs: ");
    Large = Scanner.nextInt();
    // need to initialize Small here.
    Small = (8 - Large);

    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("Expenses = " + (Food + Facility));
      System.out.println("Difference = " + (Revenue - Expenses));

    } else
      System.out.println("The number of dogs has exceeded the facility limit.");
  }
}
}
import java.util.Scanner;
公共级停车场{
公共静态void main(字符串[]args){
扫描仪=新的扫描仪(System.in);
int x=20;
int y=25;
int-Large=0;
int小;
int-quit=0;
while(quit==0){
System.out.println(“输入大型狗的数量:”);
大=Scanner.nextInt();
//这里需要初始化小的。
小=(8-大);
国际收入=((小*x)+(大*y));
国际食品=((小+大)*(2));
国际设施=30;
国际费用=(食品+设施);
内部差异=(收入-费用);

如果((小+大)这是有意义的。因为如果我太早初始化它,那么它会识别出它的错误用法。谢谢!只是想知道,为什么你的代码会阻止我原始代码的其他部分,因为它现在不起作用?当你将小设置为(8-大)时然后再改变大小,否则它将始终满足条件(小+大)@CSCH!假设我输入数字9。我希望else结果仍然显示。我正在尝试修复它,但我仍然是一个noob。我以前的语句是错误的。实际上,因为在if语句中,您基本上拥有的是(小+大)多小会比大大?我用下面的个人代码修复了它。但它阻止了它使用我的else语句。你知道为什么吗?我读了你的问题,但没有理解你试图解决的问题?你可能需要解释你的逻辑或你试图实现什么?