1.说说switch支持的数据类型有哪些?
在Java中,switch
语句支持以下几种基本数据类型:
byte
short
char
int
String
(Java 7或更高版本)
需要注意的是,switch
语句不支持以下数据类型:
long
float
double
boolean
- 任何对象类型(除了
String
,因为它是非基本类型的特例)
对于在switch
语句中使用字符串时,请使用Java 7之后的版本,因为在Java 7之前的版本中,switch
语句只支持使用整数类型作为条件表达式。在Java 7及以后的版本中,switch
语句允许使用String
作为条件表达式,这方面带来了更大的灵活性。但是需要注意,switch
语句中的字符串匹配是区分大小写的。
此外,需要注意每个case
标签中的值必须是常量或字面量,不能是变量或表达式。
2.编程题练习:任意输入一个数,如果是偶数的话就输出(if)
import java.util.Scanner;
/**
*
* 任意输入一个数,如果是偶数的话就输出(if)
*/
public class _2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入一个数字: ");
int num = scanner.nextInt();
if (num % 2 == 0) {
System.out.println("这是一个偶数");
}
}
}
3.编程题练习:判断一个任意输入的数是奇数还是偶数(ifelse)
import java.util.Scanner;
/**
*
* 判断一个任意输入的数是奇数还是偶数(ifelse)
*/
public class _3 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入一个数字: ");
int num = scanner.nextInt();
if (num % 2 == 0) {
System.out.println("这是一个偶数");
} else {
System.out.println("这是一个奇数");
}
}
}
4.编程题练习:任意输入一个成绩,判断这个成绩属于哪个等级。
等级要求:[90-100 A,[80-90 B,[70-80 C,[60-70 D,60以下就是挨巴掌
import java.util.Scanner;
/**
*
* 任意输入一个成绩,判断这个成绩属于哪个等级
*/
public class _4 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入成绩: ");
double score = scanner.nextDouble();
if (score >= 90) {
System.out.println("A");
} else if (score >= 80) {
System.out.println("B");
} else if (score >= 70) {
System.out.println("C");
} else if (score >= 60) {
System.out.println("D");
} else {
System.out.println("不及格");
}
}
}
5.编程题练习:任意输入一个年,和一个月,判断这年这月有多少天(使用if)
import java.util.Scanner;
/**
*
* 任意输入一个年,和一个月,判断这年这月有多少天(使用if)
*/
public class _5 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入年份: ");
int year = scanner.nextInt();
System.out.print("请输入月份: ");
int month = scanner.nextInt();
int days = 0;
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
days = 31;
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
days = 30;
} else if (month == 2) {
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
days = 29;
} else {
days = 28;
}
} else {
System.out.println("无效的月份");
return;
}
System.out.println(year + "年" + month + "月有" + days + "天");
}
}
6.编程题练习:任意输入一个年,和一个月,判断这年这月有多少天(使用switch)
import java.util.Scanner;
/**
*
* 任意输入一个年,和一个月,判断这年这月有多少天(使用switch)
*/
public class _6 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入年份: ");
int year = scanner.nextInt();
System.out.print("请输入月份: ");
int month = scanner.nextInt();
int days;
switch (month) {
/*
如果case: ;break;
没有添加break用于打断该指令,则
*/
case 1: // 1月
case 3: // 3月
case 5: // 5月
case 7: // 7月
case 8: // 8月
case 10: // 10月
case 12: // 12月
days = 31;
break;
case 4: // 4月
case 6: // 6月
case 9: // 9月
case 11: // 11月
days = 30;
break;
case 2: // 2月
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
days = 29;
} else {
days = 28;
}
break;
default:
System.out.println("无效的月份");
return;
}
System.out.println(year + "年" + month + "月有" + days + "天");
}
}
7.猜拳游戏 0—石头 1—剪刀 2—布,玩家通过键盘输入一个0–2的数表示玩家出拳,
电脑通过系统随机生成一个0–2的数表示电脑出拳
思考:计算出玩家赢还是计算机赢,并提示,(处理玩家的错误输入)
import java.util.Scanner;
/**
*
* 猜拳游戏 0---石头 1---剪刀 2---布,玩家通过键盘输入一个0--2的数表示玩家出拳,
* 电脑通过系统随机生成一个0--2的数表示电脑出拳
* 思考:计算出玩家赢还是计算机赢,并提示,(处理玩家的错误输入)
*/
public class _7 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("猜拳游戏开始!");
System.out.println("请玩家输入猜拳的数字:0(石头),1(剪刀),2(布)");
System.out.print("玩家出拳:");
int playerChoice = scanner.nextInt();
if (playerChoice < 0 || playerChoice > 2) {
System.out.println("输入无效,请输入0-2之间的数字!");
return;
}
// [0,2]
int computerChoice = (int) (Math.random() * ( 2 + 1 - 0 ) + 0 ); // 生成0-2的随机数表示电脑出拳
System.out.println("电脑出拳:" + computerChoice);
// 判断胜负
if (playerChoice == computerChoice) {
System.out.println("平局!");
} else if ((playerChoice == 0 && computerChoice == 1) ||
(playerChoice == 1 && computerChoice == 2) ||
(playerChoice == 2 && computerChoice == 0)) {
System.out.println("玩家赢!");
} else {
System.out.println("电脑赢!");
}
}
}
8.按要求书写学生管理系统的功能,要求如下
首先进入主界面
如果输入1进入登录页面,在登录页面输入用户名和密码,如果用户名输入111密码输入222则显示登录成功,否则就显示登录失败
如果输入2则进入注册页面,在注册页面输入用户名和密码,如果用户名输入111则提示用户已经存在
否则就显示注册成功,三个页面分别如下:
主界面如下
欢迎进入学生管理系统登录请输入输入1注册请输入输入2****
登录界面如下:
********登录页面*************请输入数字用户名:请输入数字密码:登录成功[登陆失败]
注册界面如下:
注册页面请输入数字用户名:请输入数字密码:用户名已存在[注册成功]
import java.util.Scanner;
public class _8 {
private static Scanner scanner = new Scanner(System.in);
private static String username;
private static String password;
public static void main(String[] args) {
int option;
boolean exit = false;
while (!exit) {
displayMainMenu();
option = scanner.nextInt();
switch (option) {
case 1:
login();
break;
case 2:
register();
break;
default:
System.out.println("无效的选项,请重新输入!");
}
System.out.println("\n");
}
}
private static void displayMainMenu() {
System.out.println("***********欢迎进入学生管理系统***************");
System.out.println("*************登录请输入输入1****************");
System.out.println("************注册请输入输入2*****************");
System.out.print("请输入选项:");
}
private static void login() {
System.out.println("**********登录页面*************");
System.out.print("请输入用户名:");
username = scanner.next();
System.out.print("请输入密码:");
password = scanner.next();
if (username.equals("111") && password.equals("222")) {
System.out.println("登录成功!");
} else {
System.out.println("登录失败!");
}
}
private static void register() {
System.out.println("**********注册页面*************");
System.out.print("请输入用户名:");
username = scanner.next();
if (username.equals("111")) {
System.out.println("用户名已存在!");
} else {
System.out.print("请输入密码:");
password = scanner.next();
System.out.println("注册成功!");
}
}
}