If Statement Comments
Comments
-
I dont get the questions. when i look at what the answer should have been im way off and i have been practicing for a while now.
-
so far .. I got everyone wrong.. I don't know if I am a math fool.. or if questions and programming examples are way off..
-
I have no idea what's going on. Where's the cheat button?
-
I modified the question slightly to make it clearer.
-
Your base code don't work: Main.java:13: error: illegal start of expression
public static void main(String[] args) {
^
Main.java:13: error: illegal start of expressionpublic static void main(String[] args) { ^
Main.java:13: error: ';' expected
public static void main(String[] args) {
^
Main.java:13: error: '.class' expected
public static void main(String[] args) {
^
Main.java:13: error: ';' expected
public static void main(String[] args) {
^
Main.java:23: error: reached end of file while parsing
}
^
6 errors -
Worked for me. But you're missing a semi colon in your score example.
-
Everything worked for me.
-
This is really hard :/
-
Bilbo Swaggins likes this
-
Meho the Wormcloack finds this easy
-
import java.util.Scanner;
public class Main
{public static int doStuff(int a) { if (a % 2 == 0) { return a/2; }
else if (a % 2 == 1) { return 3*a+1; } } public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); for(int i=0; i<n; i++) { int a = in.nextInt(); int result = doStuff(a); System.out.println(result); } }
}
I don't understand what's wrong, I get this compile error:
Main.java:16: error: missing return statement
}
^
1 error
But when I add the bracket, I get 8 compile errors for the following lines of code. -
i got a right answer but I returned error!!
if`( learner == collect answer) { System.out.println(" Compiler error")}
-
FYI: Plugging in the 'cheat' still returns the compiler error
-
@comphelp1, make sure your curly braces are correct. You can click on a curly brace to see it's match marked or you can click on the arrow on the side to collapse them. You don't want an extra one in the middle.
-
@Luke the simplest is to post new one and then delete previous. In general it's better not to paste long amounts of code here, and you can link to your submission instead by clicking on a link on the submissions page.
-
Thank you Learneroo, the answer I found is below:
if( a % 2 == 0) { return a / 2; } return (a * 3) + 1;
-
Questions are very hard to understand
-
@Meena, is there something specific here you don't understand?
-
yes i don't know why i don't understand questions their wording is difficult. and when i see cheat i say ohhh i knew but i perhaps i didn't understood the question
-
This is about the properties of int...
-
Cool problem, I like how you link it to a current problem that coding can be used to explore :D Keep up the good work guys! By the way, is / performing integer division?
-
@Stanley, if both numbers are integers,
/
will lose the decimal place from the answer. -
Ah, I was a bit confused but now I realise that we only want integer outputs for the Collatz Conjecture anyway. Thanks for the help :D
-
public class Main {
public static int doStuff(int a){ int NrQift = a % 2; if(a == NrQift){ return a/2; } else{ return 3 * a + 1;
} }
I'm going to take a break, until I get a reply for this question.
This is what I tried doing here, btw the variable name "NrQift" is from my language(albanian) meaning "even nr".So can someone help me out here ? I'm not trying to cheat, don't tell me the answer just help me out by explaining how do I do in the "if( How do I make the code know I want a even number as a condition for the code in the { } )
I also tried doing if(a % 2) but that didn't work either.
Tyvm -
I figured it out already after giving a bit more thought on the "Hint" xD Ty anyway whoever was gonna reply :)
-
if (a%2==0)
{
return a/2;
}
else
{
return 3*a+1;
} -
i write correct code but it say compile error
code is
if(a%2==0){
return a/2;
}
else{
return a*3 + 1;
} -
@Mina, you were missing a
}
. -
Pretty sure this should work...
if (a == a % 2) {
return a / 2;
}
else {
return (a * 3) + 1;
}why not?
-
@ArtTric
That won't work because a%2 should equal 0 (meaning that there is no remainder and therefore the number is even) and not equal to a. -
So simple...
int c=a%2; // take remainder of the input and save it
//in variable c.if(c==0){ return a/2; //if the remainder == 0.
// then return a/2; } else{ return 3*a+1; //if its not then .... }
-
I've done some work on the Collatz Conjecture before, so I kinda knew the answer. This is simpler than what has been posted already:
if (a % 2 != 0) { // odd
return (a * 3) + 1; // a times 3 plus 1
} else { // even
return a / 2; // a divided by 2
} -
@mina the operation needs to be on the right side. Its just a programming rule. example :
if (x - 2 = 0) // error
if (x = 2) // correct
it wont work in this example but thats just the rule.
-
Why was I wrong?
if (a % 2 == 0) {
return a / 2;
}
else {
return (a * 3) + 1;
} -
I entered the cheat answer and said it was incorrect haha
-
this is hard :/
-
Took me a while but finally figured it out. One thing that kept tripping me up was =. I understand that the equal sign in Java is used to say that an expression is true right? When is it appropriate
to use the equal sign then. I kept trying to use it in my if statement , that's what was messing me up.
-
See Variables in Programming and Algebra for
=
vs.==
. -
The solution is as straight-fwd as the problem, so apply simple maths and follow syntax.
-
I want to become a hacker, so i guess i have to learn all of this first :-)
-
needs more sauce
-
How we are using doStuff method in main class ?
-
what is wrong in below mentioned code
package basicpgm;
import java.util.Scanner;public class Main {
public static int doStuff(int a){ return 5*3+1; } public static void main(String[] args) { int result = doStuff(a); System.out.println(result); } }
-
if ( (x & 1) == 0 ) { even... } else { odd... }
the low bit will always be set on an odd number. -
This is so good I love this
-
import java.util.Scanner;
public class Main {
public static int doStuff(int a){ if(a%2 != 0){ return(3*a + 1); else{ return (a/2);
} } //your code here } public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); for(int i=0; i<n; i++){ int a = in.nextInt(); int result = doStuff(a); System.out.println(result); } }
} this is my code. The compiler is throwing a single error.
/usercode/Main.java:8: error: 'else' without 'if'
else{
^
1 error******* Cna somebody please help -
if (a%2==0) { return a/2; } else { return 3*a+1; } i dont know what is wrrong in this
-
I have figured out:
import java.util.Scanner;public class Main {
public static int doStuff(int a){ a=5; int s=a%2; int b=2; int c=4;
int d=6; int e=8; int f=0; int g=1; int h=3; int i=5; int j=7; int k=9; if(s==b){ a=a/2; } else if(s==c){ a=a/2; } else if(s==d){ a=a/2; } else if(s==e){ a=a/2; } else if(s==f){ a=a/2; } else if(s==g){ a=3*a+1; } else if(s==h){ a=3*a+1; } else if(s==i){ a=3*a+1; } else if(s==j){ a=3*a+1; } else{ a=3*a+1; } return a; } public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); for(int i=0; i<n; i++){ int a = in.nextInt(); int result = doStuff(a); System.out.println(result); } }
}
-
this will do it
// this if/else block will give result for any number a
if (a%2>0){
return 3*a+1
}
else{
return a/2; -
You have here syntax error:
if(hits > 100){
score = score + 10;
}
else if( hits > 90){
score = score + 6
}
else if( hits > 80){
score = score + 3;
}
else{
score = score +1;
}
//code continuesThere is missing ";" after digit 6 in your code.
You are welcome.
-
Thanks
-
I do it.
-
ya this is really confusing
-
the only thing confusing about it is the use of the == vs the =. This concept is in most programming languages so if you program in other languages, this isn't confusing at all.
If (you == dont){
return str x = 'get used to it';
}
Veronica
Nov 1, 1:13 PMSome of the questions are unclear, if you could ask then in a simpler manner, that would make it more understandable.