Learn Java - Exercise 02x - Passing Parameters to a Method & Returning Values

preview_player
Показать описание

Learn how to program in java with our online tutorial. We will cover variables, loops, if else branching, arrays, strings, objects, classes, object oriented programming, conditional statements, and more.

Here we get practice writing code to pass a parameter to a method in java and return a numerical result.
Рекомендации по теме
Комментарии
Автор

This channel is severely underrated. This man is doing God's work

NgolaNalane
Автор

I agree this channel is so underrated!! Methods literally have brought me to tears and 1 video with this guy and it all makes sense. God bless you!!!

katelynbradley
Автор

Did I miss something? What to do if the inputs have the same value? In this code the method returns the first input because of >= but that is not strictly correct in the context of the exercise? Love the tutorials by the way!

baltarussow
Автор

My method is written little bit differently, but works the same way, logic.
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter a number between 1 and 100 : " );
int number1 = input.nextInt();
System.out.println("Enter a number between 1 and 100: " );
int number 2 = input.nextInt();
int answer = compareTwoNumbers(number1, number2);
System.out.println("You entered the numbers " + number1 + " and " + number2 + "\nThe larger number is: " + answer);
}
public static int compareTwoNumbers ( int a, int b){
if ( a > b) return a ;
else return b;
}

timdjalalov
Автор

This was mine, would this also be acceptable? Thank you.

*public static void main(String[] args) {
Scanner scan = new Scanner(System.in);

System.out.println("Enter number between 1 and 100: ");
int input1 = scan.nextInt();

System.out.println("Enter another number between 1 and 100: ");
int input2 = scan.nextInt();

System.out.println("You entered the number " + input1 + " and " + input2 +
". \nThe larger number is: " + largerNumber(input1, input2));
}

public static int largerNumber(int a, int b){
int answer = Math.max(a, b);
return answer;
}*

KaisarAnvar
Автор

watching this in 2022! great explanation

FitWithShamas
Автор

Why we have assigned 1 to number1, number2 and largerNum?

kelvinshrivastav