由于丢失了原始错误,代码执行停止

时间:2017-03-10 02:10:00

标签: java cloud9

我是一个菜鸟,所以请原谅我的菜鸟缩进,如果他们不相提并论。任何关于缩进的建议都将受到极大的赞赏。所以我的问题是在switch语句之后计算机没有读取下一行代码。这是最后的最终发票金额等式并将其打印到屏幕上。还有一个错误说:

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

正如您将看到的,我将变量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

1 个答案:

答案 0 :(得分:2)

switch语句不适用于double。

请参阅本教程:

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

  

开关使用byte,short,char和int原始数据       类型。它也适用于枚举类型(在枚举中讨论)       Types),String类和一些包装的特殊类       某些原始类型:Character,Byte,Short和Integer       (在数字和字符串中讨论)。