我尝试使用不同的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);
}
答案 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个类Bill
和Calculate
。当您提示用户输入数据时,您将数据存储在类Bill
的局部变量中。现在,在用户输入所有数据后,您计算变量totalofusage
并存储在Bill
类totalofusage
变量中。但是这个变量与totalofusage
类的Calculate
变量不同。其中保持在0.0。因此,当您致电searchbill.calculateBill();
时,totalofusage
取自Calculate
类的本地变量0.0。因此,您的金额为0.0
。
要解决此问题,您必须将totalofusage
课程的Bill
中存储的金额转移到totalofusage
课程的Calculate
。这可以通过两种方式完成:
计算totalofusage
类中的Bill
后,将值设置为Calculate
类。像这样:
Calculate searchbill = new Calculate();
searchbill.totalofusage = totalofusage;
searchbill.calculateBill();
将参数作为参数传递给方法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
....
}