Java递归函数中缺少返回语句

时间:2015-11-12 02:26:27

标签: java recursion

我正在尝试在Java中生成25个非重复随机数的列表,并且我不断收到Missing Return Statement错误。可以看出,我尝试在返回之前调用该方法。不确定缺少什么。它也不适用于return (rando)

import java.util.*;
public class arrayList{

        ArrayList<Integer> checkRandom;
        ArrayList<Integer> array4;
        ArrayList<Integer> array2;
        ArrayList<Integer> array3;

    public  int addRandom(){
        Random rnd = new Random();
        int b=0;
        for (int i=0; i<26; i++){
            int rando = rnd.nextInt(101);
            if (checkRandom.indexOf(rando) != -1){
                return addRandom();
            }
            else{
                checkRandom.add(rando);
                array4.add(rando);
                return (rando);
            }
        }
        for (int j=0;j<26;j++){
            int right;
            right = checkRandom.get(j);
            System.out.println(right);
        }
        return -1;
    }





    public static void main(String args[]){
        arrayList randomGen = new arrayList();
        randomGen.addRandom();
    }
}


Exception in thread "main" java.lang.NullPointerException
at arrayList.addRandom(arrayList.java:14)
at arrayList.main(arrayList.java:37)

5 个答案:

答案 0 :(得分:2)

我建议您使用Java 8流更简单的方法。例如,要创建一个由0到100之间的26个不同随机整数组成的数组:

int[] randomArray = new Random().ints(0, 101).distinct().limit(26).toArray();

为了更详细地解释一下,这个语句可以解释为:创建一个随机数生成器,用它来生成0到100之间的无穷无尽的随机数流,删除任何重复项,获取前26个数字流并将它们转换为int数组。

Streams非常强大。一旦您的生成器处于此格式,添加sorted运算符或filter或将其收集到ListMap中是微不足道的。

答案 1 :(得分:0)

你的方法

public  int addRandom(){
    Random rnd = new Random();

    for (int i=0; i<26; i++){
        int rando = rnd.nextInt(101);
        if (checkRandom.indexOf(rando) != -1){
            return addRandom();
        }
        else{
            checkRandom.add(rando);
            array4.add(rando);
            return (rando);
        }
    }
}

最后没有return语句。方法签名表明您必须返回一个整数。编译器不知道for语句将在运行时执行。因此,您必须处理不执行for循环的情况。既然你可以告诉它每次都会执行,添加一个返回-1;在方法结束之前将解决您的问题。

public  int addRandom(){
    Random rnd = new Random();

    for (int i=0; i<26; i++){
        int rando = rnd.nextInt(101);
        if (checkRandom.indexOf(rando) != -1){
            return addRandom();
        }
        else{
            checkRandom.add(rando);
            array4.add(rando);
            return (rando);
        }
    }
    return -1;
}

您可以通过创建类的实例来调用该方法,即

arrayList randomGen = new arrayList();
randomGen.addRandom();

顺便说一句,它在java中的标准是将你的类命名为CamelCased。即ArrayList。虽然,您可能希望将其重命名为其他内容,这样您就不会将您的类与java.util.ArrayList(一种流行的java类)混淆。

答案 2 :(得分:0)

char[] colors; //Array of characters to be translated into hexadecimal String r = ""; //Red value of color String g = ""; //Green value of color String b = ""; //Blue value of color int x = 0; //X-coordinate of rectangle int y = 0; //Y-coordinate of rectangle int q; //Character count ClickableRectangle[] rects = new ClickableRectangle[400*400]; //Rectangles void settings() { size(displayWidth, displayHeight); } void setup() { background(0); colors = new char[3]; String s = ([INSERT TRANSCRIPT HERE]); //Too long to be in post// for (int i = 0; i < s.length(); i+=3) { for (int j = i; j < i+3; j++) { colors[j-i] = s.charAt(j); } r = hex(colors[0], 2); g = hex(colors[1], 2); b = hex(colors[2], 2); drawAPoint(r, g, b, i); println(i); q++; } save("SlachtochtFeuf.png"); //Ignore this, using for testing purposes println("q = " + q); println("x = " + x); println("y = " + y); } void draw() { for (int i = 0; i < rects.length; i++) { if (rects[i].isClicked()) { println(rects[i].getValue()); //Prints char representation of color } } } void drawAPoint(String r2, String g2, String b2, int i) { noStroke(); fill(unhex(r2), unhex(g2), unhex(b2)); rects[i] = new ClickableRectangle(x, y, r2, g2, b2); rects[i].display(); if (x >= width) { x = 0; y += 6; } else { x+=6; } } class ClickableRectangle { int x = 0; int y = 0; String r = ""; String g = ""; String b = ""; public ClickableRectangle(int x, int y, String r, String g, String b) { this.x = x; this.y = y; this.r = r; this.g = g; this.b = b; } public void display() { fill(unhex(r), unhex(g), unhex(b)); rect(x, y, 6, 6); } public void setRGB(String r, String g, String b) { this.r = r; this.g = g; this.b = b; } public String getValue() { return ""+char(unhex(r))+char(unhex(g))+char(unhex(b)); } public boolean isClicked() { return mouseX > x && mouseY > y && mouseX < x+6 && mouseY < y+6; } } 方法中初始化方法局部变量 b ,并在for循环中重新分配,最后返回变量 b

addRandom

答案 3 :(得分:0)

如果你想使用递归,你不需要循环。例如:

1

答案 4 :(得分:0)

public static void main(String[] args) {
    ArrayList<Integer> list = new ArrayList<>();
    Random rand = new Random();

    while (list.size() < 25) {
      int index = rand.nextInt(101);

      if (!list.contains(index)) {
          list.add(index);
      }
    }
    System.out.println(list);
  }
}