So the main idea is, the last number came on roulette machine is going to be on Index[0] and the element at index "whatNumberCame" has to become the number before
public class Main {
public static void main(String[] args) {
int[] rouletteNumbers = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36};
Scanner scanner = new Scanner(System.in);
System.out.println("Enter number : ");
int whatNumberCame = scanner.nextInt();
int collisionIndex;
for(int i = 0; i < rouletteNumbers.length ; i ){
if(rouletteNumbers[i] == whatNumberCame){
System.out.println("COLLISION AT " rouletteNumbers[i]);
collisionIndex = rouletteNumbers[i];
System.out.println(collisionIndex);
for (int j = collisionIndex 1; j <= 0 ; j--){
rouletteNumbers[j] = rouletteNumbers[j - 1];
System.out.print(rouletteNumbers);
}
}
}
}
}
for example if i enter 10 array should become : {10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36}
CodePudding user response:
You can tweak your code like this to make it works:
public static void main (String [] args) {
int[] rouletteNumbers = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36};
Scanner scanner = new Scanner(System.in);
System.out.print("Enter number : ");
int whatNumberCame = scanner.nextInt();
for(int i = 0; i < rouletteNumbers.length ; i ){
if(rouletteNumbers[i] == whatNumberCame){
for (int j = i; j > 0; j--){
rouletteNumbers[j] = rouletteNumbers[j - 1];
}
rouletteNumbers[0] = whatNumberCame;
break;
}
}
System.out.println(Arrays.toString(rouletteNumbers));
}
In your loop, you used j <= 0
which is always false
, so you inner loop doesn't execute at all.
And when you find the position of the number, you can break from the loop using break
.
CodePudding user response:
Here is a method that does the similar thing except it allows you to insert a value at whatever array index you like. With each value inserted into the array, the array will grow in length as indicated within your post example.
Usage:
To add a value to the array at index 0 (the beginning of the Array):
rouletteNumbers = insertIntoArray(rouletteNumbers, whatNumberCame, 0);
To add a value to the very end of the array:
rouletteNumbers = insertIntoArray(rouletteNumbers, whatNumberCame, rouletteNumbers.length);
To insert a value into any index within the array (say index 4):
rouletteNumbers = insertIntoArray(rouletteNumbers, whatNumberCame, 4);
The method code:
public static int[] insertIntoArray(int[] array, int valueToInsert, int insertIntoIndex) {
/* If the inertIntoIndex value is greater than the supplied Array Length
then the inertIntoIndex value is changed to the supplied Array Length.
This ensures that the valueToInsert will be place at the end of the
Array. */
if (insertIntoIndex > array.length) {
insertIntoIndex = array.length;
}
int[] tmp = new int[array.length 1];
int idxCounter = 0;
int i = 0;
for (; i < array.length; i ) {
if (i == insertIntoIndex) {
tmp[idxCounter] = valueToInsert;
idxCounter ;
}
tmp[idxCounter] = array[i];
idxCounter ;
}
/* when at this point in code, the condition below
indicates that the desired insert index location
would be at the very end of the array. */
if (i == idxCounter) {
tmp[idxCounter] = valueToInsert;
}
return tmp;
}