老師有一段代碼有問題。大家看一下我分析的對不對?
圖片中 for(int i=(month-1)*4;i<month*4;i++){
}這個 i<month*4;寫錯了, 如果輸入的數值是33-35的話,運行會報錯,數組下標越界,
應該這樣寫 for(int i=(month-1)*4;i<conteentList.lengthh;i++){}
圖片中 for(int i=(month-1)*4;i<month*4;i++){
}這個 i<month*4;寫錯了, 如果輸入的數值是33-35的話,運行會報錯,數組下標越界,
應該這樣寫 for(int i=(month-1)*4;i<conteentList.lengthh;i++){}
2020-09-07
舉報
2020-09-12
或者你這樣寫也行
2021-12-19
2021-11-23
2020-09-14
這是我自己后面寫的以及運行結果,那個數組下標越界的解決方法 我自己寫的稍微麻煩了一點 。
package com.Imooc;
import java.util.Scanner;
/**
?*?
?* @author?
?* 功能:慕課網Java課程包含35周學習計劃,輸入周數
?* 輸出:輸出本月(4周)與當周的學習任務
?*
?*/
public class Day11Demo {
public static void main(String[] args) {
// TODO Auto-generated method stub
String[] contentList=new String[35];//定義一個字符串數組長度為35并初始化
contentList[0]="【第一周】java環境搭建與語法入門";
contentList[1]="【第二周】java語法之循環,數組與方法";
contentList[2]="【第三周】面向對象之封裝與繼承";
contentList[3]="【第四周】面向對象之單例模式與多態";
contentList[4]="【第五周】常用工具類(上)";
contentList[5]="【第六周】常用工具類(下)";
contentList[6]="【第七周】常用工具類(下)";
contentList[7]="【第八周】前端基礎之HTML與CSS";
contentList[8]="【第九周】前端基礎之javaScript與綜合案例";
contentList[9]="【第十周】java web基礎";
contentList[10]="【第十一周】Java web基礎";
contentList[11]="【第十二周】常用功能與過濾器";
contentList[12]="【第十三周】監聽器與實戰項目";
contentList[13]="【第十四周】監聽器與實戰項目";
contentList[14]="【第十五周】MySQL基礎";
contentList[15]="【第十六周】MySQL基礎";
contentList[16]="【第十七周】java數據庫開發基礎";
contentList[17]="【第十八周】框架前置知識";
contentList[18]="【第十九周】Mybatis基礎";
contentList[19]="【第二十周】MyBatis實現OA系統項目實戰";
contentList[20]="【第二十一周】MyBatis實現OA系統項目實戰";
contentList[21]="【第二十二周】Linux環境搭建與Redis應用 ";
contentList[22]="【第二十三周】Spring基礎";
contentList[23]="【第二十四周】Spring基礎";
contentList[24]="【第二十五周】Spring基礎";
contentList[25]="【第二十六周】SMM開發社交網站";
contentList[26]="【第二十七周】Spring Boot電商項目實戰";
contentList[27]="【第二十八周】Spring Boot電商項目實戰";
contentList[28]="【第二十九周】面試";
contentList[29]="【第三十周】多線程與分布式";
contentList[30]="【第三十一周】多線程與分布式";
contentList[31]="【第三十二周】Spring Cloud基礎";
contentList[32]="【第三十三周】Spring Cloud電商實戰";
contentList[33]="【第三十四周】Spring Cloud電商實戰";
contentList[34]="【第三十五周】Zookeeper+Dubbo應用與面試";
System.out.println("你要開始第幾周學習啦!直接輸入數字吧:");
Scanner a=new Scanner(System.in);//定義一個整型week用來接收輸入的周數
int week=a.nextInt();
int month=0;//定義一個變量mouth代表月份,初始值為0
while(week<=0||week>=36) {
System.out.println("輸入的周數超出范圍,請重新輸入:");
week=a.nextInt();
}
if(week<=35&&week>0) {
if(week%4==0) {
month=week/4;
}else{
month=week/4+1;
}
//System.out.println(month);
System.out.println("以下是你本月的學習計劃,√代表當周的學習");
System.out.println("===============================");
if(week==33||week==34||week==35) {
for(int i=(month-1)*4;i<contentList.length;i++) {
if(i==week-1) {
System.out.println("√"+contentList[i]);
}
else {
System.out.println("? "+contentList[i]);
}
}
}
if(week>0&&week<32) {
for(int i=(month-1)*4;i<month*4;i++) {
if(i==week-1) {
System.out.println("√"+contentList[i]);
}
else {
? ?System.out.println("? "+contentList[i]);
}
}
? ? ?}
}
}
}
2020-09-14
我用了個笨辦法
2020-09-13
你這樣寫也不對,如果輸入不是最后一月的,循環會直接循環到結尾,可以這樣:
2020-09-07