不兼容类型:可能从double转换为int的有损转换

时间:2018-02-28 23:23:36

标签: java

我需要将总销售额除以500以获得我正在处理的作业的折扣阈值,而我无法弄清楚出了什么问题。如果这是一个愚蠢的问题,请道歉,但我在最初几周学习java。

String nameDFF;
    String inputStringDFF;
    int quantityDFF, discountThresholdDFF;
    double salesPriceDFF, totalSalesAmountDFF;



    //Create a new Scanner object to read user's input
    Scanner keyboard = new Scanner(System.in);

    //Get the user's name 
    System.out.print("What is your name? ");
    nameDFF = keyboard.nextLine();

    //Get the the user's quantity they want to buy
    System.out.print("\nWhat is the quantity you want to buy?\n Enter a whole number greater than or equal to 1.");
    quantityDFF = keyboard.nextInt();

    //Get the sales price of the item
    System.out.print("\nWhat is the sales price of the item?\n Enter the value greater than 0.");
    salesPriceDFF = keyboard.nextDouble();

    //Calculate total sales amount
    totalSalesAmountDFF = quantityDFF * salesPriceDFF;

    //Calculate the discount threshold
    discountThresholdDFF = totalSalesAmountDFF / 500;
}

1 个答案:

答案 0 :(得分:0)

您将双精度浮点除以500,并将结果存储为整数。

你真的希望它与剩余的小数一起做什么?它应该Math.floor所以你只得到非小数部分,Math.round到最接近的整数,或者你真的希望结果变量也是双精度?

Java需要您的指导,因为您要求它在没有足够信息的情况下执行操作,因此会出错。