Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 09-19-2006, 01:59 PM   #1 (permalink)
 
Junior Techie

Join Date: Jul 2005

Posts: 53

novacaine98

Default Help With Java Program

I have to write a program in Java and it is due by 4:00pm eastern time, today, September 19. Here's what I have to do.

Write a program that asks the user to enter the number of packages purchased. The program should then display the amount of the discount (if any) and the total amount of the purchase after the discount.

Use if...else statements only for the decision logic.
The user should input information in dialog boxes.
The output should be to the user in a dialog box.

Quantity Discount
0-9 None
10-19 20%
20-49 30%
50-99 40%
100+ 50%

Here's what I have now.

public class Homwork3 {
public static void main ( String [] args )
{
int packages;
String packages = JOptionPane.showInputDialog( "Enter number of packages purchased.");
If (packages >=0 and <=9) {
discount = packages - (packages * 0);
System.out.println("This is the discount for a quantity of zero to nine packages purchased.");
JOption.Pane.showMessageDialog(null, output);
}
else
If (packages >=10 and <=19) {
discount = packages - (packages * .20);
System.out.println("This is the discount for a quantity of ten to nineteen packages purchased.");
JOption.Pane.showMessageDialog(null, ouput);
}
else
If (packages >=20 and <=49) {
discount = packages - (packages * .30);
System.out.println("This is the discount for a quantity of twenty to forty-nine packages purchased.");
JOption.Pane.showMessageDialog(null, ouput);
}
else
If (packages >=50 and <=99) {
discount = packages - (packages * .40);
System.out.println("This is the discount for a quantity of fifty to ninety-nine packages purchased.");
JOption.Pane.showMessageDialog(null, ouput);
}
else
If (packages >=100) {
discount = packages - (packages * .50);
System.out.println("This is the discount for a quantity of one hundred or more packages purchased.");
JOption.Pane.showMessageDialog(null, ouput);
}
}
}

I ran the compiler and it says ')' expected on line 12, which is If (packages >=0 and <=9) {
novacaine98 is offline  
Old 09-19-2006, 03:35 PM   #2 (permalink)
 
Software Developer

Join Date: Mar 2006

Location: Columbus, OH

Posts: 569

jaeusm is on a distinguished road

Default

1) Each of your 'if' statements should be lower case letters only.
2) Typically, 'else if' statements are written on one line.
3) "and" is not a keyword in Java -- it is the reason for your error message. Instead of using the english word for 'and', this is the symbol you should use: &&. So your code should look like this:

Code:
if (packages >= 0 && packages <= 9)
Notice: you must use the actual variable name for each comparison.

Code:
// this is wrong and will not compile!!
if (packages >= 0 && <= 9)

// this is correct
if (packages >= 0 && packages <= 9)
There could be more errors, but these I caught at a quick glance.
jaeusm is offline  
Old 09-24-2006, 03:00 PM   #3 (permalink)
 
True Techie

Join Date: Sep 2006

Posts: 168

kwokwai

Default Re: Help With Java Program

Quote:
Originally posted by novacaine98
I have to write a program in Java and it is due by 4:00pm eastern time, today, September 19. Here's what I have to do.

Write a program that asks the user to enter the number of packages purchased. The program should then display the amount of the discount (if any) and the total amount of the purchase after the discount.

Use if...else statements only for the decision logic.
The user should input information in dialog boxes.
The output should be to the user in a dialog box.

Quantity Discount
0-9 None
10-19 20%
20-49 30%
50-99 40%
100+ 50%

Here's what I have now.

public class Homwork3 {
public static void main ( String [] args )
{
int packages;
String packages = JOptionPane.showInputDialog( "Enter number of packages purchased.");
If (packages >=0 and <=9) {
discount = packages - (packages * 0);
System.out.println("This is the discount for a quantity of zero to nine packages purchased.");
JOption.Pane.showMessageDialog(null, output);
}
else
If (packages >=10 and <=19) {
discount = packages - (packages * .20);
System.out.println("This is the discount for a quantity of ten to nineteen packages purchased.");
JOption.Pane.showMessageDialog(null, ouput);
}
else
If (packages >=20 and <=49) {
discount = packages - (packages * .30);
System.out.println("This is the discount for a quantity of twenty to forty-nine packages purchased.");
JOption.Pane.showMessageDialog(null, ouput);
}
else
If (packages >=50 and <=99) {
discount = packages - (packages * .40);
System.out.println("This is the discount for a quantity of fifty to ninety-nine packages purchased.");
JOption.Pane.showMessageDialog(null, ouput);
}
else
If (packages >=100) {
discount = packages - (packages * .50);
System.out.println("This is the discount for a quantity of one hundred or more packages purchased.");
JOption.Pane.showMessageDialog(null, ouput);
}
}
}

I ran the compiler and it says ')' expected on line 12, which is If (packages >=0 and <=9) {
I have no idea where line 12 is located in your script.
Are you using JBuilder?
kwokwai is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On