I created a boolean method that contains a string array and Random. The method is supposed to return a true or false statement depending on which element of the array is selected at random.
public static boolean attemptpassingGrade() { Scanner input = new Scanner(System.in); Random rnd = new Random(); int spin;
//create array of compliments
String[] grades = new String[5];
grades[0] = "A";
grades[1] = "B";
grades[2] = "C";
grades[3] = "D";
grades[4] = "F";
String enter;
//Prompt User to press enter
System.out.print("Press Enter to spin the grade wheel...");
enter = input.nextLine();
//assign spin to random
spin = rnd.nextInt(5);
if (spin == 0) {
System.out.println(grades[0]);
System.out.println("Passed");
return true;
}else if (spin == 1) {
System.out.println(grades[1]);
System.out.println("Passed");
return true;
}else if (spin == 2) {
System.out.println(grades[2]);
System.out.println("Passed");
return true;
}else if (spin == 3) {
System.out.println(grades[3]);
System.out.println("Didn't pass");
return false;
}else if (spin == 4) {
System.out.println(grades[4]);
System.out.println("Didn't pass");
return false;
}
}
CodePudding user response:
Change
else if (spin == 4)
to just
else
The static analyzer does not know that the value of spin
is constrained, so it want to guarantee that an appropriate return
statement is reached.
CodePudding user response:
You are getting an error because there are situations where you do not hit a return
.
Think logically about how your code currently works. If you have a value 0
to 4
you will return
and all is right in the world. However, since you do not have an else
and only have else-if
statements, if a 5
is entered your code would not know what to return
.
Now you may say that, "but spin = rnd.nextInt(5);
will always return 0
to 4
! However, your compiler doesn't know that, so it will flag an error for the situation that does not hit a return
.
You can change the final else-if
to an else
so it always hits that statement as a default, or add a default return value at the end of the method:
//assign spin to random
spin = rnd.nextInt(5);
if (spin == 0) {
System.out.println(grades[0]);
System.out.println("Passed");
return true;
}else if (spin == 1) {
System.out.println(grades[1]);
System.out.println("Passed");
return true;
}else if (spin == 2) {
System.out.println(grades[2]);
System.out.println("Passed");
return true;
}else if (spin == 3) {
System.out.println(grades[3]);
System.out.println("Didn't pass");
return false;
}else if (spin == 4) {
System.out.println(grades[4]);
System.out.println("Didn't pass");
return false;
}
return false; //Now it will always return false if the code somehow reaches here
CodePudding user response:
You need to add an "else" or just return a default boolean value at the end of the function, otherwise it will return nothing if none of the IF and IF-ELSE conditions are satisfied.
Possible solutions:
1 -
if (spin == 0) {
...
}else if (spin == 1) {
...
}else if (spin == 4) {
...
} else {
return true; // here you can return a default boolean value inside the ELSE
}
2 -
if (spin == 0) {
...
}else if (spin == 1) {
...
}else if (spin == 4) {
...
}
return true; // here you can return a default boolean value outside the IF-ELSE