第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

代碼錯(cuò)在哪,怎么改?

代碼錯(cuò)在哪,怎么改?

慕碼人7327609 2017-10-14 00:49:47
import java.util.Arrays;public class HelloWorld {? ? int Scores[]; ? ?? ? //完成 main 方法? ? public static void main(String[] args) {? ? int[]scores=new int[] {89 , -23 , 64 , 91 , 119 , 52 , 73}; ??? ??? ? HelloWorld cc= new HelloWorld();? ? cc.tops(scores); ? ?? ? ? ??? ? }? ??? ? //定義方法完成成績(jī)排序并輸出前三名的功能? ? public void tops(int[] scores){? ? ? ??? ? ? ??? ? ? ? for (int i=0;i<scores.length;i++){? ? ? ? ? ? int t=0;? ? ? ? ? ? if (scores[i]>0){? ? ? ? ? ? ? ? Scores[t]=scores[i];? ? ? ? ? ? ? ? t++;? ? ? ? ? ? }? ? ? ? ?Arrays.sort(Scores); ? ?int[] topt= Scores; ? ?int f=topt.length; ? ?System.out.println("考試成績(jī)的前三名為:");? ? System.out.println(topt[f]); ? ?System.out.println(topt[f-1]); ? ?System.out.println(topt[f-2]);? ? ? ??? ? ? ? } ? ?? ? }報(bào)錯(cuò):Exception in thread "main" java.lang.NullPointerException at HelloWorld.tops(HelloWorld.java:21) at HelloWorld.main(HelloWorld.java:10)? ?
查看完整描述

4 回答

?
西紅柿番茄醬

TA貢獻(xiàn)1條經(jīng)驗(yàn) 獲得超1個(gè)贊

你的Scores[] 的值是null,方法中循環(huán)賦值時(shí)會(huì)報(bào)錯(cuò)

http://img1.sycdn.imooc.com/59e1e61000010a7006830690.jpg

查看完整回答
1 反對(duì) 回復(fù) 2017-10-14
?
weibo_七彩本_0

TA貢獻(xiàn)1條經(jīng)驗(yàn) 獲得超1個(gè)贊

你的方法中的數(shù)組是空的,不報(bào)空指針才怪。另外取數(shù)組中的前三個(gè)最大的數(shù)字,你這方法也太不可取了。? 兩行代碼能解決的問(wèn)題。。。

package?Deno4;

import?java.util.Arrays;

public?class?HelloWorld?{
	?
	public?static?void?main(String[]?args)?{
		int[]?test?=?new?int[]?{?89,?-23,?64,?91,?119,?52,?73?};
		int?m?=?test.length;
		Arrays.sort(test);

		System.out.println("考試成績(jī)的前三名為:");
		System.out.println(test[m?-?1]);
		System.out.println(test[m?-?2]);
		System.out.println(test[m?-?3]);

	}
}


查看完整回答
1 反對(duì) 回復(fù) 2017-10-14
?
Developer_Zuck

TA貢獻(xiàn)112條經(jīng)驗(yàn) 獲得超42個(gè)贊

你的代碼里面有些變量不需要定義的,定義變量是為了簡(jiǎn)化開(kāi)發(fā),如果讓開(kāi)發(fā)變得復(fù)雜就不好了。而且你定義的t是用來(lái)干什么的呢?

還有,不需要循環(huán)就可以的,你的int t =0定義在for循環(huán)里,每次循環(huán)都會(huì)清零,那么t++也就沒(méi)有意義了。把你的代碼注釋了一些,運(yùn)行會(huì)有結(jié)果:

http://img3.sycdn.imooc.com/59e1665c0001222f01710085.jpg

http://img1.sycdn.imooc.com/59e1665c0001e3d804630299.jpg


查看完整回答
反對(duì) 回復(fù) 2017-10-14
?
onemoo

TA貢獻(xiàn)883條經(jīng)驗(yàn) 獲得超454個(gè)贊

你在 HelloWorld 中的數(shù)組?Scores 有什么用??

main 中你是 new 了一個(gè) scores 數(shù)組出來(lái)。tops 中你也傳遞的是 scores,可是你卻又讓?int[] topt= Scores,這個(gè) Scores 數(shù)組變量可是 null ??! 后面再使用?topt 當(dāng)然會(huì)拋異常啦。

你是不是混淆了 Scores 和 scores?

查看完整回答
反對(duì) 回復(fù) 2017-10-14
  • 慕碼人7327609
    慕碼人7327609
    因?yàn)橐サ魋core數(shù)組中的負(fù)數(shù)(題目要求)所以加了一個(gè)數(shù)組來(lái)裝
  • onemoo
    onemoo
    如果一定要新增Scores數(shù)組,那在使用前要先讓Scores變量引用一個(gè)真正的數(shù)組,比如 new int[scores.length]
  • 西紅柿番茄醬
    西紅柿番茄醬
    import java.util.Arrays; public class HelloWorld { int Scores[] = new int[6]; public static void main(String[] args) { // TODO Auto-generated method stub int[]scores=new int[] {89 , -23 , 64 , 91 , 119 , 52 , 73}; HelloWorld cc= new HelloWorld(); cc.tops(scores); } public void tops(int[] scores){ int t=0; for (int i=0;i<scores.length;i++){ if (scores[i]>0){ Scores[t]=scores[i]; t++; } } Arrays.sort(Scores); int[] topt= Scores; int f=topt.length; System.out.println("考試成績(jī)的前三名為:"); System.out.println(topt[f-1]); System.out.println(topt[f-2]); System.out.println(topt[f-3]); } }
  • 4 回答
  • 0 關(guān)注
  • 1761 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)