If Statement Comments

Comments

  • Some of the questions are unclear, if you could ask then in a simpler manner, that would make it more understandable.

  • 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 expression

    cont...
  • 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;
        }
    
    cont...
  • 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;
    
    cont...
  • 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.
    
    cont...
  • 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

  • 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

    cont...
  • 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);
    
    cont...
  •     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;
    
    cont...
  • 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 continues

    There is missing ";" after digit 6 in your code.

    You are welcome.

  • 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';
    }

Contact Us
Sign in or email us at [email protected]