运行程序时出现逻辑错误

时间:2015-05-05 13:08:42

标签: java

我尝试使用不同的totalofusage值运行程序。但是,金额始终为0.0。请帮忙。

package bill;

import java.util.Scanner;

public class Bill {

    public static void main(String[] args) {

        String Name, Address;
        int accountnumber, currentmeterreading, pastmeterreading;
        double totalofusage;
        Scanner read = new Scanner (System.in);


        System.out.println ("Please enter your account number:");
        accountnumber = read.nextInt();
        read.nextLine();

        System.out.println ("Please enter your name:");
        Name = read.nextLine();

        System.out.println ("Please enter your address");
        Address = read.nextLine();

        System.out.println ("Please enter the current meter reading:");
        currentmeterreading = read.nextInt();

        System.out.println ("Please enter past meter reading");
        pastmeterreading = read.nextInt();


        totalofusage = currentmeterreading - pastmeterreading;

        System.out.println ("Account number:"+ accountnumber);
        System.out.println ("Current meter reading:"+ currentmeterreading);
        System.out.println ("Past meter reading:"+ pastmeterreading);
        System.out.println ("Name:"+ Name);
        System.out.println ("Address:"+ Address);
        System.out.println ("Usage:" + totalofusage);

        Calculate searchbill = new Calculate();
        searchbill.calculateBill();

}}


package bill;

public class Calculate { 

double  totalofusage, amount;




    public void calculateBill (){

        if ((totalofusage > 0) && (totalofusage <= 200))
            amount = totalofusage * 0.218;
        else if ((totalofusage > 200) && (totalofusage <=300))
            amount = totalofusage * 0.334;
        else if ((totalofusage >300) && (totalofusage <= 400))
            amount = totalofusage * 0.400;
        else 
            amount = totalofusage * 0.402;

        System.out.println ("Amount ="+ amount);
    }

3 个答案:

答案 0 :(得分:2)

Calculate类更改为:

public class Calculate {
  public void calculateBill (double totalofusage) {
    double amount;

    if ((totalofusage > 0) && (totalofusage <= 200))
        amount = totalofusage * 0.218;
    else if ((totalofusage > 200) && (totalofusage <=300))
        amount = totalofusage * 0.334;
    else if ((totalofusage >300) && (totalofusage <= 400))
       amount = totalofusage * 0.400;
    else 
       amount = totalofusage * 0.402;

    System.out.println ("Amount ="+ amount);
}

然后这样称呼它:

searchbill.calculateBill(totalofusage);

答案 1 :(得分:0)

public class Calculate {

double totalofusage, amount;

public void calculateBill (){

    if ((totalofusage > 0) && (totalofusage <= 200))
        amount = totalofusage * 0.218;
    else if ((totalofusage > 200) && (totalofusage <=300))
        amount = totalofusage * 0.334;
    else if ((totalofusage >300) && (totalofusage <= 400))
        amount = totalofusage * 0.400;
    else 
        amount = totalofusage * 0.402;

    System.out.println ("Amount ="+ amount);
}

这里有一个实例变量totalofusage,其默认值为0。 所以,无论你多少次调用这种计算方法,它总会最终做:

amount = totalofusage * 0.402;

0 * 0.402确实为0.

答案 2 :(得分:0)

您的计划中基本上存在一个问题。您正在使用2个类BillCalculate。当您提示用户输入数据时,您将数据存储在类Bill的局部变量中。现在,在用户输入所有数据后,您计算变量totalofusage并存储在Billtotalofusage变量中。但是这个变量与totalofusage类的Calculate变量不同。其中保持在0.0。因此,当您致电searchbill.calculateBill();时,totalofusage取自Calculate类的本地变量0.0。因此,您的金额为0.0

要解决此问题,您必须将totalofusage课程的Bill中存储的金额转移到totalofusage课程的Calculate。这可以通过两种方式完成:

  1. 计算totalofusage类中的Bill后,将值设置为Calculate类。像这样:

      Calculate searchbill = new Calculate();
      searchbill.totalofusage = totalofusage;
      searchbill.calculateBill();
    
  2. 将参数作为参数传递给方法calculateBill()并正确更改calculateBill方法,如下所示:

      //IN CLASS Bill
      Calculate searchbill = new Calculate();
      searchbill.calculateBill(totalofusage);
    
      //IN CLASS Calculate
      public void calculateBill (double totalofusage){
         this.totalofusage = totalofusage
         //Remaining code
         ....
      }