Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
April 8th, 2013, 06:03 AM
 mathkid182
Registered User

Join Date: Apr 2013
Posts: 4
Time spent in forums: 53 m 13 sec
Reputation Power: 0
Homework - 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;

thanks

#2
April 8th, 2013, 07:40 AM
 NormR
Contributing User

Join Date: Aug 2010
Location: SW Missouri
Posts: 3,583
Time spent in forums: 2 Weeks 3 Days 3 h 2 m 3 sec
Reputation Power: 347
Please edit the post and wrap the code in code tags to make it easier to read and understand.

#3
April 8th, 2013, 07:45 AM
 mathkid182
Registered User

Join Date: Apr 2013
Posts: 4
Time spent in forums: 53 m 13 sec
Reputation 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);
}
}```

#4
April 8th, 2013, 09:42 AM
 NormR
Contributing User

Join Date: Aug 2010
Location: SW Missouri
Posts: 3,583
Time spent in forums: 2 Weeks 3 Days 3 h 2 m 3 sec
Reputation Power: 347
Quote:
 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.

#5
April 8th, 2013, 04:25 PM
 doa24uk
Contributing User

Join Date: Aug 2005
Posts: 74
Time spent in forums: 17 h 30 m 43 sec
Reputation Power: 32
Quote:
 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.

Quote:

You only have one loop, the while. I'm not sure what you mean.

 Viewing: Dev Shed Forums > Programming Languages > Java Help > Homework - Help with loop