gpt4 book ai didi

java - 帮助需要更正 Java 代码

转载 作者:行者123 更新时间:2023-12-02 06:03:30 24 4
gpt4 key购买 nike

我有这个,但有一个错误,我不明白:(。我对 Java 比较陌生。

package hw;
import java.util.Scanner;

public class Problem1
{
public static void main (String [] args)
{
int cost; int number; double cost_unit; double total;

Scanner entrada = new Scanner(System.in);

System.out.println("Please enter the cost of the product.");
cost = entrada.nextInt();

while (cost>0){

System.out.println("Please enter the amount of units to be sold");
number = entrada.nextInt();

if (number>0);

cost_unit = cost * 1.4;
total = cost_unit*number;

System.out.printf("Cost per unit will be $ %d\n",cost_unit);
System.out.printf("Cost per unit will be $ %d\n",total);
}
}
}

//我只想让用户输入产品的成本,给出要订购的单位数量,然后我希望程序找出产品的最终价格,利润为 40%。

最佳答案

在 printf 中,%d 表示有符号十进制整数,而 cost_unittotal 表示 double 。您应该使用 %f 来代替。

System.out.printf("Cost per unit will be $ %f\n",cost_unit);
System.out.printf("Cost per unit will be $ %f\n",total);

关于java - 帮助需要更正 Java 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/548132/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com