KaiquanMah's picture
Create 3A. Parameters
f7bf95f verified
raw
history blame
1.6 kB
public static void printLarger(int num1, int num2) {
if (num1 > num2) {
System.out.println(num1);
} else {
System.out.println(num2);
}
}
public static void main(String[] args) {
printLarger(10,4);
printLarger(111, 11111);
printLarger(5 * 5, 3 * 9);
}
Program outputs:
10
11111
27
====================
parameter data type
eg double
public class Example{
public static void main(String[] args) {
double a;
a = 4.0;
a = 24;
float f = 23.32f;
a = f;
}
}
========================================
public class Example {
public static void main(String[] args) {
printSquare(3.5); //accept double
printSquare(10); //accept int
printSquare(1.5f);//accept float
}
public static void printSquare(double num) {
System.out.println(num * num);
}
}
Program outputs:
12.25
100.0
2.25
========================================
Method parameters can also be of different types, for example:
public class Example {
public static void main(String[] args) {
tempBetween(10, 30, 25.5);
tempBetween(-5, 5, -15.25);
}
public static void tempBetween(int min, int max, double temp) {
if (temp >= min && temp <= max) {
System.out.println("Temperature is between the given values!");
} else {
System.out.println("Temperature is not between the given values.");
}
}
}
Program outputs:
Temperature is between the given values!
Temperature is not between the given values.