Getting the output

Im getting the wrong output of 1, the output should be 101. what is im doing wrong?

import java.util.*;
public class walt2
{
   
public static void main(String[] args)
{

int x=1, y=6, z=2;
int p =0,q=0,r=0;
if(x+y+z<=10)
p++;
else
q++;
r++;
System.out.println(p);

}
}
walt2.JPG
waltbaby315Asked:
Who is Participating?
 
TomasPCommented:
Ok, that is what is expected. Can you post the problem proposed as stated. Per your original code, the answer is 1 and not 101
The only way I can see 101 as and answer is if you print
p,q, r out.
that would result in
101
0
 
TomasPCommented:
You have set p to 0 and incremented it by 1 if(x+y+z<=10)
Since 9 is less than or equal to 10 then p will be incremented from zero to one and you are printing out p
Where would 101 come from?
0
 
Bryan ButlerCommented:
Sounds like you're trying to do binary addition with integers.  Is binary what you want?
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
waltbaby315Author Commented:
It's suppose to be the answer after the values of p,q and r is executed. I was  wondering how they came up with 101.
0
 
TomasPCommented:
It looks like part of the problem is missing. Even a binary dump doesn't come up with 101.  A binary dump of the total of x,y,z does result in binary 1001
You should put your conditional code in {} to make the logic clearer since you are new
did they mean....
if(x+y+z<=10) {
p++;
}
else {
q++;
}
r++;
0
 
waltbaby315Author Commented:
I put in thast solution and this is the error that I got.
walt21.JPG
0
 
TomasPCommented:
Please post the whole file. It looks like you have one too many {
0
 
waltbaby315Author Commented:
I put in thast solution and i still get  the output of 1

import java.util.*;
public class walt2
{
   
public static void main(String[] args)
{

int x=1, y=6, z=2;
int p =0,q=0,r=0;
if(x+y+z<=10) {
p++;
}
else {
q++;
}
r++;
System.out.println(p);

}
}
walt2.JPG
0
 
waltbaby315Author Commented:
how would I print p,q,r. I tried System.out.println(p)(q)(r); and System.out.println(p,q,r); and they both came up with errors.

0
 
TomasPCommented:
you can try splitting it up
System.out.println(p);
System.out.println(q);
System.out.println(r);
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.