java教学练习题_java学习笔记(5-基础语法练习题)

1.键盘录入月份,输出对应的季节:

基础版本

package daigua4;

import java.util.Scanner;

/*

* 根据输入的月份,输出对应的季节

* */

public class MonthToSeason {

public static void main(String[] args) {

// 创建Scanner实例

Scanner sc = new Scanner(System.in);

System.out.print("请输入一个月份:");

int month = sc.nextInt();

// switch case 进行条件判断

switch (month) {

case 1:

System.out.println("春天!");

break;

case 2:

System.out.println("春天!");

break;

case 3:

System.out.println("春天!");

break;

case 4:

System.out.println("夏天!");

break;

case 5:

System.out.println("夏天!");

break;

case 6:

System.out.println("夏天!");

break;

case 7:

System.out.println("秋天!");

break;

case 8:

System.out.println("秋天!");

break;

case 9:

System.out.println("秋天!");

break;

case 10:

System.out.println("冬天!");

break;

case 11:

System.out.println("冬天!");

break;

case 12:

System.out.println("冬天!");

break;

default:

System.out.println("输入月份错误!");

break;

}

sc.close();

}

}

改进版本:

switch的穿透现象

package daigua4;

import java.util.Scanner;

public class MonthToSeason2 {

public static void main(String[] args) {

// 创建Scanner实例

Scanner sc = new Scanner(System.in);

System.out.print("请输入一个月份:");

int month = sc.nextInt();

// switch case 进行条件判断

switch (month) {

case 1:

case 2:

case 3:

System.out.println("春天!");

break;

case 4:

case 5:

case 6:

System.out.println("夏天!");

break;

case 7:

case 8:

case 9:

System.out.println("秋天!");

break;

case 10:

case 11:

case 12:

System.out.println("冬天!");

break;

default:

System.out.println("输入月份错误!");

break;

}

sc.close();

}

}

2.打印5位数中所有的回文数

package daigua4;

/*

* 打印5位数中所有的回文数字

* */

public class HuiWen {

public static void main(String[] args) {

for (int i = 10000; i < 100000; i++) {

int gewei = i%10;

int shiwei = i/10%10;

int qianwei = i/1000%10;

int wanwei = i/10000%10;

if (gewei == wanwei & shiwei == qianwei) {

System.out.println(i);

}

}

}

}

3.求数组中指定条件数字的和

package daigua4;

/*

* 需求:

* (1)定义一个int类型的一维数组,内容为{171,72,19,16,118,51,210,7,18}

* (2)求出该数组中满足要求的元素和。

* 要求:求和的元素的个位和十位不能包含7,并且只能为偶数。

*

* 分析:

* A:定义一个int类型的一维数组

* B:定义一个求和变量

* C:遍历数组,获取到数组中的每一个元素

* D:判断该元素是否满足如下要求,如果是就累加,否则,不搭理它

* x%2==0

* x%10 != 7

* x/10%10 !=7

* E:输出结果

*/

public class Test1 {

public static void main(String[] args) {

int[] arr = {171,72,19,16,118,51,210,7,18};

int sum = 0;

for (int i = 0; i < arr.length; i++) {

int cur_num = arr[i];

if (cur_num > 10 & cur_num%10 != 7 & cur_num/10%10 != 7 & cur_num%2 == 0) {

System.out.println(cur_num);

sum += cur_num;

} else if(cur_num < 10 & cur_num%10 != 7 & cur_num%2 == 0){

System.out.println(cur_num);

sum += cur_num;

}

}

System.out.println(sum);

}

}

4.算平均分

package daigua4;

import java.util.Scanner;

/*

* 需求:在编程竞赛中,有6个评委为参赛的选手打分,分数为0-100的整数分。

* 选手的最后得分为:去掉一个最高分和一个最低分后 其余4个选手的平均值。

* 请写代码实现。(不考虑小数部分)

*

* 分析:

* A:定义一个长度为6的数组。

* B:通过键盘录入的方式给出评委的分数

* C:写方法实现获取数组中的最大值,最小值

* D:写方法实现数组元素的求和

* E:平均分: (和-最高分-最低分)/(arr.length-2)

* F:输出分数即可

*/

public class Test2 {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int arr[] = new int[6];

// 循环输入评委的分数

for (int i = 1; i <= 6; i++) {

System.out.println("亲输入" + i + "第位评委的分数:");

arr[i-1] = sc.nextInt();

}

int max_num = max(arr);

int min_num = min(arr);

int sum_num = sum(arr);

int average_num = (sum_num-max_num-min_num)/(arr.length-2);

System.out.println("average_num:"+average_num);

sc.close();

}

public static int max(int[] args) {

int temp = args[0];

for (int i = 0; i < args.length - 1; i++) {

temp = (temp>args[i+1])?temp:args[i+1];

}

return temp;

}

public static int min(int[] args) {

int temp = args[0];

for (int i = 0; i < args.length - 1; i++) {

temp = (temp

}

return temp;

}

public static int sum(int[] args) {

int sum = 0;

for (int i = 0; i < args.length - 1; i++) {

sum += args[i];

}

return sum;

}

}

你可能感兴趣的:(java教学练习题)