This program for absolutely beginner who want to learn programming.
If Else sample program for you.
import java.util.Scanner;
class IfElseStatementDemo {
public static void main(String args []) {
Scanner sc = new Scanner(System.in);
showGreeting("Welcome from STF PDF");
//Accept int value with res
int res = askItems(sc);
//
showOutput(res);
showGreeting("Kill MAL");
}
// showGreeting
private static void showGreeting(String str) {
System.out.println("============================");
System.out.println();
System.out.println(str);
System.out.println();
System.out.println("============================");
}
// askItems
// sc parameter arg parse
private static int askItems(Scanner sc) {
//Multiline String
System.out.println("""
Plz select One by number !
1. Orange
2. Apple
3. Mango
4. Pineapple
5. Watermelon
""");
// Scanner's Input must be int type
int num = sc.nextInt();
return num;
}
// showOutput
private static void showOutput(int i) {
//LocalVar
String name = "";
//Condition if else{}
if (i == 1){
name = "Orange";
}else if( i == 2){
name = "Apple";
}else if( i == 3){
name = "Mango";
}else if( i == 4){
name = "Pineapple";
}else if( i == 5){
name = "Watermelon";
}else{
name = "No product";
}
System.out.println("Product name is : " + name);
}
}
Thanks for your time 🙂
Harder than Ever.