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;
^
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;
^
System.out.println("It takes at least"+years+"years to double your savings."); ----------------------------------------
Quietly through .....
不过需要改两个地方
一是double amount; ,这里应该进行初始化
二是程序在返回值后不能再有输出语句
……
return amount; 而你return的是一个double类型的数所以出错
amount=savingAmount*(1+interestRate);
而在这里给他付值了
第二点很正确 return后 说明退出方法了,所以不能再在后面有任何语句
补充第三:他的方法返回的是要int,而返回double的,double的数据类型要比int大
所以你需要进行强制转化下 return (int)amount;
我看过几个程序像 public void example{}开头的结尾要加
public static void main(String[] args)
{classname myclassname=new classname;
myclassname.example( );}
那public int example(double x,double y)这类开头
最后main怎么写呀??
public static void main(String[] args){
problem1 myclassname=new problem1();
myclassname.calculateYears(1.0,2.0);
} 类的命名不大符合习惯啊 首字母应为大写
我也是初学