英文:
Problem with output, the if statement is being executed when it should not
问题
import java.util.Scanner;
public class CE_Sandwich {
public static final double SAND_COST = 3.50;
public static final double SALAD_COST = 4.50;
public static final double PLATTER_COST = 6.50;
public static final double SAND_COST_X3 = 2.75;
public static final double SALAD_COST_X3 = 3.75;
public static final double PLATTER_COST_X3 = 5.75;
public static final int SANDWICH = 1;
public static final int SALADS = 2;
public static final int PLATTER = 3;
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("\t Welcome to HCC's Sandwich Shop");
System.out.println("What would you like? Type in the number listed before each item to order.");
System.out.println("(1)SANDWICHES - $3.50 for 1 or $2.75 when you buy 3 or more");
System.out.println("(2)SALADS - $4.50 for 1 or $3.75 when you buy 3 or more");
System.out.println("(3)PLATTERS - $6.50 for 1 or $5.75 when you buy 3 or more");
int count;
int input2;
int input = scan.nextInt();
do {
if (input == SANDWICH) {
do {
System.out.println("How many sandwiches?");
count = scan.nextInt();
if (count < 1) {
System.out.println("ERROR - VALUE CANNOT BE LESS THAN 1. PLEASE RE-ENTER");
} else if (count <= 2) {
double cost = count * SAND_COST;
System.out.printf("TOTAL COST OF ORDER IS $%.2f%n", cost);
} else {
double cost = count * SAND_COST_X3;
System.out.printf("TOTAL COST OF ORDER IS $%.2f%n", cost);
}
} while (count < 1);
}
if (input == SALADS) {
do {
System.out.println("How many salads?");
count = scan.nextInt();
if (count < 1) {
System.out.println("ERROR - VALUE CANNOT BE LESS THAN 1. PLEASE RE-ENTER");
} else if (count <= 2) {
double cost = count * SALAD_COST;
System.out.printf("TOTAL COST OF ORDER IS $%.2f%n", cost);
} else {
double cost = count * SALAD_COST_X3;
System.out.printf("TOTAL COST OF ORDER IS $%.2f%n", cost);
}
} while (count < 1);
}
if (input == PLATTER) {
do {
System.out.println("How many platters?");
count = scan.nextInt();
if (count < 1) {
System.out.println("ERROR - VALUE CANNOT BE LESS THAN 1. PLEASE RE-ENTER");
} else if (count <= 2) {
double cost = count * PLATTER_COST;
System.out.printf("TOTAL COST OF ORDER IS $%.2f%n", cost);
} else {
double cost = count * PLATTER_COST_X3;
System.out.printf("TOTAL COST OF ORDER IS $%.2f%n", cost);
}
} while (count < 1);
}
System.out.println("Do you wish to reorder? Press 1 for Yes or 0 for No");
input2 = scan.nextInt();
} while (input2 == 1);
if (input2 == 0) {
System.out.println("GOODBYE :)");
}
}
}
英文:
import java.util.Scanner;
public class CE_Sandwich {
public static final double SAND_COST = 3.50;
public static final double SALAD_COST = 4.50;
public static final double PLATTER_COST = 6.50;
public static final double SAND_COST_X3 = 2.75;
public static final double SALAD_COST_X3 = 3.75;
public static final double PLATTER_COST_X3 = 5.75;
public static final int SANDWICH = 1;
public static final int SALADS = 2;
public static final int PLATTER = 3;
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("\t Welcome to HCC's Sandwich Shop");
System.out.println("What would you like? Type in the number lsited before each item to order.");
System.out.println("(1)SANDWICHES - $3.50 for 1 or $2.75 when you buy 3 or more");
System.out.println("(2)SALADS - $4.50 for 1 or $3.75 when you buy 3 or more");
System.out.println("(3)PLATTERS - $6.50 for 1 or $5.75 when you buy 3 or more");
int count;
int input2;
int input = scan.nextInt();
do {
if (input == SANDWICH) {
do {
System.out.println("How many sandwiches?");
count = scan.nextInt();
if (count<1) {
System.out.println("ERROR - VALUE CANNOT BE LESS THAN 1. PLEASE RE-ENTER");
}
if (count<=2) {
double cost = count * SAND_COST;
System.out.printf("TOTAL COST OF ORDER IS $%.2f%n", cost);
}
if (count>=3) {
double cost = count * SAND_COST_X3;
System.out.printf("TOTAL COST OF ORDER IS $%.2f%n", cost);
}
}while (count <1);
}
if (input == SALADS) {
do {
System.out.println("How many salads?");
count = scan.nextInt();
if (count<1) {
System.out.println("ERROR - VALUE CANNOT BE LESS THAN 1. PLEASE RE-ENTER");
}
if (count<=2) {
double cost = count * SALAD_COST;
System.out.printf("TOTAL COST OF ORDER IS $%.2f%n", cost);
}
if (count>=3) {
double cost = count * SALAD_COST_X3;
System.out.printf("TOTAL COST OF ORDER IS $%.2f%n", cost);
}
}while (count <1);
}
if (input == PLATTER) {
do {
System.out.println("How many platters?");
count = scan.nextInt();
if (count<1) {
System.out.println("ERROR - VALUE CANNOT BE LESS THAN 1. PLEASE RE-ENTER");
}
if (count<=2) {
double cost = count * PLATTER_COST;
System.out.printf("TOTAL COST OF ORDER IS $%.2f%n", cost);
}
if (count>=3) {
double cost = count * PLATTER_COST_X3;
System.out.printf("TOTAL COST OF ORDER IS $%.2f%n", cost);
}
}while (count <1);
}
System.out.println("Do you wish to re order? Press 1 for Yes or 0 for No");
input2 = scan.nextInt();
}while (input2 == 1);
if (input2 == 0) {
System.out.println("GOODBYE :)");
}
}
}
When I run the program, the output whenever I input a value lower than 1 for count, is a error message, but it also multiplies it by the cost. What am i doing wrong???? Any help is appreciated. I have linked the output and basically when an error message is displayed it shouldnt do the calculations yet is does.
THE OUTPUT
答案1
得分: 2
如果一个值小于1,那么它也小于或等于2。
因此在类似这样的情况下:
如果 (count < 1)
xxxx;
如果 (count <= 2)
yyyy;
对于小于等于0的值,xxxx 和 yyyy 都将被执行。你可能是想要:
如果 (count < 1)
xxxx;
否则如果 (count <= 2)
yyyy;
希望通过这个提示,你能找出需要进行的更改。
英文:
If a value is less than 1, it's also less than or equal to 2.
So in something like this:
if (count < 1)
xxxx;
if (count <= 2)
yyyy;
for values 0 or less, both xxxx and yyyy will be executed. You probably mean
if (count < 1)
xxxx;
else if (count <= 2)
yyyy;
I hope that with that hint you can figure out the changes you need to make.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论