Java 简单折扣

Java 简单折扣,java,discount,Java,Discount,为了考试而学习,我遇到了一个问题,我是一个初学者。 这是一个折扣计算器。 一袋咖啡的价格是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 inpu

为了考试而学习,我遇到了一个问题,我是一个初学者。 这是一个折扣计算器。 一袋咖啡的价格是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);
import java.util.Scanner;
公务舱折扣{
公共静态void main(字符串[]args){
//扫描仪输入;保持这一行。这是一个好的做法
扫描仪输入=新扫描仪(System.in);
双袋;
双重折扣;
双倍成本=3.75;
//输入=新扫描仪(System.ini);与上面的行组合。
System.out.println(“输入行李编号”);
bag=input.nextDouble();
//如果(行李==>10&&=10&&<20){
折扣=0.05;
}
否则,如果(行李>=20){
折扣=0.10;
} 
否则{
折扣=0;
}
双终价;
最终价格=(成本*包)*(1-折扣);
System.out.println(“您订购了”+袋+“袋咖啡”);
System.out.println(“您的贴现率为“+折扣+”%);
System.out.println(“您的合计为:+finalPrice”);
}
}

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

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

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

学习并跟随。这将有助于提高可读性

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

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

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

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

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

学习并跟随。这将有助于提高可读性

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

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

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

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

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);
  }
}
Import.java.util.scanner;
公务舱折扣{
公共静态void main(字符串[]args){
//扫描仪输入;保持这一行。这是一个好的做法
扫描仪输入=新扫描仪(System.in);
//漂浮袋;
int包;
浮动折扣;
双倍成本=3.75;
//输入=新扫描仪(System.ini);与上面的行组合。
System.out.println(“输入行李编号”);
bag=input.nextFloat();
//如果(行李==>10&&=10&&<20){
如果(行李>=10和行李<20){
折扣=0.05;
}
否则,如果(行李>=20){
折扣=0.10;
} 
否则{
//折扣=0;
折扣=0.0;
}
双终价;
最终价格=(成本*包)*(1-折扣);
//System.out.println(“您订购了”+袋+”袋咖啡);
System.out.println(“您订购了”+袋+“袋咖啡”);
System.out.println(“您的贴现率为“+折扣+”%);
System.out.println(“您的总额为:“+finalPrice”);
}
}
记住保持代码整洁,以免混淆自己。确保代码缩进

考虑一下你给变量命名的方法,如果你能想出更好的方法,不要害怕重构它们。使用清晰合理的命名可以减少你需要写的注释数量


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

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

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

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);
  }
}
Import.java.util.scanner;
公务舱折扣{
公共静态void main(字符串[]args){
//扫描仪输入;保持这一行。这是一个好的做法
扫描仪输入=新扫描仪(System.in);
//漂浮袋;
int包;
浮动折扣;
双倍成本=3.75;
//输入=新扫描仪(System.ini);与上面的行组合。
System.out.println(“输入行李编号”);
bag=input.nextFloat();
//如果(行李==>10&&=10&&<20){
如果(行李>=10和行李<20){
折扣=0.05;
}
否则,如果(行李>=20){
折扣=0.10;
} 
否则{
//折扣=0;
折扣=0.0;
}
双终价;
最终价格=(成本*包)*(1-折扣);
//System.out.println(“您订购了”+袋+”袋咖啡);
System.out.println(“您订购了”+袋+“袋咖啡”);
System.out.println(“您的贴现率为“+折扣+”%);
System.out.println(“您的总额为:“+finalPrice”);
}
}
记住保持代码整洁,以免混淆自己。确保代码缩进

考虑一下你给变量命名的方法,如果你能想出更好的方法,不要害怕重构它们。使用清晰合理的命名可以减少你需要写的注释数量

正如我前面提到的,研究一下你的语法。你的代码中有很多语法错误,这让我相信你可能是在记事本或fr中编写代码