+ 1
Problem
Hey guys! Can you help me and tell me what ia wrong with tis code? public class Program { public static void main(String[] args) { int x=6; int y=15; int z; if(x<y) { z=x; } else{ z=y; } if(z%x<=0 && z%y<=0){ System.out.println("z"); z-=z; } } } I need the fix, and I cant find it, it sais "No output"
15 Respostas
+ 1
Hi,
your result is No output because you don't print anything
Explaination:
x = 6 and y = 15 => x<y then z = 6
you want test that :
if(z%x<=0 && z%y<=0)
so z%x => 6%6 = 0 then z%x <= 0 return true
and z%y => 6%15 = 6 then z%y <= 0 return false
true && false return false
your test is always false then you won't execute System.out.println("z");
What do you really want to test ?
+ 6
The code is fine and does what it should do.
It says no output because the if condition results in false.
if(z%x<=0 && z%y<=0)
The first condition is true because z is 6 and x is 6.
But the second one is false because z is 6 and y is 15.
So first one results in 0 but the second one results in 6
+ 1
Oh right and I put the z-=z in the wrong spot.
0
Vhy isnt it working if i put z-=z in the next layer?
0
I wanna test if x and y can be divided by same number (being z)
So i put z-=z one layer down and it still isnt working.
0
if you want test if x and y can be divided by same number (being z), change your modulo expression
if (x%z == 0) && (y%z == 0){
System.out.println("x and y are divising by z");
}
else {
System.out.println("both x and y are not divising by z");
}
i don't understand why you want do z -= z
this return z = 0
0
z-=z is there because i wanna get the number that can be divided by both, if z=6 is not dividable, it will try 5,4,3.... until it gets the number that can be dividable by both, in this case 3.
0
if (x%z == 0) && (y%z == 0){
msg = x + " and " + y + " are divising by " + z;
System.out.println(msg);
}
else {
System.out.println("both " + x + " and " + y + " are not divising by " + z);
z -= 1; // or z--;
}
0
i forgot but you must include this code within a while loop with condition z>0
0
Oh okay, Ill try
0
It worked! Thanks!
0
What
0
I published it, can you tell me the name for it, I am not the best in English ..
And I made so that it tell all dividable numbers.
- 2
like like ...
a lot of like please
- 2
PPCM in french
LCM in english
like = upvote