【发布时间】:2014-08-26 22:18:00
【问题描述】:
我刚开始使用 Eclipse,对 Java 还是很陌生。我有一段代码无法编译,因为我生成的随机数不会转换为整数。错误消息在以下代码块的第二行显示为“此方法必须返回 int 类型的结果”。
public class passwordHelper {
public int rNum(String nums, String lets){
int z;
if(nums == "yes" && lets == "yes"){
z = (int)Math.random()*36;
return z;
} else if(nums == "yes"){
z = (int)Math.random()*10;
return z;
} else if(lets == "yes"){
z = (int)Math.random()*26 + 10;
return z;
} else {
System.out.println("Sorry, you need either letters or numbers in your password.");
}
}
}
如您所见,我正在使用“(int)”函数将我的数字转换为整数,但它向我发送了相同的错误消息,我也尝试过使用其他转换方法,例如“Math.floor( )”、“Math.round()”以及这三者的组合。如果有人想知道这是为用户生成随机数字和字母字符串的代码的一部分。
//感谢您的帮助
【问题讨论】:
-
字符串比较是通过equals方法完成的。尝试将 nums == "yes" 更改为 nums.equals("yes") 和其他
-
我想你有理由,但你应该在这里使用布尔值而不是字符串。
标签: java eclipse casting int double