Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm doing a task where you input 3 parameters which are size, minimum value and maximum value. It then is meant to return a random number between the minimum and maximum value of the size inputted. There are also other validations such as if the min is more than the max which I've already done.

I am able to do the task using integer instead of short. As soon as I change the data type a bunch of errors come.

Below is what I've done so far, it works as expected but I am pretty sure that there is not meant to be a bottom return null, I get errors when I delete it. On the second loop, it should return the array instead of doing a system print line. The other issue is the data types at the top, it should be short maxVal and short minVal instead of int but I can't get it to work with short.

I would very much appreciate all help. Thanks!

    public static ArrayList<Short> RandomArray1(int n, int maxVal, int minVal){
        

        
        if(n <= 0) {
            return null;
        }
        
        if(minVal > maxVal) {
            return new ArrayList<Short>();
        }
        
        ArrayList<Integer> ran = new ArrayList<Integer>();
        
        Random rand = new Random(); 
        
        
        for(int i = 0; i < n; i++) {
            
            int result = rand.nextInt(maxVal-minVal) + minVal;
            //System.out.println(result);
            ran.add(result);
            

        }
        
        for (int i = 0; i < ran.size(); i++) {
            System.out.println(ran.get(i));
            
            //return (ArrayList<Short>)ran.get(i);

        }
        

        return null; 


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
213 views
Welcome To Ask or Share your Answers For Others

1 Answer

I would do it like this.

  • first, method names by convention should start with lower case letters.
  • Use the method to generate the values and return the list
  • return interface types as opposed to implementation types (e.g. List)
  • throw exceptions if the arguments don't satisfy the requirements.

Note, having to cast the arguments to shorts is cumbersome but it prevents errors at compile time. Otherwise you may want to throw an additional run time exception if the values aren't within Short.MIN_VALUE and Short.MAX_VALUE.

public class RandomShorts {
    
    public static void main(String[] args) {
         List<Short> shortList = randomList(20, (short)200, (short)99);
         shortList.forEach(System.out::println);
    }
    
    public static List<Short> randomList(short n, short maxVal,
            short minVal) {
        
        if (n <= 0 || minVal >= maxVal) {
            throw new IllegalArgumentException(
                    "
n must be > 0
minVal must be < maxVal
");
        }
    
        List<Short> ran = new ArrayList<>();
        
        Random rand = new Random();
        for (int i = 0; i < n; i++) {
            short result =
                    (short) (rand.nextInt(maxVal - minVal) + minVal);
            ran.add(result);
        }
        return ran;
    }
}

If you just want to return a single random number using the supplied arguments, they you can do it like this.

public static short randomShort(int n, short maxVal, short minVal) {
     return (short)((Math.random()*(maxVal - minVal))+minVal);
} 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...