2016-07-22 132 views
0

学习考试和有问题,我是一个初学者。 它是一个折扣计算器。咖啡 袋收费€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); 

}}

+10

我建议你刷一下基本的Java语法,pronto。 –

+0

这不是有效的Java。 – duffymo

+0

您使用的是IDE吗?您在代码中缺少很多基本语法。 –

回答

2

if(bag >= 10 && < 20)

if(bag >= 10 && bag < 20)

一个容易犯的错误是初学者! 在学习基础知识的同时,我还是赞同编者对ide的讲师。你需要学习阅读编译器告诉你的错误。一旦你知道这个问题,我想你会同意的,上面的语法错误信息很好地指出了什么是错误的。

2

我通常不赞成为汤姆索耶的学生做作业,但在这种情况下,我认为对于任何学习以不同方式看问题的初学者来说,都有教育机会。

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

请注意代码格式和可读性。对类,方法和变量的名称进行长时间的思考。如果名称不言自明,您需要更少的评论。

了解并按照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); 
    } 
} 
0

希望您的考试不会太快,因为您有很多需要查看的语法。

我真的不喜欢直截了当地回答作业问题,所以我会修好你现在有的东西,也许这会让你指向正确的方向。

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); 
    } 
} 

记住要保持你的代码干净,所以你不要混淆自己。确保你缩进你的代码。

想想你在命名变量,不要害怕重构它们,如果你想出更好的东西。使用清晰的逻辑命名将减少您需要编写的注释数量。

正如我前面提到的,研究你的语法。你的代码中有很多语法错误,这使我相信你可能在记事本或命令行中编写代码?如果是这样,我会推荐使用IDE,如IntelliJ,NetBeans或Eclipse。这些将强调你的语法错误,并指出你可能不会马上注意到的错误。

+0

谢谢,我的考试直到八月底才开始,我没有使用ide.I现在。我的讲座告诉我先使用记事本,并且这样做。 –

+0

这是我现在得到的唯一错误?线程“main”中的异常java.lang.Error:未解决的编译问题: \t运算符&&未针对参数类型定义布尔型,int \t令牌“<”的语法错误,删除此令牌 –

+0

请参见编辑的代码为更多的变化。我还想指出你的行'System.out.println(“你的dicount是”+折扣+“%”);'是不正确的。你的顾客得到5%的折扣,但是这条线告诉他他有0.05%的折扣。 –