java - Sorting 5 generated numbers from lowest to highest -


this question has answer here:

i have project in school make simplified yatzy program , i've hit road bump. have generated 5 random dice throws , printed them out in 1 window, need sort them lowest highest , don't know how.
i've looked in forums 1 don't understand, feel free ask questions if don't understand. btw i'm swedish text in "" lines in swedish if don't understand.
code:

import javax.swing.*;  public class projekt1 {    public static void main(string[] args) {      joptionpane.showmessagedialog(null, "välkommen till johans yatzy");     joptionpane.showconfirmdialog(null, "vill du starta spelet?");       int[] tar = new int[5];      string output = "";     for(int i=0; i<5; i++){     tar[i] = (int)(math.random()*6+1);     output = output + tar[i] + "\t";     }     joptionpane.showmessagedialog(null, "dina tärningskast blev följande: " + output);      }  } 

use

java.util.arrays.sort(tar); 

to sort int array:

for(int = 0; < tar.length; i++){     tar[i] = (int)(math.random()*6+1); }  // sort tar array java.util.arrays.sort(tar);  (int = 0; < tar.length; i++) {     output = output + tar[i] + "\t"; } 

Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -