亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

老師有一段代碼有問題。大家看一下我分析的對不對?

http://img1.sycdn.imooc.com//5f550d8600013f8707210167.jpg圖片中 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++){}

正在回答

7 回答

或者你這樣寫也行

for(int?i?=(month?-1)*4;i<month*4;i++){
????if?(i>34){
????????break;
????}else?if?(i?==(w?-1)){
????????System.out.println("√"?+contentList[i]);
????}else?{
????????????System.out.println(contentList[i]);
????}
}


0 回復 有任何疑惑可以回復我~
#1

憶_卿 提問者

你這個比我后面寫的更簡潔,我的寫復雜了,老師這編程 還是可以繼續優化的。
2020-09-14 回復 有任何疑惑可以回復我~
#2

慕數據3999539

這么寫打印不出來多少周沒有數據的正確結果呀
2020-10-03 回復 有任何疑惑可以回復我~
完整版的來了,后面的老鐵可以借鑒哈
package?imoon;

import?java.util.Scanner;

public?class?Demo02?{
????public?static?void?main(String[]?args)?{
????????//創建String數組,并為每一個元素賦值。賦值內容為第幾周+學習內容
????????String[]?contentList?=?new?String[35];
????????contentList[0]?=?"【第1周】環境搭建與語法入門";
????????contentList[1]?=?"【第2周】Java語法之循環、數組與方法";
????????contentList[2]?=?"【第3周】面向對象之封裝與繼承";
????????contentList[3]?=?"【第4周】面向對象之單例模式與多態";
????????contentList[4]?=?"【第5周】常用工具類(上)";
????????contentList[5]?=?"【第6周】常用工具類(下)";
????????contentList[6]?=?"【第7周】常用工具類(下)";
????????contentList[7]?=?"【第8周】前端基礎之HTML與CSS";
????????contentList[8]?=?"【第9周】前端基礎之JavaScript與綜合案例";
????????contentList[9]?=?"【第10周】Java?Web基礎";
????????contentList[10]?=?"【第11周】Java?Web基礎";
????????contentList[11]?=?"【第12周】常用功能與過濾器";
????????contentList[12]?=?"【第13周】監聽器與項目實戰";
????????contentList[13]?=?"【第14周】監聽器與項目實戰";
????????contentList[14]?=?"【第15周】MySQL基礎";
????????contentList[15]?=?"【第16周】MySQL基礎";
????????contentList[16]?=?"【第17周】Java數據庫開發基礎";
????????contentList[17]?=?"【第18周】框架前置知識";
????????contentList[18]?=?"【第19周】MyBatis基礎";
????????contentList[19]?=?"【第20周】MyBatis實現OA系統項目實戰";
????????contentList[20]?=?"【第21周】MyBatis實現OA系統項目實戰";
????????contentList[21]?=?"【第22周】Linux環境搭建與Redis應用";
????????contentList[22]?=?"【第23周】Spring基礎";
????????contentList[23]?=?"【第24周】Spring基礎";
????????contentList[24]?=?"【第25周】Spring基礎";
????????contentList[25]?=?"【第26周】SSM開發社交網站";
????????contentList[26]?=?"【第27周】Spring?Boot電商項目實戰";
????????contentList[27]?=?"【第28周】Spring?Boot電商項目實戰";
????????contentList[28]?=?"【第29周】面試";
????????contentList[29]?=?"【第30周】多線程與分布式";
????????contentList[30]?=?"【第31周】多線程與分布式";
????????contentList[31]?=?"【第32周】Spring?Cloud基礎";
????????contentList[32]?=?"【第33周】Spring?Cloud電商實戰";
????????contentList[33]?=?"【第34周】Spring?Cloud電商實戰";
????????contentList[34]?=?"【第35周】Zookeeper+Dubbo應用與面試";

????????//提示信息
????????System.out.print("您要開始第幾周學習啦,直接輸入數字吧:");
????????//設置變量存儲接收到的數據
????????int?num??=?new?Scanner(System.in).nextInt();
????????if(num?>?contentList.length)
????????{
????????????System.out.println("您輸入的數字超過了你學習的周數,請重新輸入");
????????????return;
????????}

????????//定義個變量接收月份;
????????int?mouth?=?0;
????????//定義一個變量接收周
????????int?zhou?=?0;

????????//計算今天是幾月(1-月第一周、4-月第4周)
????????if(num?%?4?==?0)
????????{
????????????mouth?=?num/4;
????????}
????????else
????????{
????????????mouth?=?num/4?+1;
????????}
????????//計算輸入的周是這個月的第幾周
????????zhou?=?num?-?(mouth-1)?*?4;

????????System.out.println("今天是您學習的第"+mouth+"個月,第"+zhou+"周");
????????//提示信息
????????System.out.println("以下是您本月的學習計劃,?√?代表當周學習任務");
????????System.out.println("=======================================");

????????//利用for循環,找到數組中對應這個月的內容輸出(考慮*4是否超過數組的長度)
????????for(int?i?=?(mouth?-1?)?*?4;i<?(mouth?*?4<contentList.length???mouth?*?4?:?contentList.length);i++)?{
????????????if(i?==?(num-1))
????????????{
????????????????System.out.println("√"+contentList[i]);
????????????}
????????????else
????????????{
????????????????System.out.println(contentList[i]);
????????????}

????????}
????????//根據當前月第幾周,在輸出此行時,前面添加“√”
????}
}


0 回復 有任何疑惑可以回復我~

http://img1.sycdn.imooc.com//619c99880001168b05690606.jpg簡便寫法

0 回復 有任何疑惑可以回復我~

http://img1.sycdn.imooc.com//5f5f6b7800010a8007250391.jpg


這是我自己后面寫的以及運行結果,那個數組下標越界的解決方法 我自己寫的稍微麻煩了一點 。


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]);

}

}

? ? ?}

}

}


}


0 回復 有任何疑惑可以回復我~
????????//提示信息
????????System.out.print("您要開始第幾周學習啦,直接輸入數字吧:");
????????//設置變量存儲接收到的數據
????????int??inputnum?=?new?Scanner(System.in).nextInt();

????????//計算今天是幾月(1-月第一周、4-月第4周)
????????int?m,w;
????????if?(inputnum?%?4?!=?0?){
????????????m?=?inputnum?/?4?+?1;
????????????w?=?inputnum?%?4;
????????}else{
????????????m?=?inputnum?/?4;
????????????w?=?4;
????????}
????????
????????//為下面for提供起始值
????????int?startnum;
????????if?(inputnum?>?3)?{
????????????startnum?=?inputnum?-?4;
????????}else?{
????????????startnum?=?0;
????????}
????????
????????if?(inputnum?>?35)?{
????????????System.out.println("以超出課程范圍");
????????????System.exit(0);
????????}
????????
????????//提示信息
????????System.out.println("以下是您本月的學習計劃,?√?代表當周學習任務");
????????System.out.println("=======================================");
????????System.out.println("已學習了"+?m?+?"個月,第"?+?w?+?"周!");
????????
????????//利用for循環,找到數組中對應這個月的內容輸出
????????for?(int?i?=?startnum;?i?<?startnum+4;?i++)?{????????????
????????????//根據當前月第幾周,在輸出此行時,前面添加“√”
????????????if?(i?==?inputnum-1)?{
????????????????contentList[inputnum-1]?=?"√"?+?contentList[inputnum-1];
????????????}?????????????
????????????System.out.println(contentList[i]);????????????
????????}

我用了個笨辦法

0 回復 有任何疑惑可以回復我~
#1

憶_卿 提問者

沒有笨辦法,實用就行,多謝直接。
2020-09-14 回復 有任何疑惑可以回復我~

你這樣寫也不對,如果輸入不是最后一月的,循環會直接循環到結尾,可以這樣:

for(int?i?=?(month?-?1)?*?4?;?i?<?month?*?4?&&?i?<?contentList.length?;?i++){

}


0 回復 有任何疑惑可以回復我~
#1

憶_卿 提問者

這種也可以,比我后面寫的更簡便, 謝謝指教。
2020-09-14 回復 有任何疑惑可以回復我~

http://img1.sycdn.imooc.com//5f550f660001c98a10180462.jpg我用老師的源代碼,輸入了33-35測試了? 這是結果。

0 回復 有任何疑惑可以回復我~
#1

qq_慕姐5581389

簡單直接 再多一個空值在第35個下標,然后頂上new那里改成36
2020-09-12 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

老師有一段代碼有問題。大家看一下我分析的對不對?

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號