gpt4 book ai didi

java - 由于精度损失错误,代码执行停止

转载 作者:行者123 更新时间:2023-11-30 02:37:20 25 4
gpt4 key购买 nike

我是一个菜鸟,所以如果我的菜鸟缩进不符合标准,请原谅我的菜鸟缩进。任何有关缩进的建议将不胜感激。所以我的问题是,在 switch 语句之后,计算机没有读取下一行代码。这是最后的最终发票金额方程式并将其打印到屏幕上。还有一个错误说:

InvoiceApp.java:34:错误:可能会损失精度
开关(折扣百分比)
^
必需:整数
发现:双
1 个错误

正如您将看到的,我将变量discountPercent 指定为 double 值。所以我不确定为什么会出现错误。任何帮助将不胜感激。谢谢。这是迄今为止我的代码。

import java.util.Scanner;

public class InvoiceApp
{
public static void main(String[] args)
{
//Declare variables and Scanner object
Scanner input = new Scanner(System.in);

double subtotal, discountAmount, discountPercent, invoiceTotal;
int customerType;


//Display a welcome message
System.out.println("Welcome to the invoice calculator app!");


//Prompt user for customer type
System.out.print("Please enter the customer type(1 for Silver, 2 for Gold, or 3 for Platinum): ");


//Read customer type
customerType = input.nextInt();

//Prompt user for subtotal
System.out.print("Please enter the subtotal amount: ");

//Read subtotal
subtotal = input.nextDouble();

//Calculate Discount Rate

switch(discountPercent)

{ //start switch block

case 1:

{

if (subtotal >= 500)
{
discountPercent = .20;
System.out.print("Your discount rate is .20");
break;
}

else if (subtotal >= 250)
{
discountPercent = .15;
System.out.print("Your discount rate is 15% !");
break;
}

else if (subtotal >= 100)
{
discountPercent = .10;
System.out.print("Your discount rate is 10%!"); break;
}

else if(subtotal < 100)
{
discountPercent = .0;
System.out.print("Sorry, your discount rate is 0%!"); break;
}

}//end of case one block
case 2:
{
{
discountPercent = .2;
System.out.print("Your discount rate is 20%!");
break;
}
}//end of case two block


case 3:
{

if (subtotal >= 500)
{
discountPercent = .50;
System.out.print("Woop woop, your discount rate is 50%!");
break;
}

else if (subtotal < 500)
{
discountPercent = .40;
System.out.print("Your discount rate is 40%!"); break;
}

}//end of case three block
default:
{
{
discountPercent = .5;
System.out.print("Congratulations! Your discount rate is 50 %! "); }
}

}//end of switch discountPercent

//Calculate Invoice Total
discountAmount = subtotal * discountPercent;
invoiceTotal = subtotal - discountAmount;

System.out.println("Total: " + invoiceTotal);

//Display thank you message
System.out.println("Thank you!");


//Format and display the results
System.out.print("Jump around! Jump around! Your invoice total is: " + invoiceTotal);


}//end of main
}//end of class

最佳答案

switch 语句不适用于 double。

请引用本教程:

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

A switch works with the byte, short, char, and int primitive data types. It also works with enumerated types (discussed in Enum Types), the String class, and a few special classes that wrap certain primitive types: Character, Byte, Short, and Integer (discussed in Numbers and Strings).

关于java - 由于精度损失错误,代码执行停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42708925/

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