Thread: Help with loop

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    4
    Rep Power
    0

    Help with loop


    Hi i have the code below:


    import java.util.*;

    public class lab3extra
    {
    public static void main (String[] args)
    {
    Scanner console = new Scanner(System.in);
    double totalSalary = 0, hours = 0, salary;
    int week = 1;

    while(week <=5)
    {
    System.out.println("Please print hours worked " +week);
    hours = console.nextInt();
    if (hours < 0)
    {
    System.out.println("Invaild Input");
    System.exit(0);
    }
    if (hours >40)
    {
    salary = hours * 15 -200;
    }
    else
    {
    salary = hours *10;
    }
    week++;

    totalSalary = totalSalary + salary;



    if (totalSalary <= 1000){
    totalSalary = totalSalary * 1.10;
    }
    else
    if(totalSalary > 1000 && totalSalary <=2000){
    totalSalary = totalSalary * 1.05;
    }
    else
    if (totalSalary > 2000 && totalSalary <= 3000){
    totalSalary = totalSalary * 1.01;
    }


    }
    System.out.println("Total number is: "+totalSalary);
    }
    }

    and i would like to stop the loop after
    totalSalary = totalSalary + salary;

    and then use the totalSalary on the next loop, how do i go about this?
    thanks
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    Please edit the post and wrap the code in code tags to make it easier to read and understand.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    4
    Rep Power
    0
    Code:
    import java.util.*;
    
    public class lab3extra
    {
           public static void main (String[] args)
    {
           Scanner console = new Scanner(System.in);
           double totalSalary = 0, hours = 0, salary;
           int week = 1;
    
          while(week <=5)
                       {
              System.out.println("Please print hours worked " +week);
              hours = console.nextInt();
                if (hours < 0)
                                {
               System.out.println("Invaild Input");
               System.exit(0);
                }	
                if (hours >40)
                {
                salary = hours * 15 -200;
                 }
                else
                {
                salary = hours *10;
                 }
                week++;
    
                totalSalary = totalSalary + salary;
    
    
    
          if (totalSalary <= 1000){
          totalSalary = totalSalary * 1.10;
           }
           else
                 if(totalSalary > 1000 && totalSalary <=2000){
                 totalSalary = totalSalary * 1.05;
                  }
                  else 
                        if (totalSalary > 2000 && totalSalary <= 3000){
                        totalSalary = totalSalary * 1.01;
                          }
    
    
          }
          System.out.println("Total number is: "+totalSalary);
       }
    }
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    would like to stop the loop after
    Not sure what you are asking. To stop a loop, exit the loop by using the break statement or change a variable that is controlling the looping.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2005
    Posts
    74
    Rep Power
    33
    and i would like to stop the loop after
    totalSalary = totalSalary + salary;
    As NormR said, to exit the loop, use
    Code:
    break;
    or make the condition false. In this case, to be false your 'week' variable should be >5.

    and then use the totalSalary on the next loop, how do i go about this?
    You only have one loop, the while. I'm not sure what you mean.

IMN logo majestic logo threadwatch logo seochat tools logo