Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaJDK-从double到int的可能有损转换_Java_Int_Long Integer - Fatal编程技术网

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

JavaJDK-从double到int的可能有损转换,java,int,long-integer,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 li

因此,我最近编写了以下代码:

    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();
      }
}
int total2= (int) price*much*0.7;
 /*(int) tells compiler that you are aware      of what you are doing.*/
 //also called as type casting
但是,它始终显示以下内容:

TrainTicket.java:31: error: incompatible types: possible lossy conversion from double to int
            int total2 = price* much* 0.7;
当我尝试使用cmd运行它时

有人能帮我解释一下我犯的错误吗?感谢您的帮助:)。
谢谢

您试图将
price*much*0.7
赋值给整数变量,这是一个浮点值(一个
double
)。
double
不是精确整数,因此通常
int
变量不能包含
double

例如,假设计算结果为
12.6
。您不能将
12.6
保存在整数变量中,但可以舍弃分数,只存储
12

如果您不担心丢失的分数,请将您的数字转换为
int
,如下所示:

int total2 = (int) (price* much* 0.7);
或者可以将其四舍五入到最接近的整数

int total2 = (int) Math.round(price*much*0.7);

double
转换为
int
时,值的精度将丢失。 例如 将4.8657(双精度)转换为int时,int值将为4。Primitive
int
不存储十进制数。因此,将丢失0.8657

在您的例子中,0.7是一个双精度值(除非提到float-0.7f,否则默认情况下浮点被视为double)。 当您计算
price*much*0.7
时,答案是一个双精度值,因此编译器不允许您将其存储在类型整数中,因为可能会丢失精度。这就是
可能的有损转换
,您可能会丢失精度

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

    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();
      }
}
int total2= (int) price*much*0.7;
 /*(int) tells compiler that you are aware      of what you are doing.*/
 //also called as type casting
在您的情况下,由于您正在计算成本,我建议您将变量
total2
声明为double或float类型

double total2=price*much*0.7;
 float total2=price*much*0.7;
 //will work

@billc.cn的金额通常用小数表示,这就是我建议它的原因!我的意思是说用双打代替浮球。。。。浮点数的精度损失太快,即使在每天的情况下也无法使用。