English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

So that they are in numerical order...

int[] array = new int [6];
int count = 0;
luckyNum = 1 + (int)(Math.random()*49);
array [count] = luckyNum;
jTextField1.setText(luckyNum+"");

count++;
luckyNum = 1 + (int)(Math.random()*49);
array [count] =luckyNum;
jTextField2.setText(luckyNum+"");

2007-03-22 07:01:18 · 3 answers · asked by Night_nurse 2 in Computers & Internet Programming & Design

3 answers

if u just wanna swap 2 values, u use a temp. variable:
temp=array[m];
array[m]=array[n];
array[n]=temp;
(hey, the guy above just wrote that!)

anyway, if u want to order the array, there is a well-known method called "bubble sorting" u'll need 2 loops (i prefer for-loops):

for (int i=arraySize; i>1;i++)
{
for (int j=1; j {
if (array[j-1]>array[j])
{
int temp=array[j-1];
array[j-1]-array[j];
array[j]=temp;
}
}
}
//this orders the elements in ascending order
//if u want it in descending order just change the
//relational operator

2007-03-22 07:21:52 · answer #1 · answered by Khaled Z 3 · 0 0

using a temporary variable.

int nTmpVar = array[n];
array[n] = array[m];
array[m] = nTmpVar;

2007-03-22 07:10:04 · answer #2 · answered by Carlos V 2 · 0 0

count now equals 1;
if(array[count]>=array[count-1]
{
tmp=array[count-1];
array[count-1]=array[count];
array[count]=tmp;
}

2007-03-22 07:24:18 · answer #3 · answered by freeze 2 · 0 0

fedest.com, questions and answers