Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I have a problem with arrays and assigning them to values like float. Here is my code
System.out.print("Please enter the number of iteration: ");
Scanner scn = new Scanner(System.in);
int inumber = scn.nextInt(); // i get the iteration number
Random rnd = new Random();
float x = -10 + rnd.nextFloat() * 20; // i created random number between 10 and -10
System.out.println("My x = " +x);
float[] xarray = new float[4]; // created my test array
Random arrayrnd = new Random();
for (int i=0;i<inumber;i++) { // created a for loop with that number
for (int j = 0; j<xarray.length;j++) {
xarray[j] = arrayrnd.nextFloat(); // also created random for array and assigned them
}
Arrays.sort(xarray); // i sorted the array
xarray[0] = x; // i tried to assign the smallest number to x but didn't work
System.out.println("t="+i+" My new x = " +x);
Also here is my output :
Please enter the number of iteration: 2
My x = -6.2841988
t=0 My new x = -6.2841988
t=1 My new x = -6.2841988
I just don't understand why my x hasn't changed even though I tried to assign the x with the new value. I want my x to change in every turn of the loop and got the the smallest number of the array. But it seems like x never wants to move. I'm sorry if my code is complicated or if I have any mistake. Happy coding!
If you want assign TO x, you should do:
x = xarray[0];
Instead of:
xarray[0] = x;
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am learning Java, at a very beginning level. I am trying to print the Dice result, so from 1 to 6.
//instance of random class
Random DICE = new Random();
int DiceRange = 7;
int RIGHT = DICE.nextInt(DiceRange);
int LEFT = DICE.nextInt(DiceRange);
System.out.println("Right Dice = " + RIGHT);
System.out.println("Left Dice = " + LEFT);
The issue with this code that "Zero" is also printed sometimes. I want to keep the range from 1 to 6, instead of 0 to 6.
I tried to do the following, but did not work:
int i = 0;
while (DiceRange == 0); {
i++;
}
The CPU went to 100% :)
So how to exclude zero from this?
From Random documentation:
public int nextInt(int bound)
Returns a pseudorandom, uniformly distributed int value between 0
(inclusive) and the specified value (exclusive)
So starting from your code you can solve your problem using :
Random DICE = new Random();
int DiceRange = 6;
int RIGHT = DICE.nextInt(DiceRange) + 1;
int LEFT = DICE.nextInt(DiceRange) + 1;
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have a loop that displays 10 values: 1.0, 1.1, 1.2, etc.:
int i = 0;
int x = 10;
for(i;i>x;i++)
{
System.out.println(x);
}
but instead of displaying the values, I want to put them in an array. How do I do that?
How about:
// You want x ints.
int x = 10;
// Make an array big enough to hold x ints.
int[] array = new int[x];
// Loop x times.
for(int i = 0; i < x; i++) {
// Put the next number into the array.
array[i] = i;
}
first your way in writing for loop is need to be more clean
it should :
for(int i=0; i > x; i++){
System.out.println(x);
}
second your boolean condition in for loop isn't true because x=10 is always bigger than i=0 so it won't print any thing.
third to put the values in array :
simply define array : int[] numbers = new int[size of array];
then put each value inside the index i of array :
numbers[i] = i;
finally for loop will be like:
for(int i=0; i < x; i++){
numbers[i] = i;
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I had the enemy as a array[] but I recently changed it to an arrayList[] but for some reason nothing shows up anymore, any ideas?
ArrayList<Character> enemy = new ArrayList<Character>(20);
Character enty;
String[] eny = {"Eny_.png" , "eny2.png", "eny3.png"};
for (int i = 0; i < enemy.size(); i ++) {
int ranX = 700 + (int)(Math.random() * 2000);
int ranY = 100 + (int)(Math.random() * 360);
int randE = (int)(Math.random() * 2);
enty = new Character(ranX, ranY,0,0,100,25);
enty.setImage(eny[randE]);
enemy.add(i, enty);
}
for (int i = 0; i < enemy.size(); i ++) {
int ranSpeed = -1 + (int)(Math.random() * -2);
System.out.println(ranSpeed);
enemy.get(i).setVelX(ranSpeed);
enemy.get(i).getVelX();
repaint();
}
}
Because the 20 in
ArrayList<Character> enemy = new ArrayList<Character>(20)
Is the initial capacity, not the initial size. So,
for (int i = 0; i < enemy.size(); i ++) {
is not doing anything because enemy.size() is zero.
The constructor new ArrayList<Character>(20) does not create an ArrayList of size 20, it constructs an empty ArrayList with an initial capacity of 20. So, in your first loop, enemy.size() equals 0.
If you want to add 20 enemies, just use the constant 20 in your loop.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
How can I randomly choose a single element out of a 2D array and fill that one element with char 'M'?
Are you looking for something like this?
// Your array of Char with the length of 5 by 5
char[][] arrayOfChar = new char[5][5];
// Generating a radom number with min being 0 and max being length - 1
Random rand = new Random();
int x = rand.nextInt(arrayOfChar[0].length);
int y = rand.nextInt(arrayOfChar[1].length);
// Random position in array assigned M
arrayOfChar[x][y] = 'M' ;
If you wanted an int array
// Your array of int with the length of 5 by 5
int[][] arrayOfInt = new int[5][5];
// Generating a radom number with min being 0 and max being length - 1
Random rand = new Random();
int x = rand.nextInt(arrayOfInt[0].length);
int y = rand.nextInt(arrayOfInt[1].length);
// Random position in array assigned number 8
arrayOfInt[x][y] = 8 ;
Maybe something like this?
public class random2DChar {
public static void main(String args[]) {
char[][] char2DArr = new char[10][10];
Random random = new Random();
int x = random.nextInt(char2DArr.length);
char2DArr[x][x] = 'M';
System.out.println(Arrays.deepToString(char2DArr));
}
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Suppose I have a txt file called "filename". The data inside is as following,
N
12 39
34 23
12 22
5 7
7 10
11 8
.
.
.
left column contains the x value of each point. Right column contains y value of each point. N is the number of lines Point data that follow. I need to extract all the Point data and store it in a data structure(such as List). Is there any way I can do that?
File file = new File(filepath);
BufferedReader br = new BufferedReader(file.getInputStream);
int n = Integer.parseInt(br.readLine());
for (int i = 0; i < n-1; i++) // reads n-1 points, if you have n points to read, use n instead of "n-1"
{
line = br.readLine();
StringTokenizer t = new StringTokenizer(line, " ");
int x = Integer.parseInt(t.nextToken());
int y = Integer.parseInt(t.nextToken());
// do whatever with the points
}
This would work for something like this as an input file,
3 // line 1
1 2 // line 2
3 4 // line 3
My solution using Scanner instead of BufferedReader/StringTokenizer:
Scanner scanner = new Scanner(new File("filename"));
int n = scanner.nextInt();
for (int i = 0; i < n; i++) {
int x = scanner.nextInt();
int y = scanner.nextInt();
// do something with the point or store it
}
It's probably not as fast, but it's much easier to read and write.