Search⌘ K
AI Features

Solution Review: Method to Check Sum

Explore how to define and use a Java method that evaluates the sum of two parameters with conditional logic. Understand step-by-step how if, else if, and else blocks determine different return values based on sum comparison. This lesson helps you grasp method structure, variables, and control flow for effective coding.

Solution: Do

...
Java
class challenge_one{
public static int checkSum(int one, int two){
//Write your code here
//Declare the necessary variable
int check;
int sum= one+two;
if(sum<100)
check = 0;
else if(sum>100)
check=1;
else
check=2;
//Change the return variable as well
return check;
}
public static void main(String[] args){
int answer=checkSum(100,110);
System.out.println("The value of check is: "+answer);
answer=checkSum(100,0);
System.out.println("The value of check is: "+answer);
answer=checkSum(100,-110);
System.out.println("The value of check is: "+answer);
}
}

Understanding the code

...