+ 1
I want to learn how to calculate electric meter
I want to code a java program that computes my electric bill...I tried coding it a few times but I always fail and I end up deleting my code out of frustration..can you help me? (I was answering some tasks online and I always fail) The output goes like this: Enter the rate of consumption (kWh): 324 Total cost = P262.00 Cost: 0 to 100 kWh - P150.00 101 to 500 - P150.00 + 0.5 for each kWh over 100 over 500 - P350.00 + 0.3 for each kWh above 500 https://code.sololearn.com/c3gKQzK6TrKM/?ref=app
10 Answers
+ 1
if u entered 50 then how many if else will execute u need to use && also
+ 1
zemiak & Rik Wittkopp thank you for the help. Appreciate it!
0
describe how you will solve it
0
Hi zemiak the program should ask for the rate of the consumption and I will input the rate. Then after that, it would compute the rate that I inputted.
For example,
Enter the rate of consumption in kWh: (I will input a rate 324)
Since 324 is over 100 kWh of consumption, the total amount that would be paid is 150.00 but 0.5 would be added for every over 100 kWh of consumption.
//I myself don't really get the equation. So sorry if my explanation is a mess.
0
I see you attached your code,
keep right indent for better readable
correct this:
after last else delete }
count () in last two prints
where you get input rename scan to sc
in first print just print 150.00 without assign
import java.util.*;
class Main {
public static void main(String args[]) {
long unit;
double bill_Amount = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of units: ");
unit = scan.nextLong();
if (unit <= 100) {
System.out.print("Bill amount is P" + (unit = 150.00));
} else if (unit <= 500) {
System.out.print("Bill amount is P" + ((150.00) + (unit * 0.5)));
} else if (unit > 500) {
System.out.print("Bill amount is P" + ((350.00) + (unit * 0.3));
} else
System.out.print("Bill amount is P" + ((450.00) + (unit * 0.2));
}
}
}
0
zemiak I will compare your code to mine to see my mistakes. Thank you!
0
"my code" is your but formatted
0
Leoror
I had a play with your code and placed comments on errors.
Please review the attached code, save it for yourself as I will delete in 24 hrs
https://code.sololearn.com/cbLdDXSzfZel/?ref=app
0
if you want write it simpler, after if() assign computed value to variable and in the end of code do one print with this value
0
edit: unit can be long but in first print just print 150.00 without assign