Exercise 6: Write a program that chats with you. If you type Hi, then the program should print Hi and terminate. If you enter Bye, then the program should print Bye and terminate. If you enter anything else, the program should respond And how do you feel about this? and terminate. Use a switch statement.
import java.util.*;
public class HiBye {
public static void main(String[] args) {
System.out.print(": ");
Scanner input = new Scanner(System.in);
String x = input.nextLine();
switch (x) {
case "Hi":
System.out.println("Hi");
break;
case "Bye":
System.out.println("Bye");
break;
default:
System.out.println("And how do you feel about this?");
}
input.close();
}
}
No comments:
Post a Comment