What does the colon ":" and the question mark "?" operators do? [duplicate] - java

This question already has answers here:
What is a Question Mark "?" and Colon ":" Operator Used for? [duplicate]
(7 answers)
Closed 8 years ago.
What does the following line do? Could someone help me write this line in "normal" code?
int change = (Math.random() - 0.5 < 0 ? -5 : 5);

This is a ternary operator the way it works is :
condition ? (things to do if true) : (things to do if false);
In your code what it does is :
if value of Math.random() - 0.5 < 0
then assign change a values of -5
else
assign change a value of 5.

This line takes a random number (between 0 and 1) and subtracts 0.5. If that value is less than 0 then change is set to -5, otherwise 5.
int change;
if((Math.random() - 0.5) < 0)
{
change=-5;
}
else
{
change=5;
}

Related

n = n--; why same value as before [duplicate]

This question already has answers here:
Difference between b=b++ and b++ [duplicate]
(4 answers)
What is x after "x = x++"?
(18 answers)
Closed 1 year ago.
The title is self-explanatory. Consider the following code:
int n = 5;
n = n--;
It gives n = 5.
As far as I understood the expression n-- is first evaluated, returning 5 (i.e. POSTdecrement). This expression gets assigned to the LHS, here n.
After this execution, n gets diminished. Thus I expected n = 4.
Why don't I see 4?
n-- yields 5 and then sets n to 4
n = sets n to the value of the right-hand expression, which is 5
Mixing the increment/decrement operators with an assignment to the same variable rarely does anything useful.
This is not fully correct, there is 2 forms to do it --n and n--, thats is where you will do the rest on N.
With the --n before you first rest and then return the value, and the other side is the opposite.
You first pass the value and then rest the value, try doing it the other way.
Given n=5, n-- return 5 but n value is 4.
Given n=5, --n return 4 and n value is 4.
That's why n has still the same vlaue

Conditional operator in Java [duplicate]

This question already has answers here:
How does the ternary operator work?
(12 answers)
Closed 5 years ago.
Executing the following program is producing the following result, please explain the reason why the value of sum is 120 and of price is 100.
double sum=10, price =100;
sum+=price>=100?price * 1.1 : price;
Syso(sum);
Syso(price);
The output is
120
100
expression ? value 1 : value 2
if expression is true, result is value 1 (first condition)
if expression is false ,result is value 2(second condition)
Example:
x = 1, x > 10 ? x : 10
expression: x > 10 => false
value 1: x
value 2: 10
result is 10

Random Number with Java [duplicate]

This question already has answers here:
How do I generate random integers within a specific range in Java?
(72 answers)
Closed 6 years ago.
I really want some Java that randomizes from 1-2 and all that I came up with (Doesn't work either) is:
random.math (int.1+2)
Might actually look stupid to an expert but yeah
Since you're just wanting to flip between two integers, You can utilize a ternary operator and the Math.random() static method to achieve the desired results:
Math.random() >= 0.5 ? 2 : 1
The easiest would be to use the Random.nextInt(int n) method, which will generate an integer between 0 and n-1, inclusive.
Here is an example:
Random rnd = new Random();
for (int i = 0; i < 20; i++) {
int number = rnd.nextInt(2) + 1;
System.out.print(number + " ");
}
OUTPUT
1 2 1 1 2 1 1 2 2 1 2 2 2 2 1 2 2 2 1 1

Why does this expression keep evaluating to 0 [duplicate]

This question already has answers here:
Why does int exp1 = 14/20*100; equals '0' in java?
(4 answers)
Closed 7 years ago.
The following value1 keeps evaluating to 0, even though player1Disks and player2Disks are all above zero and not equal to one another.
int value1 = Math.round(100 * ((player1Disks - player2Disks)/(player1Disks + player2Disks)));`
Here are some example inputs for player1Disks and player2Disks respectively:
[5,8], [6,8], [8,4], etc.
However, all these are evaluating to zero. Am I missing something?
The integer division evaluates to 0 in the three cases you presented. 0*100 still equals 0.
You should multiply by 100 first, and then divide. Or better yet, don't use int.
First one: -3/13 = 0
Second one: -2/14 = 0
Third one: -4/12 = 0

Conditional Ternary Operations

First off, I came across this question recently and haven't being able to find a good explanation for it:
int x = (30 > 15)?(14 > 4) ? 1 : 0 : 2;
I have used ternary expression before so I am familiar with them, to be honest I don't even know what to call this expression... I think that it breaks down like this
if (con1) or (con2) return 1 // if one is correct
if (!con1) and (!con2) return 0 // if none are correct
if (con1) not (con2) return 2 // if one but not the other
Like I said I don't really know so I could be a million miles away.
Because of operator precedence in Java, this code:
int x = (30 > 15)?(14 > 4) ? 1 : 0 : 2;
will be parsed as if it were parenthesized as follows:
int x = (30 > 15) ? ((14 > 4) ? 1 : 0) : 2;
(The only operators with lower precedence than ternary ?: are the various assignment operators: =, +=, etc.) Your code can be expressed verbally as:
if (con1) and (con2) assign 1 to x
if (con1) and (not con2) assign 0 to x
otherwise assign 2 to x
EDIT: Nested ternary operators are often formatted in a special way to make the whole thing easier to read, particularly when they are more than two deep:
int x = condition_1 ? value_1 :
condition_2 ? value_2 :
.
.
.
condition_n ? value_n :
defaultValue; // for when all conditions are false
This doesn't work quite as cleanly if you want to use a ternary expression for one of the '?' parts. It's common to reverse the sense of a condition to keep the nesting in the ':' parts, but sometimes you need nesting in both branches. Thus, your example declaration could be rewritten as:
int x = (30 <= 15) ? 2 :
(14 > 4) ? 1 :
0 ;
It's int x = (30 > 15)?((14 > 4) ? 1 : 0): 2; :
if (30 > 15) {
if (14 > 4)
x = 1;
else
x = 0;
} else {
x = 2;
}

Categories

Resources