I have just begun using the for loop. Not sure how to use it in my program. Any help is appreciated.
Code:
import java.text.DecimalFormat; // for decimal formatted output
import java.util.Scanner; // package needed for GUI
public class ElectricPower
{
public static void main(String[] args)
{
DecimalFormat num = new DecimalFormat("$,###.00");
// variables
String Company; // to hold Company input
String str; // to hold username
int AccountNumber; // to hold AccountNumber
double kwhUsed;
double OldMeterReading; // string for inputting OldMeterReading
double NewMeterReading; // string for inputting NewMeterReading
double AmountDue;
double charge = 0.0;
Scanner inputDevice = new Scanner(System.in); // initialize scanner
for( str =1; str < 11; str++) // for loop to input as many customers as desired
str = 1;
while(str < 11)
{
System.out.println(str);
++str;
}
System.out.println("State your company name: "); // Company name
Company = inputDevice.nextLine();
System.out.println("Please enter customer name: "); // CustomerName
str = inputDevice.nextLine();
System.out.println("Enter account number: "); // AccountNumber
AccountNumber = inputDevice.nextInt();
System.out.println("What is your old meter reading? "); // OldMeterReading);
OldMeterReading = inputDevice.nextDouble();
System.out.println("What is your new meter reading? "); // NewMeterReading);
NewMeterReading = inputDevice.nextDouble();
// calculate the current electricity usage
kwhUsed = (NewMeterReading - OldMeterReading);
// computations
if(kwhUsed <= 300)
{
charge = 5.00;
}
else if(kwhUsed <= 1000)
{
charge = 5.00 +(0.03*(kwhUsed - 300));
}
else
{
charge = 35.00+(0.02*(kwhUsed - 1000));
}
AmountDue = charge; // calculate the bill
System.out.println("Company name is: " + Company); // company name
System.out.println("Customer name is: " + str); // customer
System.out.println("Account number is: " + AccountNumber); // account number
System.out.println("Old Meter Reading is: " + OldMeterReading); // old meter rating
System.out.println("New Meter Reading is: " + NewMeterReading); // new meter rating
System.out.println("Kilowatts per hour used is: " + kwhUsed); // kilowatts per hour
System.out.println("Amount Due is: " + num.format(AmountDue)); // amount due
System.out.println(); //Prints a blank line.
System.out.println(i);
++i;
}
}
Last edited by a moderator: