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();
}
}
}
運行結果:

轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/64756.html
標籤:其他
下一篇:WINAMOVA1.55
