samedi 19 juin 2021

Generating a list of random integers (from -1000 to 1000), the number of integers in the list are determined by user input

I have looked through other questions but nothing seems to work. The user chooses how many numbers are in the list and they should be between -1000 and 1000. I tried using two for loops, one displaying the random number and another around that loop repeating the number of time the user enters. For now I tried to just get the loop to work with positive integers up to 1000. When using system print out it finally worked but using the GUI I just get one random number. I used an arraylist because I thought it'd let me add to it with each loop.

int a;
private void btnSortActionPerformed(java.awt.event.ActionEvent evt) {                                        
   try {
    a = Integer.parseInt(txtSort.getText());
   }catch
           (NumberFormatException numberFormatException) {
       lbl1.setText("Please enter a proper integer");     
   }
   ArrayList<Integer> list = new ArrayList<>();
    for (int i=1;i<=1000; i++) {
        list.add(i);
    }
   
    for (int i=0; i<a; i++) {
        txtpane1.setText(String.valueOf(list.get(i)));
    }}

txtSort is the textfield the program gets the number of integers and i'm using Apache NetBeans. Any help is much appreciated!




Aucun commentaire:

Enregistrer un commentaire