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 1 year ago.
Improve this question
I need to populate an array with random numbers from -50 to 50 and need to use Math.random(). Here is my current code:
double[] randomNums = new double[5];
for (int i = 0; i < randomNums.length; i++) {
randomNums[i] = 100 * Math.random() - 50;
}
for (double i: randomNums) {
System.out.println(i + ',');
}
Output:
-5.836717454677796,
44.07635593282988,
23.650145270722884,
93.00810678750743,
54.0536237451922
Why is this going above 50?
You are adding the value of the char ,, which has value 44 to each double. You can test this using:
System.out.println(0 + ',');
Output: 44
To fix this you can simply remove the + ','
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 10 months ago.
Improve this question
49 5 8 14 3 7 6 21
It gives the rank of the smallest number whose remainder is 0 after dividing by 7 in the above given index.
create the algorithm.
I WAS TRY BUT I FAİLED.. I'M YET NEW TO JAVA ..
Here's some hints:
To test if an integer named x is divisible by 7:
if (x % 7 == 0) {
// x is divisible by 7
}
To enumerate all the items in an array name arr
for (int i = 0; i < arr.length; i++) {
}
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 1 year ago.
Improve this question
Store the result in the variable
How do I store the result of number % 4 = 0 in a variable?
Like this: number1 = (number2 % 4 = 0)
Not sure what do you want exactly. Here are some hints for you:
int quotient = divisor / divider;
int remainder = divisor % divider;
boolean isDivisibleByFour = number % 4 == 0; // true or false
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 need to enter N which is a number of numbers for which I need to see if they can be divided by 3. Afterwards I need to display what % of numbers from the N I can divide. Numbers need to go from 15 to 62 and they need to loop until I enter the right value each time, but they don't. Instead, they just repeat the for loop regardless of my input. Here is the code:
System.out.println("Enter N number of numbers");
int N = TextIO.getlnInt();
int number;
int counterOfDivisible = 0;
for(int i = 0; i < N ; i++) {
do {
System.out.println("Please enter a number from the 15-62 span");
number = TextIO.getlnInt();
} while (number<15 && number>62);
if(number%3==0)
counterOfDivisible++;
}
System.out.println("% of numbers from the N that can be divided by 3 is " + (counterOfDivisible*100.0)/N + "%");
number can never be <15 and >62 at the same time. Think about your condition.
Thank you all for the quick reply, first time here. Quite silly mistake on my behalf.
This seems to do the trick i was aiming for.
do {
System.out.println("Please enter a number from the 15-62 span");
number = TextIO.getlnInt();
} while (number < 15 || number > 62);
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 6 years ago.
Improve this question
I have an ArrayList of 4 items. I need to remove one item randomly and display the updated ArrayList. However my random keeps targeting the second and third element on the array list. As far as I understand my random would count like this: 0 1 2 3. Shouldn't that be enough to cover my 4 elements? Why does it keep targeting the same indexes? I have tried increasing the random number (4) + 1, but that puts me out of bounds.
Random rand = new Random();
Scanner input = new Scanner(System.in);
int numberOfGuests = 4;
ArrayList<String> guestList = new ArrayList<>(4);
System.out.println("Enter 4 guests:");
for(int i = 1; i <=numberOfGuests; i++){
System.out.printf("guest%d: ", i);
guestList.add(input.nextLine());
}
System.out.println("Guest List: " + guestList);
String remove = guestList.remove(rand.nextInt(4));
System.out.printf("%s can't come%n" , remove);
System.out.println("Guest List: " + guestList);
yoy check when you user random number generation the rage is set to length of array minus one such as in your case
String remove = guestList.remove(rand.nextInt(3));
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 8 years ago.
Improve this question
Why does the program show java.lang.ArrayIndexOutOfBoundsException: 11?
The program needs to print 11 numbers. 9 numbers should be random.
It prints 11 numbers but it has an error.
public class Main {
public static void main(String[] args) {
Random random = new Random();
int[] Cnum = new int[11];
Cnum[0]=0;
Cnum[1]=9;
System.out.print(Cnum[0]);
System.out.print(Cnum[1]);
for(int a = 2; 2 < 10; a++){
Cnum[a]=random.nextInt(9);
System.out.print(Cnum[a]);
}
}
}
The output should be (x is random) :
09xxxxxxxxx
But the output it does is:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 11
at CellphoneNumberGenerator.Main.main(Main.java:17)
09505423220
for(int a=2;2<10;a++)
That is basically a while(True) (infinite) loop because 2 is always less than 10. Perhaps you meant:
for(int a= 2; a<10; a++)
In your for cycle the condition is wrong.
You have 2 < 10 instead of a < 10