user2291293
user2291293

Reputation: 33

Returning Integer Values in Java

public class MultiplicationClass{

int answer;
int random;
int random2;

int create(){
    random = (int)(1+Math.random()*15);
    random2 = (int)(1+Math.random()*15);
    return random;
    return random2;
}

boolean checkAnswer(int useranswer){
    if(useranswer==getAnswer()){
        return true;
    }else{
        return false;
    }
}

int getAnswer(){
    return random*random2;
} 

}

This code always gives the error: unreachable statement in the line "return random2"

Upvotes: 0

Views: 61831

Answers (4)

Unknown AD
Unknown AD

Reputation: 1

<p>
i've tryed this one and it worked
check my github repository 
https://github.com/UnknownAD/CommonProgramingIssues


public class s{
        public static void main(String [] args){
        System.out.println("HI there");
        var primal=17;
        var edit=Integer.toString(primal);
        System.out.println(edit);
        method("22");
        }
        static void method(String tr){
        int stdin=Integer.parseInt(tr);
        int proc=stdin+5;
        String stdout=Integer.toString(proc);
        System.out.println(proc);
}
}

Upvotes: -1

dardo
dardo

Reputation: 4970

You can't return twice, you can only have 1 return statement.

return random;
return random2; // This one will never be reached since you've already returned from the method.

If a method has a return value, like this one returns an int, the method must return at least one value. Any instructions after return statement, if the return is not surrounded by a branch itself, will be unreachable and throw a compilation error, as you've seen.

I'll explain the multiple branches with a couple examples:

public int someMethod(int i) {
  if(i == 0) {
    return i;
  }
}

This method will not compile due to the fact that what happens if i is not equal to 0, the compiler does't know how to return the method. To fix this, either add an else, or a final return statement, either of these will work.

public int someMethod(int i) {
  if(i == 0) {
    return i;
  }
  return 2;
}

Or:

public int someMethod(int i) {
  if(i == 0) {
    return i;
  } else {
    return 2;
  }
}

Now the compiler knows if i is not 0, it can return a value of some sort.

Also, unrelated note, but glancing at how you wrote your code, I'm guessing your coming from a C background. Just a heads up, all of your methods and variables are what is known as default scope, or package-private. Even though the class is declared public, these will only be able to accessed by the class itself, and classes that share the same package with it.

Might want to look here:

http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

Upvotes: 6

Martin Seeler
Martin Seeler

Reputation: 6992

Each method in Java can return just one value. In Your example, you dont have to return any value, because you are asigning these values to your global variables. So this should be enough:

void create(){
    random = (int)(1+Math.random()*15);
    random2 = (int)(1+Math.random()*15);
}

If you want to return two values, just call the method twice and asign it to your variables like this:

int create(){
    int random = (int)(1+Math.random()*15);
    return random;
}

...

int getAnswer(){
    answer = create();
    answer2 = create();
    return answer*answer2;
} 

Upvotes: 6

Suresh Atta
Suresh Atta

Reputation: 122006

int create(){
    random = (int)(1+Math.random()*15);
    random2 = (int)(1+Math.random()*15);
    return random;
    return random2;
}

You cannot write the code after returning statement.

as random and random2 returning same value then

 int create(){
        random = (int)(1+Math.random()*15);    
        return random;

    }

Upvotes: 0

Related Questions