Store the result in the variable [closed] - java

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

Related

Java Random Double Array from -50 to 50 [closed]

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 + ','

stuck with this while loop java [closed]

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 4 years ago.
Improve this question
I have been working with while loops however I cant seem to understand how the remainder works inside this code.
int a = 10;
while( a <= 1000 && a % 100 != 0){
System.out.println("a = " + a);
a = a + 10;
}
a & 100 != 0
Performs bitwise and , then compares the result to 0. It will be false even in the first iteration, since 10 & 100 = 0

Do while loop nested in for loop wont iterate [closed]

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);

Can you get a number from doing maths with a NaN? [closed]

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 7 years ago.
Improve this question
Say I had double x = 0.0/0.0;.
Is there anything I could do with x in order to get an actual number?
Dividing by itself/0/infinity? Subtracting by something? Anything like that.
You can go through each JLS chapter for each of +, -, *, / and % and you'll read
If either operand is NaN, the result is NaN.
Using the value NaN with any of those would always produce NaN.
Is there anything I could do with x in order to get an actual number?
I'm assuming you meant with the operators above.
I think, we should prevent any number divided by zero instead.
EDIT
avg = 0;
count = 0;
for (number in scores) {
avg += number;
count++;
}
if (count != 0){
return avg / count;
} else {
return 0.0;
}

Random Array Error [closed]

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

Categories