【问题标题】:Simple Discount简单折扣
【发布时间】:2016-07-22 11:23:42
【问题描述】:

准备考试,遇到问题,我是初学者。 它是一个折扣计算器。 一袋咖啡售价 3.75 欧元 10 袋或更多 5% 折扣 20 袋或更多 10% 折扣

目前为止我所拥有的

import java.util.Scanner;
public class discount {
public static void main (String[] args){

//Scanner input; Keep this as one line. It is good practice
Scanner input = new Scanner(System.in);
double bag;
double discount;
double cost = 3.75;

//Input = new Scanner(System.ini);     combined with line above.
System.out.println("Enter Numer of bag");
bag = input.nextDouble();
//If (bag ==>10&&<20) × .05  incorrect Java syntax

if(bag >= 10 && < 20) {
 discount = 0.05;
}
else if(bag >= 20) {
  discount = 0.10;
} 
else {
  discount = 0;
}

double finalPrice;
finalPrice = (cost * bag) * (1 - discount);

System.out.println("You ordered " + bag + " bags of coffee.");
System.out.println("Your dicount is " + discount + "%");
System.out.println("You total is: " + finalPrice);

} }

【问题讨论】:

  • 我建议你快速复习一下基本的 Java 语法。
  • 这不是有效的 Java。
  • 您使用的是 IDE 吗?您的代码中缺少很多基本语法。

标签: java discount


【解决方案1】:

如果(袋 >= 10 &&

if(bag >= 10 && bag

初学者容易犯的错误! 在学习基础知识时,我有点同意讲师对编辑器与 ide 的看法。您需要学习阅读编译器告诉您的错误。一旦你知道问题所在,我想你会同意,上面的语法错误信息很好地表明了问题所在。

【讨论】:

    【解决方案2】:

    我通常不赞成为 Tom Sawyer 的学生做作业,但在这种情况下,我认为任何学习以不同方式看待问题的初学者都有受教育的机会。

    这是一个简单的类,但如果你想成为一名程序员,养成良好的习惯是个好主意。

    非常注意代码格式和可读性。仔细考虑类、方法和变量的名称。如果名称不言自明,则需要更少的 cmets。

    了解并关注Java coding standards。这将有助于提高可读性。

    忘记输入和输出以及用户界面 - 首先获得正确的功能。

    import java.util.LinkedHashMap;
    import java.util.Map;
    
    /**
     * CoffeePriceCalculator
     * User: mduffy
     * Date: 7/22/2016
     * Time: 7:46 AM
     * @link http://stackoverflow.com/questions/38525213/simple-discount
     */
    public class CoffeePriceCalculator {
    
        // Is double a good way to represent money?  What about units?  Conversions?
        public static final double DEFAULT_UNIT_PRICE = 3.75;  // British pounds
    
        private static final Map<Integer, Double> DISCOUNT_PERCENTAGE;
    
        static {
            DISCOUNT_PERCENTAGE = new LinkedHashMap<>();
            DISCOUNT_PERCENTAGE.put(10, 0.05);
            DISCOUNT_PERCENTAGE.put(20, 0.10);
        }
    
        public double calculatePrice(int numBags, double unitPrice) {
            if (numBags < 0) throw new IllegalArgumentException("Number of bags must be positive");
            if (unitPrice < 0.0) throw new IllegalArgumentException("Unit price must be positive");
            double price = numBags*unitPrice;
            price -= calculateDiscount(numBags, price);
            return price;
        }
    
        public double calculatePrice(int numBags) {
            return this.calculatePrice(numBags, DEFAULT_UNIT_PRICE);
        }
    
        public double calculateDiscount(int numBags, double price) {
            if (numBags < 0) throw new IllegalArgumentException("Number of bags must be positive");
            if (price < 0.0) throw new IllegalArgumentException("Total price must be positive");
            double discount = 0.0;
            for (int minBags : DISCOUNT_PERCENTAGE.keySet()) {
                if (numBags >= minBags) {
                    discount = price*DISCOUNT_PERCENTAGE.get(minBags);
                    break;
                }
            }
            return discount;
        }
    }
    

    现在了解JUnit 和测试驱动开发还为时过早。

    import org.junit.Assert;
    import org.junit.Test;
    
    /**
     * Junit test for CoffeePriceCalculator
     * User: mduffy
     * Date: 7/22/2016
     * Time: 7:50 AM
     * @link http://stackoverflow.com/questions/38525213/simple-discount
     */
    public class CoffeePriceCalculatorTest {
    
        public static final double TOLERANCE = 1.0e-3;
    
        @Test
        public void testCalculatePrice_NoDiscount() {
            // setup
            CoffeePriceCalculator coffeePriceCalculator = new CoffeePriceCalculator();
            int numBags = 5;
            // exercise
            double actual = coffeePriceCalculator.calculatePrice(numBags);
            // assert
            Assert.assertEquals(numBags * CoffeePriceCalculator.DEFAULT_UNIT_PRICE, actual, TOLERANCE);
        }
    
        @Test
        public void testCalculatorPrice_LowDiscount() {
            // setup
            CoffeePriceCalculator coffeePriceCalculator = new CoffeePriceCalculator();
            int numBags = 15;
            // exercise
            double actual = coffeePriceCalculator.calculatePrice(numBags);
            // assert
            Assert.assertEquals(numBags * CoffeePriceCalculator.DEFAULT_UNIT_PRICE*0.95, actual, TOLERANCE);
        }
    
        @Test
        public void testCalculatorPrice_HighDiscount() {
            // setup
            CoffeePriceCalculator coffeePriceCalculator = new CoffeePriceCalculator();
            int numBags = 25;
            // exercise
            double actual = coffeePriceCalculator.calculatePrice(numBags);
            // assert
            Assert.assertEquals(numBags * CoffeePriceCalculator.DEFAULT_UNIT_PRICE*0.90, actual, TOLERANCE);
        }
    
        @Test(expected = IllegalArgumentException.class)
        public void testCalculatePrice_NegativeBags() {
            // setup
            CoffeePriceCalculator coffeePriceCalculator = new CoffeePriceCalculator();
            int numBags = -25;
            // exercise
            coffeePriceCalculator.calculatePrice(numBags);
        }
    }
    

    【讨论】:

      【解决方案3】:

      希望你的考试不会很快到来,因为你有很多语法要复习。

      我不太喜欢直接回答家庭作业问题,所以我会修正你目前的问题,也许这会为你指明正确的方向。

      Import.java.util.scanner;
      
      public class discount {
        public static void main (String[] args){
      
          //Scanner input; Keep this as one line. It is good practice
          Scanner input = new Scanner(System.in);
          //Float bag;
         int bag;
          Float discount;
          Double cost = 3.75;
      
          //Input = new Scanner(System.ini);     combined with line above.
          System.out.println("Enter Numer of bag");
          bag = input.nextFloat();
          //If (bag ==>10&&<20) × .05  incorrect Java syntax
      
          //if(bag >= 10 && < 20) {
          if(bag >= 10 && bag < 20) {
           discount = 0.05;
          }
          else if(bag >= 20) {
            discount = 0.10;
          } 
          else {
            //discount = 0;
            discount = 0.0;
          }
      
          double finalPrice;
          finalPrice = (cost * bag) * (1 - discount);
      
          //System.out.println("You ordered " + bag + " bags of coffee.);
          System.out.println("You ordered " + bag + " bags of coffee.");
          System.out.println("Your dicount is " + discount + "%");
          System.out.println("Your total is: " + finalPrice);
        }
      }
      

      请记住保持代码清洁,以免混淆自己。确保你缩进你的代码。

      考虑一下您为变量命名的内容,如果您想出更好的东西,请不要害怕重构它们。使用清晰和合乎逻辑的命名将减少您需要编写的 cmets 数量。

      正如我之前提到的,学习你的语法。您的代码中有很多语法错误,这让我相信您可能是在记事本或命令行中编写代码?如果是这样,我建议使用诸如 IntelliJ、NetBeans 或 Eclipse 之类的 IDE。这些将强调您的语法错误并指出您可能不会立即注意到的错误。

      【讨论】:

      • 谢谢,我的考试要到八月底,我没有使用 ide。我现在正在使用。我的讲座告诉我先使用记事本,并且是这样做的。
      • 这是我现在得到的唯一错误?线程“main”java.lang.Error 中的异常:未解决的编译问题:运算符 && 未定义参数类型 boolean、int 标记“
      • 查看编辑后的代码以获得更多更改。我还想指出您的System.out.println("Your dicount is " + discount + "%"); 行不正确。您的客户获得了 5% 的折扣,但这条线告诉他他获得了 0.05% 的折扣。
      猜你喜欢
      • 1970-01-01
      • 2020-04-26
      • 1970-01-01
      • 1970-01-01
      • 2018-02-26
      • 2019-08-28
      • 1970-01-01
      • 1970-01-01
      • 2018-03-08
      相关资源
      最近更新 更多