I want to learn how to calculate electric meter | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

21st Oct 2021, 4:31 AM
Leoror
Leoror - avatar
10 Answers
+ 1
if u entered 50 then how many if else will execute u need to use && also
21st Oct 2021, 7:34 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 1
zemiak & Rik Wittkopp thank you for the help. Appreciate it!
21st Oct 2021, 8:37 AM
Leoror
Leoror - avatar
0
describe how you will solve it
21st Oct 2021, 6:00 AM
zemiak
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.
21st Oct 2021, 6:37 AM
Leoror
Leoror - avatar
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)); } } }
21st Oct 2021, 7:33 AM
zemiak
0
zemiak I will compare your code to mine to see my mistakes. Thank you!
21st Oct 2021, 7:44 AM
Leoror
Leoror - avatar
0
"my code" is your but formatted
21st Oct 2021, 7:47 AM
zemiak
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
21st Oct 2021, 7:54 AM
Rik Wittkopp
Rik Wittkopp - avatar
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
21st Oct 2021, 8:07 AM
zemiak
0
edit: unit can be long but in first print just print 150.00 without assign
21st Oct 2021, 8:17 AM
zemiak