import java.util.Scanner;
public class problem1{
    public int calculateYears(double savingAmount,double interestRate)                                     
     { int years=0;
       double amount;
       Scanner in=new Scanner(System.in);       
       System.out.println("Your saving balance?");
       savingAmount=in.nextFloat();                      
       System.out.println("Interest rate?");     
       interestRate=in.nextFloat();
       while(savingAmount<2000)                                               
      { amount=savingAmount*(1+interestRate); 
        years++;}
       return amount;                               
      System.out.println("It takes at least"+years+"years to double your savings.");
     }
}
cmd报错说
return amount; 这一行可能损失精度       找到:double
                                                                    需要:int 
                                                                                return amount;
                                                                                            ^

解决方案 »

  1.   

    显式的转换int到double试一试?
      

  2.   


    System.out.println("It takes at least"+years+"years to double your savings."); ----------------------------------------
    Quietly through .....
      

  3.   

    不知道楼主是什么意思
    不过需要改两个地方
    一是double amount; ,这里应该进行初始化
    二是程序在返回值后不能再有输出语句
      

  4.   

    意思就是,把那行改成return (int)amount;你的amount是double型,但是你的方法签名里写的返回值是int型,所以要做个转换
      

  5.   

    public int calculateYears(double savingAmount,double interestRate)    是说你的方法要返回int型数,double amount; 
     ……
    return amount;  而你return的是一个double类型的数所以出错
      

  6.   

    小鸟在这里谢谢各位大侠了!!!!!!up~up~up
      

  7.   

    第一点在他的这个程序中不需要,因为他没有直接使用未初始化的变量
    amount=savingAmount*(1+interestRate); 
    而在这里给他付值了
    第二点很正确 return后 说明退出方法了,所以不能再在后面有任何语句
    补充第三:他的方法返回的是要int,而返回double的,double的数据类型要比int大
    所以你需要进行强制转化下   return (int)amount;   
      

  8.   

    顺便问下我的这个程序最后main怎么写啊?
    我看过几个程序像 public void example{}开头的结尾要加
    public static void main(String[] args)
     {classname myclassname=new classname;
      myclassname.example( );}
    那public int example(double x,double y)这类开头
    最后main怎么写呀??
      

  9.   

    这里的main()就加在 public int calculateYears(double savingAmount,double interestRate)     方法的后面啊
    public static void main(String[] args){
      problem1 myclassname=new problem1(); 
      myclassname.calculateYears(1.0,2.0);
    } 类的命名不大符合习惯啊  首字母应为大写
    我也是初学
      

  10.   

    你的那个 (1.0,2.0)怎么来的?为什么不写成myclassname.calculateYears(savingAmount,interestRate)