繁体   English   中英

您好,我似乎无法弄清楚为什么我的程序无法正常工作

[英]Hello I can't seem to figure out why my program isn't working

该程序是计算肉类的总价格,但我一直得到 0.68 美元而不是 10.89 美元的 output,任何帮助将不胜感激。 虽然我确信最后一个 println 语句很可能是一个错误,但任何帮助都会很有用。

 /**********************************************************************
Program: Deli.java
Description: Computes the price of a deli item given the weight (in ounces) 
        and the price per pound.
***********************************************************************/

import java.util.Scanner;
import java.text.DecimalFormat;
import java.text.NumberFormat;

public class Deli
{
/* -------------------------------------------------------------------------------------
The function main reads in the price per pound of a deli item
and the number of ounces of a deli item then computes the total price 
and prints a "label" for the item that includes the unit price (per pound),
the weight and total price.
----------------------------------------------------------------------------------------*/

public static void main (String[] args)
{
final double OUNCES_PER_POUND = 16.0;
double pricePerPound;   // price per pound
double weightOunces;    // weight in ounces
double weight;      // weight in pounds
double totalPrice;      // total price for the item
Scanner scan = new Scanner(System.in);

/* ------------------------------------------------------------------------ 
Declare money as a NumberFormat object and use the
getCurrencyInstance method to assign it a value.
Declare fmt as a DecimalFormat object and instantiate
it to format numbers with at least one digit to the left of the
decimal and the fractional part rounded to two digits.
Prompt the user and read in each input. 
-------------------------------------------------------------------------*/
NumberFormat format1 = NumberFormat.getCurrencyInstance();
DecimalFormat format2 = new DecimalFormat("0.##");
System. out. println ("Welcome to the CS Deli! ! \n ");
System.out.print ("Enter the price per pound of your item: ");
pricePerPound = scan.nextDouble();
System.out.print ("Enter the weight (ounces): ");
weightOunces = scan.nextDouble();

// Convert ounces to pounds and compute the total price
weight = weightOunces / OUNCES_PER_POUND;
totalPrice = pricePerPound * weight;

// Print the unit price, weight, and total price using the formatting objects.
// Format the weight in pounds and use the money format for the prices.
System.out.println("\nUnit Price: " + format1.format(pricePerPound) + " per pound");
System.out.println("\nWeight: " + format2.format(weightOunces) + " pounds");
System.out.println("\nTotal Price: " + format1.format(totalPrice));
}
}

"The inputs are $4.25 for the unit price, and 2.56 pounds."

我认为您对您的应用程序如何期待其输入感到有些困惑。 提示询问每磅的价格

Enter the price per pound of your item: 4.25

然后要求输入以盎司为单位的重量

Enter the weight (ounces): 2.56

同样,它要求输入盎司而不是磅。 然后,该代码采用此盎司输入 (2.56) 并将其转换为磅,最终约为0.16磅。 好吧,我想每磅4.25美元的0.16磅商品将花费您0.68美元(或仅 68 美分)。

如果您真的想购买 2.56 磅的物品,那么您应该在盎司提示中提供40.96 (2.56 * 16)。

正如@JohnBayko 已经如此亲切地指出的那样,您在此代码行中的 output 中显示了错误的值:

System.out.println("\nWeight: " + format2.format(weightOunces) + " pounds");

它应该是您显示的转化价值,而不是输入的内容:

System.out.println("\nWeight: " + format2.format(weight) + " pounds");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM