2017-09-27 110 views
-1

我不斷收到一個錯誤 「Java Error incompatible types:possible lossy conversion」Java錯誤不兼容的類型:可能有損轉換?

我相信我可能必須設置更多變量,但我不太確定?

路線:

1)創建)稱爲SavingsAccount

2 public類把你的名字和節號在註釋塊頂部

3)包含一個構造函數的參數所有者的姓名和初始帳戶餘額。

4)包含賬戶持有人名稱和賬戶餘額的私人非靜態變量。

5)包含低利率和高利率的私人靜態變量。

6)爲賬戶持有人姓名和賬戶餘額寫出公共的getter和setter方法。

7)爲利率編寫一個公共的getter方法,如上所述計算。

8)爲低利率和高利率包含公共靜態getter和setter方法。 9)保存文件並確保編譯完成。

import java.util.*; 

public class SavingsAccount 
{ 
    //private static and private nonstatic variables 
    private String ownerName; 
    private double acctBalance; 
    private static double lowInt; 
    private static double highInt; 

    //write a public getter and setter for account holder 
    public void setOwnersName(String name) 
    { 
     ownerName = name; 
    } 
    public String getOwnerName() 
    { 
     return ownerName; 
    } 
    //write a public getter and setter for the account balance 
    public void setAcctBalance(double balance) 
    { 
     acctBalance = balance; 
    } 
    public double getAcctBalance() 
    { 
     return acctBalance; 
    } 

    //write a public getter method for interest rate 
    public double getInterestRate() 
    { 
     if(acctBalance < 1000) 
     { 
      return lowInt; 
     } 
     if(acctBalance > 1000) 
     { 
      return highInt; 
     } 
    } 

    //include public static getter and setter method for the lowe interest 
    public static int setLowInt(double rate) 
    { 
     lowInt = rate; 
    } 
    public static int getLowInt() 
    { 
     return lowInt; 
    } 
    //include public static getter and setter for high interest 
    public static int setHighInt(double rate) 
    { 
     highInt = rate; 
    } 
    public static int getHighInt() 
    { 
     return highInt; 
    } 

    public SavingsAccount(String name, double balance) 
    { 
     ownerName = name; 
     acctBalance = balance; 
    } 
}//end class 
+1

爲什麼的返回類型'setLowInt'的'int'?它應該是'空白'。 ---如果利率是「雙」,爲什麼返回類型的'getLowInt'是'int'?將返回類型更改爲'double',或將字段'lowInt'更改爲'int'並將'setLowInt'的'rate'參數更改爲int。 ---建議:將'lowInt'重命名爲'lowInterest'以避免混淆,所以'Int'不會被誤解爲'Integer'。 – Andreas

回答

0

你需要你的回報轉換爲int

public static int getLowInt() 
{ 
    return (int) lowInt; 
} 
相關問題