為啥我用java.util.Scanner會(huì)報(bào)錯(cuò)
import?java.util.Scanner;
public class HelloWorld {
? ? public static void main(String[] args) {
? ? ? ? //創(chuàng)建String數(shù)組,并為每一個(gè)元素賦值。賦值內(nèi)容為第幾周+學(xué)習(xí)內(nèi)容
? ? ? ? String[] contentList = new String[35];
? ? ? ? contentList[0] = "【第1周】環(huán)境搭建與語(yǔ)法入門";
? ? ? ? contentList[1] = "【第2周】Java語(yǔ)法之循環(huán)、數(shù)組與方法";
? ? ? ? contentList[2] = "【第3周】面向?qū)ο笾庋b與繼承";
? ? ? ? contentList[3] = "【第4周】面向?qū)ο笾畣卫J脚c多態(tài)";
? ? ? ? contentList[4] = "【第5周】常用工具類(上)";
? ? ? ? contentList[5] = "【第6周】常用工具類(下)";
? ? ? ? contentList[6] = "【第7周】常用工具類(下)";
? ? ? ? contentList[7] = "【第8周】前端基礎(chǔ)之HTML與CSS";
? ? ? ? contentList[8] = "【第9周】前端基礎(chǔ)之JavaScript與綜合案例";
? ? ? ? contentList[9] = "【第10周】Java Web基礎(chǔ)";
? ? ? ? contentList[10] = "【第11周】Java Web基礎(chǔ)";
? ? ? ? contentList[11] = "【第12周】常用功能與過(guò)濾器";
? ? ? ? contentList[12] = "【第13周】監(jiān)聽器與項(xiàng)目實(shí)戰(zhàn)";
? ? ? ? contentList[13] = "【第14周】監(jiān)聽器與項(xiàng)目實(shí)戰(zhàn)";
? ? ? ? contentList[14] = "【第15周】MySQL基礎(chǔ)";
? ? ? ? contentList[15] = "【第16周】MySQL基礎(chǔ)";
? ? ? ? contentList[16] = "【第17周】Java數(shù)據(jù)庫(kù)開發(fā)基礎(chǔ)";
? ? ? ? contentList[17] = "【第18周】框架前置知識(shí)";
? ? ? ? contentList[18] = "【第19周】MyBatis基礎(chǔ)";
? ? ? ? contentList[19] = "【第20周】MyBatis實(shí)現(xiàn)OA系統(tǒng)項(xiàng)目實(shí)戰(zhàn)";
? ? ? ? contentList[20] = "【第21周】MyBatis實(shí)現(xiàn)OA系統(tǒng)項(xiàng)目實(shí)戰(zhàn)";
? ? ? ? contentList[21] = "【第22周】Linux環(huán)境搭建與Redis應(yīng)用";
? ? ? ? contentList[22] = "【第23周】Spring基礎(chǔ)";
? ? ? ? contentList[23] = "【第24周】Spring基礎(chǔ)";
? ? ? ? contentList[24] = "【第25周】Spring基礎(chǔ)";
? ? ? ? contentList[25] = "【第26周】SSM開發(fā)社交網(wǎng)站";
? ? ? ? contentList[26] = "【第27周】Spring Boot電商項(xiàng)目實(shí)戰(zhàn)";
? ? ? ? contentList[27] = "【第28周】Spring Boot電商項(xiàng)目實(shí)戰(zhàn)";
? ? ? ? contentList[28] = "【第29周】面試";
? ? ? ? contentList[29] = "【第30周】多線程與分布式";
? ? ? ? contentList[30] = "【第31周】多線程與分布式";
? ? ? ? contentList[31] = "【第32周】Spring Cloud基礎(chǔ)";
? ? ? ? contentList[32] = "【第33周】Spring Cloud電商實(shí)戰(zhàn)";
? ? ? ? contentList[33] = "【第34周】Spring Cloud電商實(shí)戰(zhàn)";
? ? ? ? contentList[34] = "【第35周】Zookeeper+Dubbo應(yīng)用與面試";
? ? ? ? //提示信息
? ? ? ? System.out.print("您要開始第幾周學(xué)習(xí)啦,直接輸入數(shù)字吧:");
? ? ? ? //設(shè)置變量存儲(chǔ)接收到的數(shù)據(jù)
? ? ? ? int n = new Scanner(System.in).nextInt();?
? ? ? ? //int n = 11;
? ? ? ? //計(jì)算今天是幾月(1-月第一周、4-月第4周)
? ? ? ? int m ;
? ? ? ? m = (n/4)+1;
? ? ? ??
? ? ? ? //計(jì)算輸入的周是這個(gè)月的第幾周
? ? ? ? int w ;
? ? ? ? w = n%4;
? ? ? ? //提示信息
? ? ? ? System.out.println("以下是您本月的學(xué)習(xí)計(jì)劃, √ 代表當(dāng)周學(xué)習(xí)任務(wù)");
? ? ? ? System.out.println("=======================================");
? ? ? ??
? ? ? ? //利用for循環(huán),找到數(shù)組中對(duì)應(yīng)這個(gè)月的內(nèi)容輸出
? ? ? ? for(int i=(m-1)*4 ; i < (m*4) && i < contentList.length; i++){
? ? ? ??
? ? ? ? ? ? //根據(jù)當(dāng)前月第幾周,在輸出此行時(shí),前面添加“√”
? ? ? ? ? ? if (i == n-1){
? ? ? ? ? ? ? ? System.out.println("√"+contentList[i]);
? ? ? ? ? ? }
? ? ? ? ? ? else {
? ? ? ? ? ? ? ? System.out.println(contentList[i]);
? ? ? ? ? ? }
? ? ? ? } ? ?
? ? }
}
報(bào)錯(cuò):
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at HelloWorld.main(HelloWorld.java:45)
注釋掉“int n = new Scanner(System.in).nextInt(); ”,用“int n = 11;”就是對(duì)的。
2021-09-16