Help With Java Program

Status
Not open for further replies.

novacaine98

Baseband Member
Messages
53
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) {
 
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.
 
novacaine98 said:
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?
 
Status
Not open for further replies.
Back
Top Bottom