关于自动假设方程的指导

时间:2015-04-19 05:50:37

标签: java

在这段代码中,我试图让它成为int" Small"在键入整数" Large之后自动假设。"我为它设定的等式是:小+大必须=< 8,所以当他们输入5为" Large",然后" 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.");
  }
}
}

1 个答案:

答案 0 :(得分:0)

问题是你正在初始化Small到早期,所以它一直保持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.");
  }
}
}