Java_案例实例1.简单的人机交互
程序员文章站
2022-03-04 21:11:10
...
运行效果截图:
源代码:
package ch3_4;
import java.util.Scanner;
public class InteractionDemo {
public static void main(String[] args) {
int option;
int i;
float f;
String s;
System.out.println("----------------------------------------菜单----------------------------------------");
System.out.println("1.输入整数\t\t2.输入小数\t\t3.输入字符串\t\t0.退出");
System.out.println("------------------------------------------------------------------------------------");
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("选项:");
option = scanner.nextInt();
switch (option) {
case 0:
System.out.println("\t确定要退出吗(y/n)");
s = scanner.next();
if (s.equalsIgnoreCase("y")) {
System.out.println("\t程序退出");
break;
} else {
break;
}
case 1:
System.out.println("\t请输入整数:");
i = scanner.nextInt();
System.out.println("\t你输入的是:" + i);
break;
case 2:
System.out.println("\t请输入小数:");
f = scanner.nextFloat();
System.out.println("\t你输入的是:" + f);
case 3:
System.out.println("\t请输入字符串:");
s = scanner.next();
System.out.println("\t你输入的是:" + s);
default:
System.out.println("\t请输入其他正确选项");
break;
}
}
}
}