【问题标题】:Having trouble with a simple Java program使用简单的 Java 程序时遇到问题
【发布时间】:2018-10-22 00:07:50
【问题描述】:

尝试编写一个 Java 程序来满足这一点:Duke Shirts 以每件 24.95 美元的价格出售 Ja​​va T 恤,但以下数量可能有折扣:1 件或 2 件衬衫,无折扣,总运费为 10.00 美元 3-5 件衬衫,折扣为 10%,总运费为 8.00 美元 6-10 件衬衫,折扣为 20%,总运费为 5.00 美元 11 件或更多衬衫,折扣为 30%,运费免费 编写一个 Java 程序,提示用户输入所需衬衫的数量.然后程序应该打印衬衫的扩展价格、运费和订单的总成本。酌情使用货币格式。

这是我的代码:

import java.lang.Math;
import java.util.Scanner;

public class Excercise2_3 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        System.out.println("How many shirts do you need?");
        double shirts = input.nextInt();
        if (shirts <= 2)
            System.out.printf("The extended cost is : $%.2", (shirts * 24.95));
            System.out.printf("The shipping charges are $10.00");
            System.out.printf("The total cost is : $%.2", (shirts * 24.95) + 10);
        if (shirts > 2 && shirts <= 5)
            System.out.printf("The extended cost is : $%.2", ((shirts * 24.95)*.10));
            System.out.printf("The shipping charges are $8.00");
            System.out.printf("The total cost is : $%.2", ((shirts * 24.95)*.10) + 8);
        if (shirts > 5 && shirts <= 10)
            System.out.printf("The extended cost is : $%.2", ((shirts * 24.95)*.20));
            System.out.printf("The shipping charges are $5.00");
            System.out.printf("The total cost is : $%.2", ((shirts * 24.95)*.20) + 5);
        if (shirts > 10)
            System.out.printf("The extended cost is : $%.2", ((shirts * 24.95)*.00));
            System.out.printf("Shipping is free!");
            System.out.printf("The total cost is : $%.2", ((shirts * 24.95)*.30));

    }

}

谁能解释一下为什么编译不正确?谢谢!

【问题讨论】:

  • 欢迎堆栈溢出。堆栈溢出不是一个将您的 CS101 作业众包的网站。如果您有一个问题可以明确说明并举出一个最小可行的例子,人们将很乐意为您提供帮助。我会参考这个stackoverflow.com/help/mcve
  • 显然没有尝试众包,从我太长的代码发布中可以看出。它已经完成了 95%,我花了很多时间。我正在寻找的帮助来自 Clay 的 3 句回复,我不是在找人为我做作业。不过谢谢...我将来有问题时会发布更短的代码 sn-ps。我不希望我的教授认为我在作弊。
  • 我的意思是,您的问题是“这是我收到的任务,有人可以调试我的代码吗?”仅仅因为有人回复并不意味着您正在适当地使用该网站。如果您要发布该问题的一般示例并解释您为什么感到困惑,那么它可能对其他人有益并且超出您当前家庭作业的范围。这有意义吗?

标签: jave


【解决方案1】:

if 语句周围缺少花括号。此外,您的 printf 格式字符串应为 %.2f。您缺少 f 并且缺少换行符。

import java.util.Scanner;

public class TempApp {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("How many shirts do you need?");
        double shirts = input.nextInt();
        if (shirts <= 2) {
            System.out.printf("The extended cost is : $%.2f\n", (shirts * 24.95));
            System.out.printf("The shipping charges are $10.00\n");
            System.out.printf("The total cost is : $%.2f\n", (shirts * 24.95) + 10);
        }
        if (shirts > 2 && shirts <= 5) {
            System.out.printf("The extended cost is : $%.2f\n", ((shirts * 24.95) * .10));
            System.out.printf("The shipping charges are $8.00\n");
            System.out.printf("The total cost is : $%.2f\n", ((shirts * 24.95) * .10) + 8);
        }
        if (shirts > 5 && shirts <= 10) {
            System.out.printf("The extended cost is : $%.2f\n", ((shirts * 24.95) * .20));
            System.out.printf("The shipping charges are $5.00\n");
            System.out.printf("The total cost is : $%.2f\n", ((shirts * 24.95) * .20) + 5);
        }
        if (shirts > 10) {
            System.out.printf("The extended cost is : $%.2f", ((shirts * 24.95) * .00));
            System.out.printf("Shipping is free!");
            System.out.printf("The total cost is : $%.2f", ((shirts * 24.95) * .30));
        }
    }
}

【讨论】:

  • 非常感谢 Clay,我明白我现在做错了什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-08
  • 1970-01-01
  • 2011-02-03
相关资源
最近更新 更多