【问题标题】:Eclipse - Java, casting from double to intEclipse - Java,从 double 转换为 int
【发布时间】: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


【解决方案1】:

你必须在所有分支中返回一个值,有 3 种可能:

1) 在 else 分支中返回一个 int:

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.");
            return -1;
    }
}

2) 抛出未经检查的异常

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.");
        throw new RuntimeException("Sorry, you need either letters or numbers in your password.");
    }
}

3) 抛出检查异常

public int rNum(String nums, String lets) throws Exception {

    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.");
        throw new Exception("Sorry, you need either letters or numbers in your password.");
    }
}

【讨论】:

  • 需要使用 .equals() 进行字符串比较。
  • 为什么?有原因吗?表现?恕我直言,无论如何它都会被编译成相同的字节码......
  • "==" 不会编译成与 .equals() stackoverflow.com/questions/1643067/… 相同的字节码
【解决方案2】:

您的方法必须返回一个 int 值,但在最后一个 else 块中您没有返回任何内容。在那里返回一个默认值,例如返回 0 或抛出一个表示无效输入的异常。

还有;

要比较两个字符串,您必须使用其 equals 方法。

nums == "yes" 更改为"yes".equals(nums)

lets == "yes""yes".equals(lets)

【讨论】:

  • 虽然绝对正确,稍后会提出 - 这不是问题,评论就足够了。
  • 在您在评论中添加“@name”之前,不会通知 eagean。通常,当有人评论您时,您总是会收到有关您自己的问题和答案的通知,但反之亦然。
  • 非常感谢,实际上我最终遇到了您提出的第二个问题,所以您也为我整理了这个问题,非常感谢您。
【解决方案3】:

试试这个:

public int rNum(String nums, String lets){

    int z;
    Random random = new Random();

    if(nums.equals("yes") && lets.equals("yes")){
        z = random.nextInt(36);// generates a random number from 0 to 36
        return z;

    } else if(nums.equals("yes")){
        z = random.nextInt(10);//generates a random number from 0 to 10
        return z;   

    } else if(lets.equals("yes")){
        z = random.nextInt(26 + 10);//generates a random number from 0 to 36
        return z;

    } else {
        System.out.println("Sorry, you need either letters or numbers in your password.");
            return -1;
    }
}

【讨论】:

    【解决方案4】:

    Math.random() 返回一个介于 0 和 1 之间的数字。如果将其转换为 int,您将获得 0。您可以将双精度数相乘,然后使用括号相加。

    替代方案: 您可以使用:

    Random rand = new Random();
    

    然后

    rand.nextInt(...);
    

    附注:

    • 添加return语句(在末尾)
    • 将字符串比较更改为使用“等于”

    【讨论】:

      【解决方案5】:

      String 是引用类型,您不能将引用类型与比较运算符进行比较。即你不能在这里使用==
      要比较字符串,您可以使用equals()equalsIgnoreCase()
      equalsIgnoreCase() 在比较两个字符串时不考虑大小写,我更喜欢在这里使用它。但在这里我修改了您的代码,将== 替换为equals()。如果您不想考虑大小写,即此处希望 yesYES 为 true,请改用 equalsIgnoreCase()

      int z;
      if(nums.equals("yes") && lets.equals("yes")){
          z = (int)Math.random()*36;
          return z;
      } else if(nums.equals("yes")){
          z = (int)Math.random()*10;
          return z;   
      } else if(lets.equals("yes")){
          z = (int)Math.random()*26 + 10;
          return z;
      } else {
          System.out.println("Sorry, you need either letters or numbers in your password.");
      }
      

      【讨论】:

        猜你喜欢
        • 2013-11-23
        • 1970-01-01
        • 2013-06-02
        • 2020-04-05
        • 1970-01-01
        • 2011-05-10
        • 1970-01-01
        • 2013-12-08
        相关资源
        最近更新 更多