Results 1 to 2 of 2
  1. #1
    zoe
    zoe is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default Help with if else statements

    Hello, I started writing java. I have the first and second steps done (well, i cannot get the program to loop if the user says yes, and I'm also having problems with the if/else statements)
    Here is my code:
    Java Code:
    public class bh {
    public static void main (String agrs[])
    {
    
    double salary, total, tax, taxes, money;
    int hours;
    char answer;
    
    System.out.print("Enter salary per hour: ");
    salary = DummiesIO.getDouble();
    System.out.print("Enter tax as a percent (eg. .30): ");
    tax = DummiesIO.getDouble();
    
    if (tax >= .0 && tax <= 1.0)
    System.out.print("Enter number hours worked: ");
    else
    System.out.print("Invalid Entry. Please enter a number between .0 and .90 : ");
    tax = DummiesIO.getDouble();
    
    System.out.print("Enter tax as a percent (eg. .30): ");
    tax = DummiesIO.getDouble();
    {
    if (tax >= .0 && tax <= 1.0)
    {
    total = (salary * tax);
    taxes = (salary - total);
    money = (taxes * hours);
    
    System.out.print("You have made: $ ");
    System.out.print(money);
    }
    else
    System.out.print("Invalid Entry. Please enter a number between .0 and .90 : ");
    tax = DummiesIO.getDouble();
    
    }
    
    /*System.out.print("Do you want to calculate more hours? (Yes / No): ");
    reply = DummiesIO.getChar();
    
    if (reply = "Yes")
    {
    System.out.print("Enter salary per hour: ");
    salary = DummiesIO.getDouble();
    System.out.print("Enter tax as a percent (eg. .30): ");
    tax = DummiesIO.getDouble();
    System.out.print("Enter number hours worked: ");
    hours = DummiesIO.getInt();
    System.out.print("Enter tax as a percent (eg. .30): ");
    tax = DummiesIO.getDouble();
    
    total = (salary * tax);
    taxes = (salary - total);
    money = (taxes * hours);
    
    System.out.print("You made : $");
    System.out.println(money);
    }
    else
    {
    System.out.print("Thank You. Bye ");
    
    }*/
    
    }
    
    }
    The error message i get is with this line :
    Java Code:
    money = (taxes * hours);
    and the error message states :
    Java Code:
    nub:~/Desktop/JavaStuff Josh$ javac bh.java
    bh.java:42: variable hours might not have been initialized
    money = (taxes * hours);
    ^
    1 error
    Please help. Thanks

  2. #2
    cruxblack is offline Senior Member
    Join Date
    Jul 2007
    Posts
    130
    Rep Power
    0

    Default

    U haven't put an initial value for hours, maybe u forgot to put an input for it?
    Nothing wrong with ur if else, just, u didn't put any input for the hours or any initial value, maybe that's why the error came :D

Similar Threads

  1. avoiding if statements
    By valoyivd in forum New To Java
    Replies: 1
    Last Post: 04-02-2008, 10:08 AM
  2. Help with actionPerformed Statements
    By wco5002 in forum New To Java
    Replies: 8
    Last Post: 03-26-2008, 05:02 AM
  3. Paper,Scissor,Rock If then Statements
    By Alberto in forum New To Java
    Replies: 2
    Last Post: 02-12-2008, 12:18 AM
  4. Replies: 0
    Last Post: 09-28-2007, 01:59 PM
  5. Problems with packages (import statements)
    By ai_2007 in forum Advanced Java
    Replies: 1
    Last Post: 06-29-2007, 12:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •