1、使用 switch 把小写类型的 char 型转为大写(键盘输入)。只转换 a, b, c, d, e. 其它的输出 "other"。
public class SwitchDetail {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入小写字母(a-e)");
char c1 = scanner.next().charAt(0);
switch (c1){
case 'a':
System.out.println('A');
break;
case 'b':
System.out.println('B');
break;
case 'c':
System.out.println('C');
break;
case 'd':
System.out.println('D');
break;
case 'e':
System.out.println('E');
break;
default:
System.out.println("other");
}
}
2、 对学生成绩大于 60 分的,输出"合格"。低于 60 分的,输出"不合格"。(注:输入的成绩不能大于 100), 提示 成绩/60
public class SwitchDetail {
public static void main(String[] args) {
/**
* (2) 对学生成绩大于 60 分的,输出"合格"。
* 低于 60 分的,输出"不合格"。
* (注:输入的成绩不能大于 100), 提示 成绩/60
*/
Scanner scanner = new Scanner(System.in);
System.out.println("请输入学生成绩(0-100):");
int score = scanner.nextInt();
if (score >= 0 && score <= 100) {
switch ((int)(score/60)){
case 1:
System.out.println("合格");
break;
case 0:
System.out.println("不合格");
break;
}
}else{
System.out.println("输入的成绩大于100,请重新输入");
}
3、 根据用于指定月份,打印该月份所属的季节。 3,4,5 春季 6,7,8 夏季 9,10,11 秋季 12, 1, 2 冬季 [提示 使用穿透]
public class SwitchDetail {
public static void main(String[] args) {
/**
* 根据用于指定月份,打印该月份所属的季节。
* 3,4,5 春季
* 6,7,8 夏季
* 9,10,11 秋季
* 12, 1, 2 冬季 [提示 使用穿透]
*/
Scanner scanner = new Scanner(System.in);
System.out.println("请输入月份(1-12)");
int month = scanner.nextInt();
switch(month){
case 3:
case 4:
case 5:
System.out.println("春季");
break;
case 6:
case 7:
case 8:
System.out.println("夏季");
break;
case 9:
case 10:
case 11:
System.out.println("秋季");
break;
case 12:
case 1:
case 2:
System.out.println("冬季");
break;
}
}
}
4、打印 1~100 之间所有是 9 的倍数的整数,统计个数 及 总和.
/*
* 打印 1~100 之间所有是 9 的倍数的整数,统计个数 及 总和.[化繁为简,先死后活]
*/
public class ForExercise {
public static void main(String[] args) {
int sum = 0;//总和
int count = 0;//统计个数
for(int i = 1;i < 100;i++){
if(i % 9 == 0){
sum += i;
count++;
}
}
System.out.println(count);
System.out.println(sum);
}
}
5、输出下面表达式的输出
0 + 5 = 5
1 + 4 = 5
2 + 3 = 5
3 + 2 = 5
4 + 1 = 5
5 + 0 = 5
public class ForExercise02 {
public static void main(String[] args) {
int n = 5;
for(int i = 0;i <= n;i++){
System.out.println(i + " + " + (n-i) + " = " + n);
}
}
}
6、打印 1—100 之间所有能被 3 整除的数 [使用 while]
/*
打印 1—100 之间所有能被 3 整除的数 [使用 while]
*/
@Test
public void test01() {
int i = 1;
while(i <= 100){
if(i % 3 == 0){
System.out.println(i);
}
i++;
}
}
7、 打印 40—200 之间所有的偶数
/*
打印 40—200 之间所有的偶数
*/
@Test
public void test02(){
int i = 40;
while(i <= 200){
if(i % 2 == 0){
System.out.println(i);
}
i++;
}
}
8、打印 1—100[使用 do...while]
/*
打印 1—100
*/
@Test
public void test01(){
int i = 1;
do{
System.out.println(i);
i++;
}while(i <= 100);
}
9、计算 1—100 的和[使用 do...while]
*
计算 1—100 的和
*/
@Test
public void test02(){
int i = 1;
int sum = 0;
do {
sum += i;
i++;
}while(i <= 100);
System.out.println(sum);
}
10、 统计 1---200 之间能被 5 整除但不能被 3 整除的个数[使用 do...while]
/*
统计 1---200 之间能被 5 整除但不能被 3 整除的个数
*/
@Test
public void test03(){
int i = 1;
int count = 0;//统计满足条件的个数
do{
if(i % 5 ==0 && i % 3 != 0){
System.out.println("i = " + i);
count++;
}
i++;
}while(i <= 200);
System.out.println(count);
}
11、如果李三不还钱,则老韩将一直使出五连鞭,直到李三说还钱为止
[System.out.println("
老韩问:还钱吗?y/n")] [使用 do...while]
public class DoWhileExercise02 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
char answer = ' ';
do{
System.out.println("老韩使出了五连鞭");
System.out.println("老韩问:还钱吗?y/n");
answer = scanner.next().charAt(0);
System.out.println("李三的回答是: " + answer);
}while(answer == 'n');
System.out.println("李三还钱了");
}
}