import java.io.*;
import java.util.*;
class ElectricityBill {
int num;
String cname;
int units;
double amount = 0;
void setData(int num, String cname, int units) {
Scanner in = new Scanner(System.in);
cname = in.nextLine();
num = in.nextInt();
units = in.nextInt();
}
void billCalculate() {
if (units < 100) {
amount = units * 1.2;
} else if (units >= 100 && units <= 300) {
amount = (100 * 1.2) + ((units - 100) * 2);
} else if(units > 300) {
amount = (100 * 1.2) + (200 * 2) + ((units - 300) * 3);
}
}
void show() {
System.out.println("Customer Number:" + num);
System.out.println("Customer Name:" + cname);
System.out.println("Units Consumed:" + units);
System.out.println("Bill to pay:" + amount);
}
}
public class TestClass {
public static void main(String[] args) {
ElectricityBill b = new ElectricityBill();
b.setData(b.num, b.cname, b.units);
b.billCalculate();
b.show();
}
}
答案 0 :(得分:1)
看看:
void setData(int num, String cname, int units)
{
Scanner in = new Scanner(System.in);
cname = in.nextLine();
num = in.nextInt();
units = in.nextInt();
}
您覆盖了方法参数。
尝试
void setData(int num, String cname, int units)
{
this.num = num;
this.cname = cname;
this.units = units;
}
以及
public static void main(String[] args)
{
ElectricityBill b = new ElectricityBill();
Scanner in = new Scanner(System.in);
b.setData(in.nextInt(), in.nextLine(), in.nextInt());
b.billCalculate();
b.show();
}