If-else
/* This is a simple java program that uses if-else statement The program prints out the number 1 if the user enters y The program prints out the number 0 if the user enters n * */ import java.util.*;//needed for scanner class public class Finalexam { public static void main(String[] arg) { Scanner key = new Scanner(System.in); int x;//x as the integer System.out.println("Enter y for yes or n for no"); String input = key.nextLine();//holds the character y or n if(input.equals("y") || input.equals("Y"))//condition statment { x = 1;//assigns 1 to x if y is enter } else { x = 0;// assign 0 to x } System.out.println(x);//prints out whatever x is } } Here is the output of the program Enter y for yes or n for no n 0 Enter y for yes or n for no y 1 ----jGRASP wedge2: exit code for process is 0. ----jGRASP: operation complete.
/* This is a simple java program that uses if-else statement The program prints out the number 1 if the user enters y The program prints out the number 0 if the user enters n * */ import java.util.*;//needed for scanner class public class Finalexam { public static void main(String[] arg) { Scanner key = new Scanner(System.in); int x;//x as the integer System.out.println("Enter y for yes or n for no"); String input = key.nextLine();//holds the character y or n if(input.equals("y") || input.equals("Y"))//condition statment { x = 1;//assigns 1 to x if y is enter } else { x = 0;// assign 0 to x } System.out.println(x);//prints out whatever x is } }
Here is the output of the program
Enter y for yes or n for no n 0 Enter y for yes or n for no y 1 ----jGRASP wedge2: exit code for process is 0. ----jGRASP: operation complete.
< Java > <Home><Java Programs>