1

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

InvoiceApp.java:34: error: possible loss of precision switch(discountPercent) ^ required: int found: double 1 error

正如您将看到的,我将变量 discountPercent 分配为双精度值。所以我不确定为什么会出现错误。任何帮助将不胜感激。谢谢你。到目前为止,这是我的代码。

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
4

1 回答 1

2

switch 语句不适用于双精度。

请参考本教程:

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

开关适用于 byte、short、char 和 int 原始数据类型。它还适用于枚举类型(在 Enum Types 中讨论)、String 类和一些包装某些原始类型的特殊类:Character、Byte、Short 和 Integer(在 Numbers 和 Strings 中讨论)。

于 2017-03-10T02:17:09.133 回答