0

这是存储为 Registration.java 的注册类。

public class Registration {
    int registrationId;
    double fees;
    int marks;
public void setRegistrationId(int registrationId){
    this.registrationId=registrationId;
}
public int getRegistrationId(){
    return registrationId;
}
public void setFees(double fees){
    this.fees=fees;
}
public double getFees(){
    return fees;
}
public void calculateFees(int marks){
    int discount=0;
    if(marks>=85 && marks<=100)
        discount = 12;
    else if(marks>=75 && marks<=84)
        discount = 7;
    else if(marks>=65 && marks<=74)
        discount = 0;
    else
        System.out.println("You have not passed!");
    fees = fees-(fees*(discount/100.0));
    System.out.println("The discount availed is "+discount+" % and the fees after discount is: "+fees);
}
}

另一个名为 DemoReg1.java 的类承载 starter 方法。

public class DemoReg1 {

  public static void main(String[] args) {
    int branchList[]={1001,1002,1003,1004,1005};
    double fees[]= {25575,15500,33750,8350,20500};      
    Registration reg = new Registration();
    reg.setRegistrationId(2001);
    int branchId=1002;      
    double fees1=0;
    for(int i=0;i<branchList.length;i++)
    {
        if(branchList[i]==branchId)
        { 
            fees1=fees[i];
            reg.setFees(fees1);
            System.out.println("The Registration ID is:"+reg.getRegistrationId());
            System.out.println("The fees before discount is:"+reg.getFees());
            reg.calculateFees(79);
            break;
        } 
       }
      }
    }

我得到的输出是:

The Registration ID is:2001
The fees before discount is:15500.0
The discount availed is 7 % and the fees after discount is: 14415.0

我想添加一个“else”条件,当提供的 branchId(局部变量)为 1006 时,它会打印出以下语句。

System.out.println("Invalid branch ID");

我应该在代码的哪里添加上述语句?谢谢!

4

3 回答 3

6

如果找到分支,则引入一个设置为 true 的布尔变量,并在循环后检查该布尔变量的值:

boolean branchFound = false;
for(int i=0;i<branchList.length;i++)
{
    if(branchList[i]==branchId)
    { 
        branchFound = true;
        fees1=fees[i];
        reg.setFees(fees1);
        System.out.println("The Registration ID is:"+reg.getRegistrationId());
        System.out.println("The fees before discount is:"+reg.getFees());
        reg.calculateFees(79);
        break;
    } 
   }
  }
}
if (!branchFound) {
    System.out.println("Invalid branch ID");
}

您可能应该引入一个BranchIdAndFee包含 branchId 和相关费用的类 ( ),而不是使用并行数组。而且您还应该使用 aMap<Integer, BranchIdAndFee>能够在恒定时间内从分支 ID 获取信息,而不必循环。

于 2013-05-11T13:30:29.533 回答
3

而不是使用else子句,只需设置一个boolean标志,并在循环后检查它的值。如果是真的,那么你知道找到了一个有效的分支。不需要错误消息。否则,您必须输出错误消息。

boolean isValidBranch = false;
// Create a flag.    

for(int i=0;i<branchList.length;i++)
{
    if(branchList[i]==branchId)
    { 
        isValidBranch = true;
        fees1=fees[i];
        reg.setFees(fees1);
        System.out.println("The Registration ID is:"+reg.getRegistrationId());
        System.out.println("The fees before discount is:"+reg.getFees());
        reg.calculateFees(79);
        break;
    } 
   }

然后只需检查标志是否已设置为 true:

 if(!isValidBranch)
 {
     // Print out your error message.
 }
于 2013-05-11T13:29:58.067 回答
0

在 for 循环之后,您可以测试提供的分支 ID 是否有效,例如:

int ii;

for (ii = 0 ; ii < branchList.length; ii++)
{
  if (branchList[ii] == branchId)
  {
    // do your calculations
    break;
  }
}

if (ii == branchList.length)
{
  printf("Error: supplied branch ID %d was invalid\n", branchId);
}
于 2013-05-11T13:33:32.610 回答