Java JDK - möglich verlustreiche Umwandlung von double zu int

Also ich habe vor kurzem geschrieben das folgenden code:

    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();
      }
}

Jedoch, es hält die Anzeige dieser:

TrainTicket.java:31: error: incompatible types: possible lossy conversion from double to int
            int total2 = price* much* 0.7;

Wenn ich versuche und ausführen mit cmd.

Kann jemand helfen, und erklären Sie die Fehler, die ich gemacht habe? Jede Hilfe ist willkommen :).
Danke!

InformationsquelleAutor | 2015-12-21

Schreibe einen Kommentar