-
A little help!
The problems that I am having is that I can not get my items in
the switch statment to add up and give me a total at the end of
the program. No matter how many item numbers I enter the result
is always the same. also when I enter the sentinal value to let
the program know that I want to end the program and add up all
the values, it says that I have entered an invalid number and
when I enter a number other than 1,2,3,or 4, it does'nt do
that like I want it to. I am having a hard time with this
and I would really appriciate it greatly if anyone could give
me some tips. I have spent too long on this problem and I am
about to go crazy.
import javax.swing.JOptionPane;
public class salary2 {
public static void main ( String args [] ){
int inputNumber = 0,
item = 0,
basePay = 200;
String inputItem;
double sum;
while ( inputNumber != -1 ) {
item = item + 1;
inputItem = JOptionPane.showInputDialog(null, " Please enter Item
number 1, 2, 3, or 4,\n -1 when done: ");
inputNumber = Integer.parseInt( inputItem );
}
switch ( inputNumber ) {
case 1:
sum = 239.99;
break;
case 2:
sum = 129.75;
break;
case 3:
sum = 99.95;
break;
case 4:
sum = 350.89;
break;
default:
JOptionPane.showMessageDialog( null, " INVALID number
entered!" );
}
sum = inputNumber * .09 + basePay ;
JOptionPane.showMessageDialog(null, "Total earnings are: " + sum,
"Total earnings",
JOptionPane.INFORMATION_MESSAGE );
System.exit (0);
}
}
-
Re: A little help!
Hi,
The code works perfectly...
Check your code.. you are assiging different values to sum but you are overwriting
it immediately...
Check it out!
Regards
Rajkamal
"Eric " <Wdiv@hotmail.com> wrote:
>
>The problems that I am having is that I can not get my items in
>the switch statment to add up and give me a total at the end of
>the program. No matter how many item numbers I enter the result
>is always the same. also when I enter the sentinal value to let
>the program know that I want to end the program and add up all
>the values, it says that I have entered an invalid number and
>when I enter a number other than 1,2,3,or 4, it does'nt do
>that like I want it to. I am having a hard time with this
>and I would really appriciate it greatly if anyone could give
>me some tips. I have spent too long on this problem and I am
>about to go crazy.
>
>
>
>
>import javax.swing.JOptionPane;
>
>
>public class salary2 {
> public static void main ( String args [] ){
>
>
> int inputNumber = 0,
> item = 0,
> basePay = 200;
>
> String inputItem;
>
>
> double sum;
>
>
> while ( inputNumber != -1 ) {
> item = item + 1;
>
>
> inputItem = JOptionPane.showInputDialog(null, " Please enter Item
>
> number 1, 2, 3, or 4,\n -1 when done: ");
>
>
> inputNumber = Integer.parseInt( inputItem );
>
> }
>
> switch ( inputNumber ) {
> case 1:
> sum = 239.99;
> break;
> case 2:
> sum = 129.75;
> break;
> case 3:
> sum = 99.95;
> break;
> case 4:
> sum = 350.89;
> break;
> default:
> JOptionPane.showMessageDialog( null, " INVALID number
>
> entered!" );
>
> }
>
> sum = inputNumber * .09 + basePay ;
>
>
> JOptionPane.showMessageDialog(null, "Total earnings are: " + sum,
>"Total earnings",
> JOptionPane.INFORMATION_MESSAGE );
>
>
> System.exit (0);
>}
>}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks