与java不兼容的类型错误

时间:2017-11-15 18:08:07

标签: integer double incompatibletypeerror

Lab7_5.java:79: error: incompatible types: possible lossy conversion from double to int
                extra = minutesOver + .20;
                                    ^
Lab7_5.java:83: error: incompatible types: possible lossy conversion from double to int
                return totalDue;
                       ^
2 errors

我在网站上环顾四周,结果发现其他人也遇到了这个问题,但我查看了评论,我仍然不明白这个问题,我的代码中没有双重为什么会这样说。我也不明白错误说找不到符号。请帮助我真的被困在这一段时间。

代码:

import java.util.Scanner;

public class Lab7_5 {
    static Scanner keyboard = new Scanner(System.in);

    public static void main(String[] args) {
        // Declare local variables
        int minutesAllowed, minutesUsed, minutesOver;
        double totalDue; 
        String endProgram = "no";

        // Loop to run program again
        while (endProgram.equals("no")) {
            minutesAllowed = 0;
            minutesUsed = 0;
            totalDue = 0;
            minutesOver = 0;

            // Call functions
            minutesAllowed = getAllowed(minutesAllowed);
            minutesUsed = getUsed(minutesUsed);
            minutesOver = minutesUsed - minutesAllowed;

        if (minutesOver < 0){
            minutesOver = 0;
    }
            totalDue = calcTotal(totalDue, minutesOver, minutesUsed, minutesAllowed);

            // Call functions cont.
            printData(minutesAllowed, minutesUsed, totalDue, minutesOver);

            System.out.println("Do you want to end the program (enter no to process another set of test scores): ");
            endProgram = keyboard.next();

        while (!(endProgram.equals("yes")) && !(endProgram.equals("no"))){
            System.out.println("Please enter a value of yes or no");
            endProgram = keyboard.nextLine();


        }
    }
    }
    public static int getAllowed(int minutesAllowed) {
        System.out.println("How many minutes are allowed: ");

        minutesAllowed = keyboard.nextInt();
    while (minutesAllowed <200 || minutesAllowed >800) {
        System.out.println("Please enter minutes between 200 and 800 ");
        minutesAllowed = keyboard.nextInt();
    }

        return minutesAllowed;
    }

    public static int getUsed(int minutesUsed) {
        System.out.println ("How many minutes were used: ");
        minutesUsed = keyboard.nextInt();

        while (minutesUsed <0) {
            System.out.println("Please enter minutes of at least 0");
            System.out.println("How many minutes were used: ");
            minutesUsed = keyboard.nextInt();

        }
        return minutesUsed;
    }

    public static int calcTotal(double totalDue, int minutesOver, int minutesUsed, int minutesAllowed) {
        int extra;
    if (minutesUsed <= minutesAllowed){
        totalDue = 74.99;
    } else { 
        minutesOver = minutesUsed - minutesAllowed;
        extra = minutesOver * .20;
        totalDue = 74.99 + extra;
        System.out.println ("You were over your minutes by" + minutesOver);
    }
        return totalDue;
    }

    public static void printData(int minutesAllowed, int minutesUsed, double totalDue, int minutesOver) {
        System.out.println("----------------MONTHLY USE REPORT----------------------");
        System.out.println("Minutes allowed were " + minutesAllowed);
        System.out.println("Minutes used were " + minutesUsed);
        System.out.println("Minutes over were " + minutesOver );
        System.out.println("Total due is $ " + totalDue);

    }
}

1 个答案:

答案 0 :(得分:0)

您将extra声明为int,然后将其设为double

int extra;

extra = minutesOver * .20 // error here
// example 6 * .20 != an int