hi, so im new to java, fresh off the c++ shores (what a nightmare programing is :p) and i have what should be a really simple assignment

1. The electicity accounts of residents in a very small town are calculated as follows:

* If 500 units or less are used the cost is 2 cents per unit;
* If more than 500, but not more than 1000 units are used , the cost is $10 for the first 500 units , and then 5 cents for every unit in excess of 500;
* If more than 1000 units are used, the cost is $35 for the first 1000 units plus 10 cents for every unit in excess of 1000.
* In addition, a basic service fee of $5 is charged, no matter how much eletricity is used.


Write program which reads any five consumptions from keaboard and uses for loop to calculate and display the total charge for each one. For example
If data is 200 500 700 1000 1500, the answers will be $9, $15, $25, $40,$90.


so i coded it out and all, but the math just doesnt make sense, like its not adding up to what it should be -_-

i thought maybe it had something to do with my 3 if statements, but when i tried doing else if or just plain else eclipse yelled at me, is there some hidden java secret im missing?

import java.util.Scanner;
public class Homework1
{

	public Homework1() 
	{

	}

	public static void main(String[] args) 
	{
	
		double v1, total;
		Scanner keyboard = new Scanner(System.in);
		
		for(int i = 0; i < 5; i++)
		{
			System.out.println("Enter the number of units of electricity used");
			v1 = keyboard.nextInt();
			
				if (v1 < 500);
					total = ((v1 * .02) + 5);
				if (v1 > 500);
					total = (v1 - 500) * .05 + 15;
				if (v1 > 1000);
					total = (v1 - 1000) * .10 + 40;
					
				System.out.print("Your total is: ");
				System.out.println(total);
		}
	}
}

Semicolon ( ; ) separates commands. When you write this:

if (v1 < 500);
total = ((v1 * .02) + 5);

You have ONE if statement, that executes nothing because of the semicolon. It doesn't matter if it returns true or false. It "terminates" at the ';'
Then the second command will be executed always.
The same think happens with the rest.

Right approach:

if (v1<500)
  total = ((v1 * .02) + 5);

Better approach:

if (v1<500) {
  total = ((v1 * .02) + 5);
}

Even Better approach:

if (v1<500) {
  ....
} else if (v1 > 500) {
  ....
} else if (v1 > 1000) {
   ....
}

Right Approach:
The requirements say:

If more than 500, but not more than 1000 units are used

So the second if should be: ( (v1 > 500) && (v1 < 1000) ) Also you must have some of the '<' be '<='. If you leave them all '<' and the user enters 500 or 1000, none of the ifs will be true and nothing will be executed.
Also you must add in the end after the if the basic service fee of $5

i see now, i feel like an idiot, a whole summer off from programming made me forget even the mots basic things haha this semester will be fun...

thank you very much

oh and before i look too dumb haha i had included the 5 dollar charge in the base costs -_-

oh and before i look too dumb haha i had included the 5 dollar charge in the base costs -_-

Don't worry about looking dumb when learning a language. Worry about getting the basics, because they're essential down the road.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.