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
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 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
Given this code:
int p,k=8;
p=k*(++k-8);
System.out.println(p);
when ++k is evaluated k=9 and then that becomes k*(9-8) giving 9*1
int p,k=8;
p=(++k-8)*k;
System.out.println(p);
But this gives 9 as output
You have a multiplication with
left side: k
right side: (++k-8)
As you correctly stated, braces have precedence. However, your program still runs "from left to right". So first the left side is evaluated, which is k = 8. Afterwards the right side is evaluated, which is (++k-8) = 1.
Now we have determined both sides and can multiply them together: 8*1 = 8.
this is the class file your code compiled:
int k = 8;
byte var10000 = k;
int k = k + 1;
int p = var10000 * (k - 8);
System.out.println(p);
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;
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I am doing some experiments on my own about quantization processes etc.
I try to implement a binarization process which makes a "binary string" which will get processed by xor afterwards and some other stuff.
Anyhow the binarization is the following, where d and u are some numbers that will get compared:
String b = "";
for (int i = 0; i < u.length; u++) {
if(d[i] < u[i]) {
b[i] += '0';
} else {
b[i] += '1';
}
}
Currently like described I have a string where each character is 0 or 1.
Using a BigInteger gives me an Object where I can XOR two values against each other:
BigInteger bi = new BigInteger(b, 2);
(...)
BigInteger result = bi.xor(other_bi);
Is there another way to achieve what I want to do? I didn't find anything but maybe there is one I have not found?
The BitSet class is more appropriate for representing a sequence of bits. To set a bit you would use the BitSet.set method.
Taking your example as a BitSet.
BitSet bs = new BitSet(u.length);
for (int i = 0; i < u.length; u++)
if(d[i] >= u[i])
bs.set(i);
BitSet bs2 = ...
bs2.xor(bs); // changes and reuses bs2.
int bitsSet = bs2.cardinality();
int firstSetBit = bs2.nextSetBit(0);