【问题标题】:How get the result of a boolean method in one class into an if statement of another class in Java?java - 如何将一个类中的布尔方法的结果转换为Java中另一个类的if语句?
【发布时间】:2017-11-13 21:43:36
【问题描述】:

我正在编写一个程序来收集员工的名字、姓氏和销售数据。我的所有基础都可以完美运行,但我有一个问题。在我的 while 循环中,我对其进行了编程,因此如果用户输入“添加”,那么它将调用我在不同类中创建的方法,并将他们输入的任何数字添加到员工当前的销售总额中,减法相同。但是如果我需要编写一个 if 语句来检查减法是否不成功以及是否显示“错误”。

 public class EmployeeInfo
 {
 //class constants

 //class variables
 private String firstName;
 private String lastName;
 private int itemsSold;
 private int employeeTotal;

public EmployeeInfo(String first, String last, int sold)
{

    //local constants

    //local variables

    /********************   Start Constructor  *****************/

    //Calls the first and last name, and the amount of items sold
    firstName = first;
    lastName = last;
    itemsSold = sold;

}//End constructor


 public void addition(int itemsSold)
   {
      //local constants

      //local variables


      /********************   Start main method  *****************/

      //Adds the items sold to the employee total
      employeeTotal = itemsSold + employeeTotal;

}//end addition


 public void subtraction(int itemsSold)
 {
       //local constants

       //local variables

       /********************   Start main method  *****************/

       //Subtracts the items not sold to the employee total
       employeeTotal = (employeeTotal - itemsSold);

}//end subtraction


public boolean booleanMethod(boolean result)
{
       //local constants

       //local variables

       /********************   Start main method  *****************/

       if(employeeTotal < 0)
       {
           result = false;
       }
       else
       {
           result = true;
       }

       return result;

 }//End booleanMethod

   public String toString()
    {
       //local constants

       //local variables


       /******************************************************/

       return String.format(Util.setLeft(45,"Eployee Name and Sales Figures") + "\n\n" + Util.setLeft(40, "First Name: ") + Util.setRight(30, firstName) +
       "\n" + Util.setLeft(40, "Last Name : ") + Util.setRight(30, lastName) + "\n" + Util.setLeft(40, "Items Sold: ") + Util.setRight(30, Integer.toString(employeeTotal)));

   }//end to string

}//end employeeInfo

这是使用 booleanMethod, 方法的类的一部分

while(soldItems != QUIT)
    {

        //Clear screen then prompt the user to add or subtract
        System.out.print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
        System.out.print(Util.setLeft(35, "Add or Subtract: "));
        input = Keyboard.readString();
        System.out.print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");

        //if the user enters add the program will add to the employee total
        if(input.equals(ADD))
        {
            info.addition(soldItems);

        }

        //else the program will subtract from the employee total
        else
        {
            info.subtraction(soldItems);

            if (info.booleanMethod(false))
            {
                System.out.print("Error");
                System.out.print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
            }
        }

该工具成功完成,但它不显示错误消息它只是显示我的行要求另一个销售数字,我如何让它实际工作?

【问题讨论】:

  • 让减法执行验证并返回运算结果boolean
  • 与其让实例处于无效状态,例如售出的商品数量为负数,不如让方法失败。例如,您的方法EmployeeInfo.subtraction() 可能会检查是否有足够的项目可供减去。如果是这样,它将执行减法并返回true;否则,它将返回 false 而无需执行任何其他操作。
  • @MadProgrammer 我已经试过了,但也没有用。
  • 在booleanMethod方法中,对total做一个sysout,看看它有什么值。

标签: java class methods boolean


【解决方案1】:

首先让subtraction 负责执行验证...

public boolean subtraction(int itemsSold)
{
    //local constants

    //local variables

    /********************   Start main method  *****************/

    //Subtracts the items not sold to the employee total
    employeeTotal = (employeeTotal - itemsSold);

    return employeeTotal < 0;
}//end subtraction

然后简单地使用返回结果来确定应该做什么......

if (!info.subtraction(soldItems)) {
    //...
}

恕我直言,subtraction 实际上应该在它达到可能的无效状态之前停止,例如...

public boolean subtraction(int itemsSold)
{
    //local constants

    //local variables

    /********************   Start main method  *****************/

    if (employeeTotal - itemsSold >= 0) {
        //Subtracts the items not sold to the employee total
        employeeTotal = (employeeTotal - itemsSold);
        return true; 
    } 

    return false;
}//end subtraction

【讨论】:

  • 成功了,所以减法没有通过,但我仍然需要显示错误消息。编辑:它想通了
  • if (!info.subtraction(soldItems)) { // Display error } - 如果操作无法执行,“建议”修复仍将返回 false
【解决方案2】:

把你的代码改成

if (!info.booleanMethod(false)) 

这意味着它是错误的。您当前的代码意味着

if(true)

实际上根本没有理由传入布尔值。布尔值属于 booleanMethod() 方法。

应该是这样的:

public boolean booleanMethod()
{


//local constants

   //local variables

   /********************   Start main method  *****************/

   if(employeeTotal < 0)
   {
       return false;
   }
   else
   {
       return  true;
   }

}

你应该这样称呼它:

if (!info.booleanMethod())

【讨论】:

    猜你喜欢
    • 2016-04-19
    • 2018-06-13
    • 2015-09-01
    • 1970-01-01
    • 2012-07-07
    • 2020-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多