文章目录
巩固题(努力)
1、月份
案例需求:
(1)用一个数组,保存12个月的英语单词
{"January","February","March","April","May","June","July","August","September","October","November","December"}
(2)从键盘输入1-12,显示对应的单词。
参考答案:
import java.util.Scanner;
public class Homework1 {
public static void main(String[] args) {
String[] monthNames = {"January","February","March","April","May","June","July","August","September","October","November","December"};
Scanner input = new Scanner(System.in);
System.out.print("请输入月份值:");
int month = input.nextInt();
if(month <1 || month> 12){
System.out.println("输入有误");
}else{
System.out.println("月份" + month +"是" + monthNames[month-1]);
}
input.close();
}
}
2、打印扑克牌
案例需求:遍历输出一副扑克牌。
开发提示:
(1)用第一个数组保存扑克牌的所有花色:黑桃、红桃、梅花、方片
String[] hua = {"黑桃","红桃","梅花","方片"};
(2)用第二个数组保存具有以上花色的数字:2-K
String[] dian = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
(3)用第三个数组保存扑克牌的牌面
String[] pu = new String[hua.length*dian.length+2];
(4)嵌套遍历第1个、第2个数组,排列组合生成扑克牌牌面,并把结果放到第3个数组的元素中
(5)单独考虑大王小王
(6)遍历输出
参考答案:
public class Homework2 {
public static void main(String[] args) {
String[] hua = {"黑桃","红桃","梅花","方片"};
String[] dian = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
String[] pu = new String[hua.length*dian.length+2];
for(int i=0,k=0; i<hua.length; i++){
for(int j=0; j<dian.length; j++,k++){
pu[k] = hua[i]+dian[j];
}
}
pu[pu.length-2] = "大王";
pu[pu.length-1] = "小王";
for (int i = 1; i <= pu.length; i++) {
System.out.print(pu[i-1]+" ");
if(i%13==0){
System.out.println();
}
}
}
}
3、正序和逆序输出26个英文字母
案例需求:
(1)要求使用char数组存储26个英文字母,并分别用正序和逆序方式显示输出。
(2)要求每10个字母一行。
参考答案:
public class Homework3 {
public static void main(String[] args) {
char[] letters = new char[26];
for (int i=0; i<letters.length; i++){
letters[i] = (char)('a' + i);
}
System.out.println("小写字母的正序排序:");
int count = 0;
for (int i = 0; i <letters.length; i++) {
count++;
if(count == 10 || i == letters.length-1){
System.out.println(letters[i]);
count = 0;
}else if(count < 10) {
System.out.print(letters[i]+",");
}
}
System.out.println("小写字母的逆序排序:");
count = 0;
for (int i = letters.length-1; i >= 0; i--) {
count++;
if(count == 10 || i == 0){
System.out.println(letters[i]);
count = 0;
}else if(count < 10) {
System.out.print(letters[i]+",");
}
}
}
}
4、随机生成一组验证码
案例需求:随机生成一组验证码,验证码由大小写字母和10个阿拉伯数字字符中的任意6位组成。
开发提示:
1、声明一个char[]数组,长度为62,并存储26个小写字母、26个大写字母、10个阿拉伯数字字符
2、随机生成6位下标,下标范围是[0,62),取出该下标对应的字符,把它们拼接成一个6位字符构成的验证码
参考答案:
public class Homework4 {
public static void main(String[] args) {
char[] letters = new char[26*2+10];
//[0,25]存储小写字母
//[26,51]存储大写字母
//[52,61]存储大写字母
for (int i=0; i<26; i++){
letters[i] = (char)('a' + i);
letters[26+i] = (char)('A' + i);
}
for (int i=0; i<10; i++){
letters[52 + i] = (char)('0' + i);
}
String code = "";
for (int i=0; i<6; i++){
/*
Math.random():[0,1)的小数
Math.random()*letters.length:[0,letters.length)的小数
(int)(Math.random()*letters.length):[0,letters.length)的整数,
即[0, letters.length-1]
*/
int index = (int)(Math.random()*letters.length);
code += letters[index];
}
System.out.println("随机验证码:" + code);
}
}
5、判断数组中的元素值是否对称
案例需求:判断某个数组是否属于对称数组,即数组正序遍历和倒序遍历的结果是一样的。例如:
开发提示:循环比较数组首尾对称位置元素是否相同,如果有不同,就不是对称数组,所有对称位置都相等才是对称数组。
参考答案:
public class Homework5 {
public static void main(String[] args){
int[] arr = {1,2,3,4,4,3,2,1};
//(1)先假设它是对称的
boolean flag = true;
//(2)遍历,查看数组的元素是否首尾对称
//left表示左边的下标
//right表示右边的下标
for(int left=0,right=arr.length-1; left<right; left++,right--){
if(arr[left] != arr[right]){
flag = false;
break;
}
}
System.out.println(flag?"对称":"不对称");
}
}
拔高题(自愿)
6、打鱼还是晒网
案例需求:假设张三从1990年1月1日开始执行三天打鱼两天晒网,5天一个周期,风雨无阻,那么李四想要找张三玩,需要从键盘输入年,月,日后,判断这一天张三是在打鱼还是晒网。
开发提示:
(1)先用两个数组分别存储平年和闰年每个月总天数:
-
使用数组1存储平年的总天数和12个月的总天数
-
使用数组2存储闰年的总天数和12个月的总天数
int[] commonYearDays = {365,31,28,31,30,31,30,31,31,30,31,30,31};
int[] leapYearDays = {366,31,29,31,30,31,30,31,31,30,31,30,31};
(2)再算从1990年1月1日到year年month月day日的总天数
- [1990, year-1]年的总天数
- 第year年[1, month-1]月的总天数
- 第month月day天
(3)再用总天数 % 5(三天打鱼两天晒网的周期),根据结果来判断是打鱼还是晒网
(4)闰年的判断标准是:
- 年份year可以被4整除,但不能被100整除
- 或者年份year可以被400整除
参考答案:
import java.util.Scanner;
public class Homework6 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入年:");
int year = input.nextInt();
System.out.print("请输入月:");
int month = input.nextInt();
System.out.print("请输入日:");
int day = input.nextInt();
input.close();
int[] commonYearDays = {365,31,28,31,30,31,30,31,31,30,31,30,31};
int[] leapYearDays = {366,31,29,31,30,31,30,31,31,30,31,30,31};
//累加[1990, year-1]年的总天数
int days = 0;
for(int i=1990; i<year; i++){//这个i代表年份
if(i%4==0 && i%100!=0 || i%400==0){
days+= leapYearDays[0];
}else{
days+= commonYearDays[0];
}
}
//累加第year年[1, month-1]月的满月天数
boolean leapYearFlag = year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
for (int i = 1; i < month; i++) {
if(i==2 && leapYearFlag){
days += leapYearDays[i];
}else {
days += commonYearDays[i];
}
}
//累加第year年month月的day天
days += day;
//判断
System.out.print(year + "年" + month + "月" + day + "日这一天在");
if(days%5==1 || days%5==2 || days%5==3){
System.out.println("打鱼");
}else{
System.out.println("晒网");
}
}
}
如果要考虑年月日的输入合法性校验,代码较复杂
import java.util.Scanner;
public class Homework6 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//如果要考虑输入非法值的情况,代码就比较复杂
System.out.println("请输入日期:");
int year;
while(true){
System.out.print("年:");
year = input.nextInt();
if(year >= 1990){
break;
}else{
System.out.println("年份必须大于等于1990年,请重新输入");
}
}
int month;
while(true){
System.out.print("月:");
month = input.nextInt();
if(month >=1 && month <= 12){
break;
}else{
System.out.println("月份范围是[1,12],请重新输入");
}
}
int[] totalDays = {31,28,31,30,31,30,31,31,30,31,30,31};
//如果第year年是闰年,那么2月份总天数是29
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
totalDays[1] = 29;
}
int day;
while(true){
System.out.print("日:");
day = input.nextInt();
if(day >=1 && day <= totalDays[month-1]){
break;
}else{
System.out.println(year +"年" + month +"月的日期范围是[1," + totalDays[month-1] + "],请重新输入");
}
}
input.close();
int[] commonYearDays = {365,31,28,31,30,31,30,31,31,30,31,30,31};
int[] leapYearDays = {366,31,29,31,30,31,30,31,31,30,31,30,31};
//累加[1990, year-1]年的总天数
int days = 0;
for(int i=1990; i<year; i++){//这个i代表年份
if(i%4==0 && i%100!=0 || i%400==0){
days+= leapYearDays[0];
}else{
days+= commonYearDays[0];
}
}
//累加第year年[1, month-1]月的满月天数
boolean leapYearFlag = year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
for (int i = 1; i < month; i++) {
if(i==2 && leapYearFlag){
days += leapYearDays[i];
}else {
days += commonYearDays[i];
}
}
//累加第year年month月的day天
days += day;
//判断
System.out.print(year + "年" + month + "月" + day + "日这一天在");
if(days%5==1 || days%5==2 || days%5==3){
System.out.println("打鱼");
}else{
System.out.println("晒网");
}
}
}
7、模拟大乐透
案例需求:大乐透(前区“35选5”+后区“12选2”),即前区在1-35之间的号码中随机选取5个,后区在1-12之间的号码中随机选取2个,组成一期的中奖号码,请用程序模拟产生一组大乐透中奖号码。
开发提示:
- 声明一个int类型的数组front,长度为35,默认值都是0;
- 声明一个int类型的数组after,长度为12,默认值都是0;
- 随机产生[0,35)之间的整数。如果随机产生的是0,那么就把front[0]修改为1,如果随机产生的是5,那么就把front[5]修改为1,如果随机产生的是10,就把front[10]修改为1。但是如果本次随机产生的是5,而front[5]已经是1了,那么需要重新随机产生一个整数。用这种方式把front数组的5个元素修改为1。
- 随机产生[0,12)之间的整数。使用同样的方式,把after数组的2个元素修改为1。
- 遍历front和after数组,输出大乐透中奖号码,判断front和after数组元素是否为1,如果为1,就显示它的下标+1值。
参考答案:
public class Homowork7 {
public static void main(String[] args) {
int[] front = new int[35];
int[] after = new int[12];
for (int i = 1; i <=5; i++) {
/*
Math.random():[0,1)的小数
Math.random() * front.length:[0,front.length)的小数
(int)(Math.random() * front.length):[0,front.length)的整数,[0,front.length-1]
*/
int index;
do{
index = (int)(Math.random() * front.length);
}while(front[index]==1);
front[index] = 1;
}
for (int i = 1; i <=2; i++) {
int index;
do{
index = (int)(Math.random() * after.length);
}while(after[index]==1);
after[index] = 1;
}
System.out.println("本期大乐透中奖号码:");
System.out.print("前区号码为:");
for (int i = 0; i < front.length; i++) {
if(front[i] == 1){
System.out.print(i+1+"\t");
}
}
System.out.print("\n后区号码为:");
for (int i = 0; i < after.length; i++) {
if(after[i] == 1){
System.out.print(i+1+"\t");
}
}
System.out.println();
}
}
8、统计字符出现次数
案例需求:英语中最长的单词是"pneumonoultramicroscopicsilicovolcanoconiosis",意思是“肺尘病,矽肺病”,一共有45个英文字母。现在要求统计这个单词中出现了哪些字母,以及它们出现的次数,并且找出出现次数最多的字母。
开发提示:
(1)使用String类型的变量words存储英语单词"pneumonoultramicroscopicsilicovolcanoconiosis"
(2)通过word.toCharArray()可以根据字符串word得到一个char[]类型的数组。
其中toCharArray()是String类型提供的系统函数,就像Math.random()等函数一样,后面会学到,这里先提前用一下。它的作用就是创建一个char[]数组,并把字符串中的每一个字符依次存储到这个char[]数组中。
//以"hello"为例
String word = "hello";
char[] wordCharArray = word.toCharArray();
//此时wordCharArray字符数组内容是 {‘h','e','l','l','o'}
(3)声明一个int类型的数组counts,长度为26,分别用来记录26个字母出现的次数,counts[0]记录的是’a’字母的次数,counts[1]记录的是’b’字母的次数,依次类推。
参考答案:
public class Homowork8 {
public static void main(String[] args) {
String word = "pneumonoultramicroscopicsilicovolcanoconiosis";
char[] wordCharArray = word.toCharArray();
int[] counts = new int[26];
for (int i = 0; i < wordCharArray.length; i++) {
counts[wordCharArray[i]-97]++;
}
int max = counts[0];
for (int i = 1; i < counts.length; i++) {
if(counts[i] > max){
max = counts[i];
}
}
System.out.println("单词中出现了如下字母:");
for (int i = 0; i < counts.length; i++) {
if(counts[i] != 0){
System.out.println((char)(i+97) +":" + counts[i]);
}
}
System.out.print("出现次数最多的字母是:");
for (int i = 0; i < counts.length; i++) {
if(max == counts[i]){
System.out.print((char)(i+97) + "\t");
}
}
System.out.println();
}
}
9、统计低于平均分的学生人数
案例需求:先从键盘输入本组学员的人数,再从键盘输入本组学员的姓名和成绩,最后统计:
- 本组学员的平均分,
- 低于平均分的学员人数,
- 哪些学员低于平均分,
- 最高分和最低分分别是谁。
参考答案:
import java.util.Scanner;
public class Homowork9 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请录入本组学员人数:");
int count = input.nextInt();
int[] grades = new int[count];
String[] names = new String[count];
for (int i = 0; i < grades.length; i++) {
System.out.print("第" + (i+1) + "个学员的姓名:");
names[i] = input.next();
System.out.print("第" + (i+1) + "个学员的成绩:");
grades[i] = input.nextInt();
}
input.close();
//学员姓名和成绩
System.out.println("所有学员的成绩如下:");
for (int i = 0; i < names.length; i++) {
System.out.println(names[i] + ":" + grades[i]);
}
//找出最高分、最低分、累加总分,计算平均分
int max = grades[0];
int min = grades[0];
double sum = 0;
for (int i = 1; i < grades.length; i++) {
if(grades[i] > max){
max = grades[i];
}
if(grades[i] < min){
min = grades[i];
}
sum += grades[i];
}
double avg = sum/grades.length;
//输出结果
System.out.println("最高分成绩是:" + max);
System.out.println("最第分成绩是:" + min);
System.out.println("平均分成绩是:" + avg);
System.out.println("获得最高分的有:");
for (int i = 0; i < names.length; i++) {
if(max == grades[i]){
System.out.println(names[i]);
}
}
System.out.println("获得最第分的有:");
for (int i = 0; i < names.length; i++) {
if(min == grades[i]){
System.out.println(names[i]);
}
}
System.out.println("低于平均分的有:");
int total = 0;
for (int i = 0; i < names.length; i++) {
if(grades[i] < avg){
System.out.println(names[i]);
total++;
}
}
System.out.println("低于平均分的一共有:" + total +"人");
}
}