gpt4 book ai didi

java - 我真的需要帮助放入另一个变量(Java)来处理底部 3 种情况 :

转载 作者:行者123 更新时间:2023-12-01 05:53:12 25 4
gpt4 key购买 nike

import javax.swing.JOptionPane;
import java.text.DecimalFormat;

public class CandleLine
{
public static void main(String[] args)
{
double dollars, answer;
int shipmentCode;

dollars = getPrice();
shipmentCode = getCode();
answer = getTotalPrice(dollars,shipmentCode);
output(answer,dollars);
finish();
}

public static double getPrice()
{
double price = 0.0;
boolean done = false;

while (!done)
{
String answer = JOptionPane.showInputDialog(null,"Enter the original price\n(do not use commas or dollar signs)\n or click Cancel to exit:");

if (answer == null) finish();

try
{
price = Double.parseDouble(answer);
if (price <= 0) throw new NumberFormatException();
else done = true;
}
catch(NumberFormatException e)
{
JOptionPane.showMessageDialog(null,"Your entry was not in the proper format.","Error",JOptionPane.INFORMATION_MESSAGE);
}
}
return price;
}

public static int getCode()
{
int code = 0;
boolean done = false;

while (!done)
{
try
{
String message = "Enter the shipment code:" + "\n\n1) Priority\n2) Express\n3) Standard\n\n";

code = Integer.parseInt(JOptionPane.showInputDialog(null,message));

if (code<1 || code>3) throw new NumberFormatException();
else done = true;
}
catch(NumberFormatException e)
{
JOptionPane.showMessageDialog(null,"Please enter a 1, 2, or 3.","Error",JOptionPane.INFORMATION_MESSAGE);
}
}
return code;
}

public static double getTotalPrice(double price, int shipmentcode)
{
double totalprice = 0.0;

switch(shipmentcode)
{
case 1:
totalprice = 14.95 + price;
break;
case 2:
totalprice = 11.95 + price;
break;
case 3:
totalprice = 5.95 + price;
break;
}
return totalprice;
}

public static void output(double totalprice, double price)
{
DecimalFormat twoDigits = new DecimalFormat("$#.00");

JOptionPane.showMessageDialog(null,"Your shipment fee is" + shipmentcode,"Shipment Fee",JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(null,"Your total cost is" + twoDigits.format(totalprice),"Price Total",JOptionPane.INFORMATION_MESSAGE);
}
public static void finish()
{
System.exit(0);
}
}

。。。。。。。。。当案例 3 的总价超过 75 美元时(没有超过该价格的运费),则需要为零。我该如何实现这个?

最佳答案

 case 3:
if (price > 75 ) {
totalPrice = price;
} else {
totalprice = 5.95 + price;
}
break;

关于java - 我真的需要帮助放入另一个变量(Java)来处理底部 3 种情况 :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3920268/

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