The function of the code is as below:
Classify one's age and,
if sb is over 18 years old, then compare his/her favorite food with "pizza".
package practice; import java.util.Scanner; public class Main { public static void main(String[] args) { System.out.println("Type your age here:"); Scanner sc=new Scanner(System.in); int age=sc.nextInt(); //this is the difference between two codes if (age>=18) { System.out.println("Type your favourite food here: "); String food = sc.nextLine(); if (food.equals("pizza")) { System.out.println("Mine too!"); } else { System.out.println("not mine!"); } } else if(age>=13){ System.out.println("You are a teenager"); } else{ System.out.println("You are a child"); } }}
The code above can't fulfill my purpose.
package practice; import java.util.Scanner; public class Main { public static void main(String[] args) { System.out.println("Type your age here:"); Scanner sc=new Scanner(System.in); String s=sc.nextLine(); int age=Integer.parseInt(s); //This is the difference between two codes if (age>=18) { System.out.println("Type your favourite food here: "); String food = sc.nextLine(); if (food.equals("pizza")) { System.out.println("Mine too!"); } else { System.out.println("not mine!"); } } else if(age>=13){ System.out.println("You are a teenager"); } else{ System.out.println("You are a child"); } }}