簡體   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