输出有问题,if语句在不应执行时被执行。

huangapple go评论77阅读模式
英文:

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(&quot;\t Welcome to HCC&#39;s Sandwich Shop&quot;);
System.out.println(&quot;What would you like? Type in the number lsited before each item to order.&quot;);
System.out.println(&quot;(1)SANDWICHES - $3.50 for 1 or $2.75 when you buy 3 or more&quot;);
System.out.println(&quot;(2)SALADS - $4.50 for 1 or $3.75 when you buy 3 or more&quot;);
System.out.println(&quot;(3)PLATTERS - $6.50 for 1 or $5.75 when you buy 3 or more&quot;);
int count;
int input2;
int input = scan.nextInt();
do {
if (input == SANDWICH) {
do {
System.out.println(&quot;How many sandwiches?&quot;);
count = scan.nextInt();
if (count&lt;1) {				 			
System.out.println(&quot;ERROR - VALUE CANNOT BE LESS THAN 1. PLEASE RE-ENTER&quot;);
}
if (count&lt;=2) {
double cost = count * SAND_COST;
System.out.printf(&quot;TOTAL COST OF ORDER IS $%.2f%n&quot;, cost);
}
if (count&gt;=3) {
double cost = count * SAND_COST_X3;
System.out.printf(&quot;TOTAL COST OF ORDER IS $%.2f%n&quot;, cost);
}
}while (count &lt;1);
}
if (input == SALADS) {
do {
System.out.println(&quot;How many salads?&quot;);
count = scan.nextInt();
if (count&lt;1) {				 			
System.out.println(&quot;ERROR - VALUE CANNOT BE LESS THAN 1. PLEASE RE-ENTER&quot;);
}
if (count&lt;=2) {
double cost = count * SALAD_COST;
System.out.printf(&quot;TOTAL COST OF ORDER IS $%.2f%n&quot;, cost);
}
if (count&gt;=3) {
double cost = count * SALAD_COST_X3;
System.out.printf(&quot;TOTAL COST OF ORDER IS $%.2f%n&quot;, cost);
}
}while (count &lt;1);
}
if (input == PLATTER) {
do {
System.out.println(&quot;How many platters?&quot;);
count = scan.nextInt();
if (count&lt;1) {				 			
System.out.println(&quot;ERROR - VALUE CANNOT BE LESS THAN 1. PLEASE RE-ENTER&quot;);
}
if (count&lt;=2) {
double cost = count * PLATTER_COST;
System.out.printf(&quot;TOTAL COST OF ORDER IS $%.2f%n&quot;, cost);
}
if (count&gt;=3) {
double cost = count * PLATTER_COST_X3;
System.out.printf(&quot;TOTAL COST OF ORDER IS $%.2f%n&quot;, cost);
}
}while (count &lt;1);
}	
System.out.println(&quot;Do you wish to re order? Press 1 for Yes or 0 for No&quot;);
input2 = scan.nextInt();
}while (input2 == 1);
if (input2 == 0) {
System.out.println(&quot;GOODBYE :)&quot;);
}
}
}

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 &lt; 1)
xxxx;
if (count &lt;= 2)
yyyy;

for values 0 or less, both xxxx and yyyy will be executed. You probably mean

     if (count &lt; 1)
xxxx;
else if (count &lt;= 2)
yyyy;

I hope that with that hint you can figure out the changes you need to make.

huangapple
  • 本文由 发表于 2020年10月1日 07:01:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/64146904.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定