Java之入门题
下面是5道Java基础题的解法,提供一个解决的视角,仅供参考。
题目描述:
编程题提示用户输入年月日信息,判断这一天是这一年中的第几天并打印
注意点:
- 用户输入的年月日是否为异常值?如月份和日有无越界
- 需要判断当年是否为闰年,闰年需修改2月的实际天数
代码如下:
import java.util.Scanner;
public class homework1 {
public static void main(String[] args) {
//对用户输入的年月日是否是为异常值进行判定
for(;;) {
Scanner sc = new Scanner(System.in);
System.out.println("请您输入年:");
int year = sc.nextInt();
System.out.println("请您输入月:");
int month = sc.nextInt();
System.out.println("请您输入日:");
int day = sc.nextInt();
int[] day_num = {
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int res = 0; // 用于记录当日是一年中的第几天
if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
day_num[1] = 29;
} // 考虑闰年的情况
if(month >= 1 && month <= 12 && day >= 1 && day <= day_num[month-1]){
for(int i = 1; i < month; i++){
res += day_num[i-1];
}
System.out.println(year + "-" + month + "-" + day + "是当年的第" + (res+day) + "天。");
break;
}else {
System.out.println("您输入的年月日不符合实际情况,请重新输入哦!");
}
}
}
}
运行结果:
题目描述:
编程找出1000以内的所有完数并打印出来。所谓完数就是一个数恰好等于它的因子之和,如:6=1+2+3
注意点:
- 默认1不算完数,设置一个变量记录因子和
代码如下:
public class homework2 {
public static void main(String[] args) {
System.out.println("1000以内的所有完数如下:");
for(int i = 2; i <= 1000; i++){
// 1不算完数,从2开始遍历。
int sum = 0; // 用于记录该数的因子和
for(int j = 1; j < i; j++){
if(0 == i % j){
sum += j;
}
}
if(i == sum){
System.out.print(i + " ");
}
}
System.out.println();
}
}
运行结果:
题目描述:
实现双色球抽奖游戏中奖号码的生成,中奖号码由6个红球号码和1个蓝球号码组成。
其中红球号码要求随机生成6个1~33之间不重复的随机号码。
其中蓝球号码要求随机生成1个1~16之间的随机号码。
注意点:
rd.nextInt(10)
代表随机生成0-9- 需要设置标记位判断是否生成重复数字
代码如下:
import java.util.Random;
public class homework3 {
public static void main(String[] args) {
System.out.println("随机生成一个双色球抽奖游戏的中奖号码");
int[] flag = new int[7]; // 用于判断是否有重复数字, 最后一个位置为标记位
Random rd = new Random();
for(int i = 0; i < flag.length - 1; i++){
flag[6] = 0; // 用于判断是否打印该数字的标记,该数字只有在不为重复数字的情况下才为0
flag[i] = rd.nextInt(33)+1;
for(int j = 0; j < i; j++){
if(flag[i] == flag[j]){
i--;
flag[6] = -1;//说明该数字重复了
}
}
if(0 == flag[6]){
System.out.print(flag[i] + " ");
}
}
System.out.println(rd.nextInt(16)+1);
}
}
运行结果:
题目描述:
自定义数组扩容规则,当已存储元素数量达到总容量的80%时,扩容1.5倍。例如,总容量是10,当输入第8个元素时,数组进行扩容,容量从10变15。
注意点:
-
扩容新数组时n*1.5未必是整数,需要强制转换类型
-
原数组搬移至扩容后的新数组,改变的是栈区保存的指向堆区的地址
代码如下:
import java.util.Scanner;
import java.util.Arrays;
public class homework4 {
public static void main(String[] args) {
System.out.println("请输入初始的数组元素个数:");
Scanner sc = new Scanner(System.in);
int len = sc.nextInt();
int[] auto_arr = new int[len]; // 初始化数组
System.out.println("请向数组中添加元素:");
int i = 0;
while(true) {
auto_arr[i++] = sc.nextInt();
if((i)/(len*1.0) >= 0.8){
int[] auto_arr_new = new int[(int)(len * 1.5)]; //创建一个扩容后的新数组
for(int j = 0; j < auto_arr.length; j++) {
//将原数据搬移至新数组中
auto_arr_new[j] = auto_arr[j];
}
auto_arr = auto_arr_new; //将原栈区数组的指向扩容后的新数组
len = auto_arr.length; //修改数组的长度
System.out.println(Arrays.toString(auto_arr)); //打印扩容后的数组,可省略
}
}
}
}
运行结果:
题目描述:
使用双重循环实现五子棋游戏棋盘的绘制
注意点:
-
为了节约空间成本,不使用二维数组
-
利用ASCII码实现数字到字符的转化
代码如下:
public class homework5 {
public static void main(String[] args) {
for(int i = 0; i < 17; i++) {
for(int j = 0; j < 17; j++) {
if(0 == i && 0 == j) {
System.out.print(" ");
}else if(0 == i && j <= 10) {
//控制第一行的情况
System.out.print((j-1) + " ");
}else if(0 == i && j <= 16) {
System.out.print((char)(j-11+97) + " ");
}else if(0 == j && i <= 10) {
// 控制第一列的情况
System.out.print((i-1) + " ");
}else if(0 == j && i <= 16) {
System.out.print((char)(i-11+97) + " ");
}else {
System.out.print("+ ");
}
}
System.out.println();
}
}
}
运行结果: