Java 你好,我能';我似乎不明白为什么我的程序是';行不通

Java 你好,我能';我似乎不明白为什么我的程序是';行不通,java,Java,该计划是计算肉类的总价格,但我一直得到0.68美元的产出,而不是10.89美元,任何帮助都将不胜感激。虽然我确信最后一个println语句很可能出错,但任何帮助都会有用 /********************************************************************** Program: Deli.java Description: Computes the price of a deli item given the weight (in ounce

该计划是计算肉类的总价格,但我一直得到0.68美元的产出,而不是10.89美元,任何帮助都将不胜感激。虽然我确信最后一个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));
}
}

“输入的单价为4.25美元,2.56英镑。”

我认为您在这里对应用程序如何期望输入感到有点困惑。提示询问每磅的价格:

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

同样,它要求输入盎司,而不是磅。然后,代码将这个盎司输入值(2.56)转换为磅,最终约为0.16磅。好吧,我想每磅定价为4.25美元的物品的0.16英镑将花费你0.68美元(或仅68美分)

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

正如@JohnBayko已经优雅地指出的,您在这个代码行的输出中显示了错误的值:

System.out.println("\nWeight: " + format2.format(weightOunces) + " pounds");
应显示您的转换值,而不是输入的值:

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

输入和实际输出是什么?我可以看到你打印的是
weightOunces
作为“磅”。输入的单价是4.25美元,2.56磅。即使做了这个修正,输出仍然是0.68美元,而不是我需要的10.89美元。那么,你仍然输入2.56盎司。试着输入40.96盎司。你觉得这个合适吗?
System.out.println("\nWeight: " + format2.format(weightOunces) + " pounds");
System.out.println("\nWeight: " + format2.format(weight) + " pounds");