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 9 years ago.
Improve this question
Why does this statement does not work
boolean b = (y==3-x)||(y==3)||(y=3+x);
but this one does
boolean b = (y==3-x)||(y==3);
b = b || (y == x-3);
and && statement has no problems with number of arguments passed
boolean b = x < 7 && x >= 0 && y < 7 && y >= 0;
Because in the first case:
boolean b = (y==3-x)||(y==3)||(y=3+x);
You are doing the assignment not comparison for (y=3+x)
Change it to:
boolean b = (y==3-x)||(y==3)||(y==3+x);
and it will work for you
However in the second case:
boolean b = (y==3-x)||(y==3);
b = b || (y == x-3);
You are doing the comparison everywhere thats why it is working for you!
Also in the third case you are doing comparison
boolean b = x < 7 && x >= 0 && y < 7 && y >= 0;
NOTE:-
== is for comparison and = is for assigment.
<,>,<=,>=, == are all used for comparison
You missed an equals sign, meaning the last parenthesis assigns 3+xto b, evaluates to int rather than boolean and so can't be used for a logical OR expression. This works, though:
boolean b = (y==3-x)||(y==3)||(y==3+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
In Java, the following code:
long x = 123;
String s = "abc" + x;
takes a significantly more runtime than:
long x = 123;
String s = "abc" + String.valueOf(x);
I got to know this through leetcode. I was trying to solve the following problem: https://leetcode.com/problems/fraction-to-recurring-decimal/
Here is the exact code for my solution:
public String fractionToDecimal(int numerator, int denominator) {
long n = numerator, d = denominator;
boolean isNegative = (n * d < 0);
if(n < 0) n = -n;
if(d < 0) d = -d;
long q = n / d;
long r = n % d;
if(r == 0) return (isNegative ? "-" : "") + q;
StringBuilder sb = new StringBuilder();
if(isNegative) sb.append('-');
sb.append(q).append('.');
Map<Long, Integer> found = new HashMap<>();
int index = sb.length();
while(r > 0 && !found.containsKey(r)){
found.put(r, index++);
n = r * 10;
q = n / d;
r = n % d;
sb.append(q);
}
if(r > 0) {
sb.insert(found.get(r), "(");
sb.append(')');
}
return sb.toString();
}
When I click on Submit it takes as long as 7 milliseconds to complete.
But if I literally just change line no. 8 from + q to + String.valueOf(q) the runtime plummets down to just 1 millisecond. Please feel free to copy paste the code on leetcode to try it out there and see this change in runtime for yourself, if necessary.
This is highly confusing to me. Why is this happening? As per my understanding, in both the cases, compiler first converts the long to a String, and then concatenates those two Strings together, right? So, under the hood, isn't concatenating a String and a long exactly the same as concatenating two Strings together? Then why does one take more time to run than the other? Any insight would be highly appreciated. TIA.
Note: this answer was written before the question was changed. It used to include the expressions shown below.
"abc" + 123 is a constant expression - the concatenation is done at compile-time so "abc123" ends up in the constant pool.
"abc" + String.valueOf(123) is not a constant expression. The concatenation happens at execution time, which is obviously rather more expensive than just using the compile-time concatenation result.
So I'd expect the result to be the opposite of what you've actually reported in the question.
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 5 years ago.
Improve this question
I executed the code and output was 19, but I don't understand why.
public static void main(String[] args)
{
int x = 0;
x = (x = 1) + (x = 2) * (++x) * (x++);
System.out.println(x);
}
You evaluate the operands from left to right, and then evaluate the multiplication operators before the addition operator:
x = (x = 1) + (x = 2) * (++x) * (x++);
1 + (2 * 3 * 3 ) = 19
assignment pre post
operator increment increment
returns the returns the returns the
assigned value incremented value before
value it was incremented
its evaluated like this -
1+2*3*3
(x=1) - first x is set t 1
(x=2) - then x is set to 2
(++x) - then x is incremented to 3; pre-increment and affects the equation in this case
(x++) - the last was post increment; no effect on the equations
as per my knowledge:
1 + 2*3*3 = 19
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
With Java, how would one find the greatest two numbers of a set of 3 numbers without using if conditionals.
For example given the 3 numbers {2,3,5}
int a = 2;
int b = 3;
int c = 5;
int total;
total would be replaced with the value of c+b = 8
List<Integer> data = Arrays.asList(23,6,13);
Collections.sort(data);
Collections.reverse(data)
data = data.sublist(0,2);
System.out.println(data);
One line:
int biggest = (a > b ? a : b) > c ? (a > b ? a : b) : c;
Two lines:
int firstStep = (a > b ? a : b);
int biggest = firstStep > c ? firstStep : c;
Java 8:
int max = Arrays.stream(numbers).max().getAsInt();
int sec_max = Arrays.stream(numbers).filter(i -> i != max).max().getAsInt();
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
I don't know how to write code in Java to check if x and y are both strictly between 0 and 1.
I have tried a lot of code, but it doesn't work. Can I get some help?
Without much to go on:
double x = getX();
double y = getY();
if ( x > 0 && x < 1 && y > 0 && y < 1 ) {
// TODO
}
Edit: Removed equality validity since the question mentioned "strictly".
double y = 0.1,x = 0.2;
if ( ( x < 1 && x > 0) && ( y < 1 && y > 0 ) )
{
// than do whatever
}
That should work if you do not include 1 and 0 as valid values
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 8 years ago.
Improve this question
Simple password maker
Random myrand = new Random();
int x = myrand.nextInt(126);
x = x+1;
char c = (char)x;
//this gets the character version of x
for (int mycounter = 0; mycounter < 10; mycounter++)
{
x = myrand.nextInt(127);
x = x+1;
if (x == 32)
{
x = 33;
}
c = (char)x;
System.out.print(c);
}
System.out.println();
Where is my error?
How do I fix this?
Currently you're detecting the space character, but what about other characters which aren't printable? I wouldn't expect anything lower than ASCII 32 to be useful as a password.
I suspect you want something like:
// Remove bit before the loop which used x and c. It was pointless.
Random myrand = new Random();
for (int mycounter = 0; mycounter < 10; mycounter++)
{
// Range [33, 127)
int x = myrand.nextInt(127 - 33) + 33;
char c = (char) x;
System.out.print(c);
}
Rather than map "everything non-printable" to a single character, the code above just avoids picking it in the first place, by restricting the range further.