2017-09-27 108 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; 
} 
相关问题