我正在编写一个收集员工的名字,姓氏和销售数字的程序。我的所有基础工作都很完美,但我有一个问题。在我的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");
}
}
该工具成功完成,但它没有显示错误消息,它只是显示我的行要求另一个销售数字,我如何让它实际工作?
答案 0 :(得分: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
答案 1 :(得分:0)
将您的代码更改为
if (!info.booleanMethod(false))
这意味着它是假的。您当前的代码意味着
if(true)
实际上根本没有理由传入布尔值。 boolean属于booleanMethod()方法。
它应该是这样的:
public boolean booleanMethod()
{
//local constants
//local variables
/******************** Start main method *****************/
if(employeeTotal < 0)
{
return false;
}
else
{
return true;
}
}
你应该这样称呼它:
if (!info.booleanMethod())