Java JDK - 从double到int的可能有损转换

1 java int long-integer

所以我最近编写了以下代码:

    import java.util.Scanner;

public class TrainTicket
{
      public static void main (String args[])
      {

         Scanner money = new Scanner(System.in);
         System.out.print("Please type in the type of ticket you would like to buy.\nA. Child B. Adult C. Elder.");
         String type = money.next();
         System.out.print("Now please type in the amount of tickets you would like to buy.");
         int much = money.nextInt();
         int price = 0;
         switch (type)
          {
            case "A":
            price = 10;
            break;
            case "B":
            price = 60;
            break;
            case "C":
            price = 35;
            break;
            default:
            price = 0;
            System.out.print("Not a option ;-;");
           }
          if (price!=0)
          {
            int total2 = price* much* 0.7;
            System.out.print("Do you have a coupon code? Enter Y or N");
            String YN = money.next();
            if (YN.equals("Y"))
            {
             System.out.print("Please enter your coupon code.");
             int coupon = money.nextInt();
             if(coupon==21)
             {
              System.out.println("Your total price is " + "$" + total2 + ".");
             }
             else
             {
              System.out.println("Invalid coupon code, your total price is " + "$" + price* much + ".");
             }
            }
            else
            {
            System.out.println("Your total price is " + "$" + price* much + "." ); 
            }
          }

       money.close();
      }
}
Run Code Online (Sandbox Code Playgroud)

但是,它一直显示:

TrainTicket.java:31: error: incompatible types: possible lossy conversion from double to int
            int total2 = price* much* 0.7;
Run Code Online (Sandbox Code Playgroud)

当我尝试用cmd运行它时.

有人可以帮助并解释我所犯的错误吗?任何帮助表示赞赏:).谢谢!

khe*_*ood 8

您正在尝试将price* much* 0.7浮点值 (a double) 分配给整型变量。Adouble不是精确整数,因此通常int变量不能保存double值。

例如,假设您的计算结果是12.6。您不能保存12.6整数变量,但您可以丢弃分数并只存储12

如果您不担心会损失多少分数,请将您的数字转换为如下所示int

int total2 = (int) (price* much* 0.7);
Run Code Online (Sandbox Code Playgroud)

或者您可以将其四舍五入到最接近的整数。

int total2 = (int) Math.round(price*much*0.7);
Run Code Online (Sandbox Code Playgroud)


Mat*_*hai 6

当您转换doubleint,值的精度损失.例如,当您将4.8657(double)转换为int时.int值将为4.Primitive int不存储十进制数字.因此您将丢失0.8657.

在您的情况下,0.7是一个double值(浮点在默认情况下被视为double,除非提到float-0.7f).当你计算时price*much*0.7,答案是一个双精度值,因此编译器不允许你将它存储在一个整数类型中,因为可能possible lossy conversion会丢失精度.所以,你可能会失去精度.

那么你能做些什么呢?你需要告诉编译器你真的想要这样做.你需要告诉它你知道你在做什么.所以使用以下代码显式地将double转换为int:

int total2= (int) price*much*0.7;
 /*(int) tells compiler that you are aware      of what you are doing.*/
 //also called as type casting
Run Code Online (Sandbox Code Playgroud)

在您的情况下,由于您正在计算成本,我建议您将变量声明total2为double或float类型.

double total2=price*much*0.7;
 float total2=price*much*0.7;
 //will work
Run Code Online (Sandbox Code Playgroud)