Sorry for my bad English.
I write a game for Android. I simply need to generate the 4 numbers that are not repeated (all different).
This is my code, but it doesn't work :/
k = 3 in start
Random rand = new Random();
a = rand.nextInt(10);
b = rand.nextInt(10);
c = rand.nextInt(10);
d = rand.nextInt(10);
for (int i = 1; i < k; i++) {
if (a == b) {
b = rand.nextInt(10);
k++;
}
else {
k = 0;
}
}
for (int i = 1; i < k; i++) {
if (a == c || b == c ) {
c = rand.nextInt(10);
k++;
}
else {
k = 0;
}
}
for (int i = 1; i < k; i++) {
if (a == d || b == d || c == d) {
d = rand.nextInt(10);
k++;
}
else {
k = 0;
}
}
When I try to bring the number, they can be repeated.
make a List of 10 Integers [0, 1, 2, 3, ... 9] e.g. ArrayList, call Collections.shuffle() and take first four elements
java.util.Random random = new java.util.Random();
java.util.HashSet<Integer> ints = new java.util.HashSet<Integer>();
do {
ints.add(random.nextInt(10));
} while (ints.size()<4);
If you need non-trivial random number generation you need to review this for Android. http://android-developers.blogspot.com/2013/08/some-securerandom-thoughts.html
try this
import java.util.Random;
public class Test {
public static void main(String args[]){
Random rand = new Random();
int[] array = new int[4];
int count=0;
while(count < 4){
int randomNumber = rand.nextInt(10);
if(!contains(array,randomNumber)){
array[count] = randomNumber;
count++;
}
}
printArray(array);
}
public static boolean contains( int[] array, int key) {
boolean result = false;
for (int i = 0; i < array.length; i++){
if(array[i]== key)
result = true;
}
return result;
}
public static void printArray(int[] array){
for(int i=0;i<array.length;i++){
System.out.println(array[i]);
}
}
}
Related
`import java.util.Random;
public class Main {
public static void main(String[] args) {
int[][] board = new int[5][5];
int sNA = 5;
// new GUI();
sequenceMaker(board);
drawBoard(board);
}
//
public static void drawBoard(int[][] board2D) {
int m=1;
for(int i = 0; i < board2D.length; i++){
for(int j=0; j < board2D.length; j++){
System.out.print(board2D[i][j]+" ");
}
System.out.println();
}
}
public static void sequenceMaker(int[][] board2D) {
Random rand = new Random();
int m = 1;
int x = 0;
while(x < 24){
int columnRandom = rand.nextInt(5);
int rowsRandom = rand.nextInt(5);
x += 1;
if(board2D[columnRandom][rowsRandom] == 0) {
board2D[columnRandom][rowsRandom] = m;
m += 1;
}
else if(board2D[columnRandom][rowsRandom] == m) {
while(board2D[columnRandom][rowsRandom] == m) {
columnRandom = rand.nextInt(5);
rowsRandom = rand.nextInt(5);
board2D[columnRandom][rowsRandom] = m;
m+=1;
}
}
}
}
}`
This is what I wrote to this point, but the output doesn't include every index, maximally going to 15-16ish.
I tried a while loop, so that if another integer is in the place of randomally generated number, it generates those number once again. I don't know why my output is incomplete though.
I have solved my own question (I think).
What I have changed is that in the sequenceMaker() function, I have moved the x+=1 into the if statements. The code looks like this :
public static void sequenceMaker(int[][] board2D) {
Random rand = new Random();
int m = 1;
int x = 0;
while(x < 25){
int columnRandom = rand.nextInt(5); // random column
int rowsRandom = rand.nextInt(5); // random row
if(board2D[columnRandom][rowsRandom] == 0) {
board2D[columnRandom][rowsRandom] = m;
m += 1;
x += 1;
}
else if(board2D[columnRandom][rowsRandom] != 0) {
while(board2D[columnRandom][rowsRandom] == 0) {
columnRandom = rand.nextInt(5);
rowsRandom = rand.nextInt(5);
board2D[columnRandom][rowsRandom] = m;
m+=1;
x += 1;
}
}
}
}
I hope my mediocrity helped someone!
public static void sequenceMaker(int[][] board2D) {
//it will initialize your array with random integers
Random rand = new Random();
for(int i=0; i<board2D.length; i++){
for(int j=0; j < board2D[i].length; j++ ){
int randomNo = rand.nextInt(20); //you can change the bound as required
board2D[i][j] = randomNo;
}
}
}
I have this professor that wants us to make a using the method header
Public static int[] eliminateDuplicates(int[] arr)
The program uses a randomly generated array to see if there are duplications but the user gives a limit on how many indexes's the random array has. Here is what I have but it's not working.
import java.util.Random;
import java.util.Scanner;
public class Dublicates
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
Random generator = new Random();
System.out.println("Enter array length: ");
int[] a = new int[input.nextInt()];
for (int i = 0; i<a.length; i++)
{
a[i] = generator.nextInt(a.length*2);
}
int[] result = eliminateDuplicates(a);
System.out.println("The new numbers are: " + result.length);
System.out.println("The double numbers were:");
for (int b : result)
{
System.out.println(b + " ");
}
}
public static int[] eliminateDuplicates(int[] arr)
{
int[] temp = new int[arr.length];
int size = 0;
for (int i = 0; i < arr.length; i++) {
if (linearSearch(temp, arr[i]) == -1) {
temp[size] = arr[i];
size++;
}
int[] result = new int[size];
for (int i = 0; i < size; i++) {
result[i] = temp[i];
}
{
return result;
}
}
}
public static int linearSearch(int[] arr, int key)
{
for(int i = 0; i<arr.length; i++)
{
if (key == arr[i])
return i;
}
return -1;
}
}
As your statements making the final result is inside the for loop, the statements inside for will only run once and will not give the right answer.
So you have to change your code as follows.
public static int[] eliminateDuplicates(int[] arr)
{
int[] temp = new int[arr.length];
int size = 0;
for (int i = 0; i < arr.length; i++) {
if (linearSearch(temp, arr[i]) == -1) {
temp[size] = arr[i];
size++;
}
}
int[] result = new int[size];
for (int i = 0; i < size; i++) {
result[i] = temp[i];
}
return result;
}
The default values in the integer array is 0, The Random.nextInt() can generate 0 random value, When you run linear search then 0 will not be included in the final resultant array.
I have modify Random.nextInt() so that it will not generate 0 random number:
import java.util.Random;
import java.util.Scanner;
public class HelloCodiva
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
Random generator = new Random();
System.out.println("Enter array length: ");
int[] a = new int[input.nextInt()];
for (int i = 0; i<a.length; i++){
a[i] = generator.nextInt(a.length*2)+1;//So that 0 is not generated
}
int[] result = eliminateDuplicates(a);
for (int originalValue: a){
System.out.println(originalValue+ " ");
}
System.out.println("The new numbers are: " + result.length);
System.out.println("The double numbers were:");
for (int b : result){
System.out.println(b + " ");
}
}
public static int[] eliminateDuplicates(int[] arr)
{
int[] temp = new int[arr.length];
int size = 0;
for (int i = 0; i < arr.length; i++) {
if (linearSearch(temp, arr[i]) == -1) {
temp[size] = arr[i];
size++;
}
}
int[] result = new int[size];
System.arraycopy(temp, 0, result, 0, size);
return result;
}
public static int linearSearch(int[] arr, int key)
{
for(int i = 0; i<arr.length; i++)
{
if (key == arr[i])
return i;
}
return -1;
}
}
I need to execute positive and negative numbers from an array list. I also need to execute duplicates from array list. I will post my java code and hope someone can tell me why I can't run this code. Is there something missing in it? Thanks in advance.
public static void main(String[] args) {
int i, a, b;
int[] array1 = new int[20];//{12,23, -22, 0, 43,545, -4, -55,43, 12,0, -999, -87
array1[0] = 12;
array1[1] = 23;
array1[2] = -22;
array1[3] = 0;
array1[4] = 43;
array1[5] = 545;
array1[6] = -4;
array1[7] = -55;
array1[8] = 43;
array1[9] = 12;
array1[10] = 0;
array1[11] = -991;
array1[12] = -87;
int[] arrayPlus = new int[20];
int[] arrayMinus = new int[20];
a = b = 0;
for (i = 0; i < 13; i++) {
if (array1 > 0 || array1 == 0) {
arrayPlus[a] = array1;
a++;
} else {
arrayMinus = array1;
b++;
}
}
System.out.println("Positive array numbers");
for (i = 0; i < a; i++) {
System.out.println(arrayPlus);
}
System.out.println("");
System.out.println("Negative array numbers");
for (i = 0; i < b; i++) {
System.out.println(arrayMinus);
}
}
}
You are comparing whole table array1 with integer. You can't do that. You should compare only one element of the array with 0. That mean you should use array1[i] instead.
Try to change this block:
if (array1 > 0 || array1 == 0){
arrayPlus[a] =array1;
...
{arrayMinus =array1;
...
}
for (i = 0; i < a; i++) {
System.out.println(arrayPlus);}
System.out.println("Negative array numbers");
for (i = 0; i < b; i++) {
System.out.println(arrayMinus);}
}
With this:
if (array1[a] > 0 || array1[a] == 0){
arrayPlus[a] =array1[a];
...
{arrayMinus[a] =array1[a];
...
for (i = 0; i < a; i++) {
System.out.println(arrayPlus[a]);}
System.out.println("Negative array numbers");
for (i = 0; i < b; i++) {
System.out.println(arrayMinus[b]);}
}
And for more learn array go in link
Try to instead of code
for (i = 0; i < 13; i++) {
if (array1 > 0 || array1 == 0) {
arrayPlus[a] = array1;
a++;
} else {
arrayMinus = array1;
b++;
}
}
Use following code:
for (i = 0; i < 13; i++) {
if (array1[i] > 0 || array1[i] == 0) {
arrayPlus[a] = array1[i];
a++;
} else {
arrayMinus[b] = array1[i];
b++;
}
}
You should work with elements of arrays (array1[i], arrayMinus[b]) not with whole arrays (array1, arrayMinus). Some problem with code:
for (i = 0; i < a; i++) {
System.out.println(arrayPlus); // use arrayPlus[i]
}
System.out.println("");
System.out.println("Negative array numbers");
for (i = 0; i < b; i++) {
System.out.println(arrayMinus); // use arrayMinus[i]
}
import java.util.*;
public class Zhangbubble
{
public static void main (String[] args)
{
int Bub[] = new int[6];
Random randy = new Random();
boolean Done = false;
for (int x=0; x<6; x++)
{
Bub[x] = randy.nextInt(100);
System.out.println (Bub[x]);
}
System.out.println ("This is the original array");
while (! Done)
{
Done = true;
for (int x = 0; x<Bub.length-1; x++)
{
if(Bub[x+1] > Bub[x])
{
int temp = Bub[x];
Bub[x] = Bub[x+1];
temp = Bub[x+1];
Done = false;
}
else
{
Done = false;
}
}
for(int x = 0; x<6; x++)
{
System.out.print(Bub[x]+" ");
}
}
}
}
So my programming teacher asked us to make a bubble sort in java using a boolean. His example shows the code in a while loop with for loops. This code is suppose to continuously sort until it has the numbers in the array organized from least to greatest. However, I'm really lost and I can't seem to figure out where I'm going wrong. Any help would be greatly appreciated!
The problem is in your switching algorithm. You are assigning temp twice.
int temp = Bub[x];
Bub[x] = Bub[x+1];
temp = Bub[x+1]; //Here should assign Bub[x+1] to temp
//Example: Bub[x+1] = temp
edit-Actually, there could be some improvement in the sorting algorithm itself, too. Personally, I like to do it this way:
public class Sort {
private static int[] array = { 3, 8, -1, 7, 0, 3 };
public static void main(String[] args) {
for(int i = 0; i < array.length - 1; i++) {
for(int j = i + 1; j < array.length; j++) {
if(array[i] > array[j]) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
for(int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
}
}
this working
public static void main(String[] args) {
int Bub[] = new int[6];
Random randy = new Random();
boolean Done = false;
for (int x=0; x<6; x++)
{
Bub[x] = randy.nextInt(100);
System.out.println (Bub[x]);
}
System.out.println ("This is the original array");
while ( ! Done)
{
for (int x = 0; x<Bub.length-1; x++)
{
if(Bub[x+1] > Bub[x])
{
int temp = Bub[x];
Bub[x] = Bub[x+1];
Bub[x+1]=temp ;
Done = false;
}
else
{
Done = false;
}
}
for(int x = 0; x<6; x++)
{
System.out.print(Bub[x]+" ");
}
Done = true;
}
}
im new to this but im tring to do a hotel reservation program.
So i have a 2D int array with ALL rooms, when i start the program i want them to be randomly shuffle in to an array called RoomNotInUse or RoomInUse (so evertime i start the program the rooms are randomly generated.
Would be awesome if anyone know a way around this :)
// ARRAYS
protected static int[][] rooms = {
{1,1}, {1,2}, {1,3}, {1,4}, {1,5},
{2,1}, {2,2}, {2,3}, {2,4}, {2,5},
{3,1}, {3,2}, {3,3}, {3,4}, {3,5},
{4,1}, {4,2}, {4,3}, {4,4}, {4,5},
{5,1}, {5,2}, {5,3}, {5,4}, {5,5}
};
//Declare all hotel rooms 5x5, the first number is the floor and the sec is the room
private char[][] ROIU = {
};
//Rooms not in use
private char[][] RIU = {
};
//Rooms in use
public class roomShuffle {
}
//Shuffle all rooms in 2 diffrent arrays, ROIN and RIU
public class RoomNotInUse {
}
//Displayes all the rooms thats not in use
public class RoomInUse {
}
//Displayes all rooms in use
}
You can use Fisher–Yates algorithm modified for two-dimensional arrays:
void shuffle(int[][] a) {
Random random = new Random();
for (int i = a.length - 1; i > 0; i--) {
for (int j = a[i].length - 1; j > 0; j--) {
int m = random.nextInt(i + 1);
int n = random.nextInt(j + 1);
int temp = a[i][j];
a[i][j] = a[m][n];
a[m][n] = temp;
}
}
}
Assign all array into list. Than use Collections.shuffle().
List<int[]> pair=new ArrayList<int[]>();
pair.addAll(Arrays.asList(rooms));
Collections.shuffle(pair);
You can use shuffle-
Collections.shuffle()
Here's a tutorial that describes with or without Collections.
A generic shuffle method in Java should me similar to this
The important thing is that you have to swap an item with a random element that comes after in the collection ;)
public void shuffle(Comparable [] a){
for(int i=0;i,a.length;i++)
swap(a,i,getRandom(i,a.length-1);
}
private int getRandom(int min, int max){
Random rnd = new Random();
return min + rnd.nextInt(max-min+1);
}
private void swap(Comparable [] a, int i, int j){
Comparable temp = a[i];
a[i]=a[j];
a[j]=temp;
}
Otherwise you can use the Collection.shuffle method.
Scanner scanner = new Scanner(System.in);
int n;
int m;
int selection;
System.out.println("Enter number of team");
n = scanner.nextInt();
m = (int) Math.sqrt(n);
int[][] matrix = new int[m][m];
List<Integer> listMatrix = new ArrayList<Integer>();
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
matrix[i][j] = i * m + j + 1;
System.out.print(matrix[i][j] + "\t");
}
System.out.println();
}
for (int i = 0; i < m * m; i++) {
listMatrix.add(i + 1);
}
System.out.println();
do {
System.out.println("what line is the card?");
selection = scanner.nextInt();
} while (!(selection >= 1 && selection <= m));
selection -= 1;
int[] values = new int[m];
for (int i = 0; i < m; i++) {
values[i] = matrix[selection][i];
// System.out.print(values[i] + "\t");
}
System.out.println();
Collections.shuffle(listMatrix);
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
matrix[i][j] = listMatrix.get(j + i * m);
System.out.print(matrix[i][j] + "\t");
}
System.out.println();
}
scanner.close();