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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

大家看看我這個(gè)是怎么回事?。课疫@個(gè) 運(yùn)行不出來(lái)但是看不出哪里有錯(cuò)

import?java.util.Arrays;public?class?HelloWorld?{????????//完成?main?方法????public?static?void?main(String[]?args)?{????????int[]?scores?=?{89,-23,64,91,119,52,73};??????//??system.out.println("考試成績(jī)的前三名為:")????????HelloWorld?hello?=?new?HelloWorld();????????hello.marks(scores);????????????????????????????}????????//定義方法完成成績(jī)排序并輸出前三名的功能????public?void?marks(int?[]?scores){????????int?[]?arrays={};????????int?count?=?0;????????Arrays.sort(scores);????????for(int?i=scores.length-1;i>=0;i--)????????{????????????if(scores[i]<0?||?scores[i]>100){????????????????continue;????????????}????????????else{????????????count++;????????????for(int?j=0;j<3;j++)????????????arrays[j]=scores[i];????????????if?(count==3)????????????????break;????????????}????????}????????System.out.println("考試成績(jī)的前三名為"+Arrays.toString(arrays));???????????}

import java.util.Arrays;

public class HelloWorld {

? ??

? ? //完成 main 方法

? ? public static void main(String[] args) {

? ? ? ? int[] scores = {89,-23,64,91,119,52,73};

? ? ? //? system.out.println("考試成績(jī)的前三名為:")

? ? ? ? HelloWorld hello = new HelloWorld();

? ? ? ? hello.marks(scores);

? ? ? ??

? ? ? ??

? ? ? ??

? ? }

? ??

? ? //定義方法完成成績(jī)排序并輸出前三名的功能

? ? public void marks(int [] scores){

? ? ? ? int [] arrays={};

? ? ? ? int count = 0;

? ? ? ? Arrays.sort(scores);

? ? ? ? for(int i=scores.length-1;i>=0;i--)

? ? ? ? {

? ? ? ? ? ? if(scores[i]<0 || scores[i]>100){

? ? ? ? ? ? ? ? continue;

? ? ? ? ? ? }

? ? ? ? ? ? else{

? ? ? ? ? ? count++;

? ? ? ? ? ? for(int j=0;j<3;j++)

? ? ? ? ? ? arrays[j]=scores[i];

? ? ? ? ? ? if (count==3)

? ? ? ? ? ? ? ? break;

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? System.out.println("考試成績(jī)的前三名為"+Arrays.toString(arrays));

? ? ? ?

? ? }

? ??


正在回答

5 回答

spacer.gif

另外,你運(yùn)行的效果應(yīng)該是報(bào)錯(cuò)數(shù)組下限超出范圍吧,因?yàn)槟愣x的arrays【】給的是個(gè){}---空,然后我給你改了下,自己看下效果是怎樣的

2 回復(fù) 有任何疑惑可以回復(fù)我~
#1

qq_長(zhǎng)山_0 提問(wèn)者

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0是這個(gè)錯(cuò)誤。 為什么不能定義數(shù)組為空呢?為空 的話 是不能賦值了嗎? 你改了以后沒(méi)有錯(cuò)誤了
2018-07-30 回復(fù) 有任何疑惑可以回復(fù)我~
#2

qq_摸摸頭_4 回復(fù) qq_長(zhǎng)山_0 提問(wèn)者

定義數(shù)組有三種方式,分為靜態(tài)初始化和動(dòng)態(tài)初始化, 舉個(gè)例子,假如現(xiàn)在我們要定義一個(gè)整形數(shù)組,int [] array=new []{1,2,3}; int [] array={1,2,3};這倆種是靜態(tài)初始化,就是在定義的時(shí)候直接就把值給了,命名規(guī)則是:變量類型 【】 數(shù)組名=new []{數(shù)組中的值}; 動(dòng)態(tài)初始化就是:int 【】 array=new [數(shù)組的長(zhǎng)度]; array={1,2,3}; 可以不用直接聲明的時(shí)候就是初始化,在之后靈活存入。 如果定義數(shù)組為空,那么這個(gè)數(shù)組的長(zhǎng)度就為0了,還怎么存入數(shù)值。
2018-08-01 回復(fù) 有任何疑惑可以回復(fù)我~
#3

qq_長(zhǎng)山_0 提問(wèn)者 回復(fù) qq_摸摸頭_4

嗯嗯懂得了,非常感謝
2018-08-08 回復(fù) 有任何疑惑可以回復(fù)我~

https://img1.sycdn.imooc.com//5b5e73420001042c05280291.jpg看圖就明白了

1 回復(fù) 有任何疑惑可以回復(fù)我~
#1

qq_長(zhǎng)山_0 提問(wèn)者

明白了明白了 謝謝謝謝
2018-07-30 回復(fù) 有任何疑惑可以回復(fù)我~

import java.util.Arrays;

public class HelloWorld {

// 完成 main 方法

public static void main(String[] args) {

int[] scores = { 89, -23, 64, 91, 119, 52, 73 };

// system.out.println("考試成績(jī)的前三名為:")

HelloWorld hello = new HelloWorld();

hello.marks(scores);

}

// 定義方法完成成績(jī)排序并輸出前三名的功能

public void marks(int[] scores) {

// int[] arrays = {};

System.out.println("考試成績(jī)前三名為:");

int count = 0;

Arrays.sort(scores);

for (int i = scores.length - 1; i >= 0; i--)

{

if (scores[i] < 0 || scores[i] > 100) {

continue;

}

else {

count++;

System.out.println(scores[i]);

/*

* for (int j = 0; j < 3; j++)

* arrays[j] = scores[i];

*/

/*

* 首先你這個(gè)for循環(huán)沒(méi)有{}劃分范圍,其次我不知道你為什么又定義一個(gè)數(shù)組arrays用來(lái)存儲(chǔ)成績(jī)的前三名,

* 既然定義了count,讓它<=3,這就可以直接控制scores數(shù)組的輸出 ,再然后你這個(gè)內(nèi)for循環(huán)的作用你仔細(xì)看看,

* 它在外for循環(huán)里面,就算你運(yùn)行成功了,也是每次將同一個(gè)值賦給不同下標(biāo)的arrays數(shù)組值

*/

if (count == 3)

break;

}

}

// System.out.println("考試成績(jī)的前三名為" + Arrays.toString(arrays));

}

}


1 回復(fù) 有任何疑惑可以回復(fù)我~
#1

qq_長(zhǎng)山_0 提問(wèn)者

我做這個(gè)題的時(shí)候想起來(lái)前面的 直接用這個(gè)函數(shù)輸出數(shù)組了。就想用這個(gè)方法
2018-07-30 回復(fù) 有任何疑惑可以回復(fù)我~

在你的代碼的循環(huán)中,你用了雙重循環(huán),當(dāng)分?jǐn)?shù)滿足在0到100分之間時(shí),你將分?jǐn)?shù)用一個(gè)循環(huán)輸入到arrays數(shù)組中,但是實(shí)際上此時(shí)的scores[i]是一個(gè)值,你用j的這個(gè)循環(huán)將同樣的值給了arrays數(shù)組,也就是說(shuō)else執(zhí)行完過(guò)后,arrays數(shù)組中是三個(gè)相同的值,而跳到外層循環(huán)之后,再次遇到滿足條件的scores[i]時(shí),會(huì)覆蓋掉你原來(lái)的arrays數(shù)組,所以你的代碼輸出的結(jié)果是第三名的分?jǐn)?shù),并且是三次吧。我沒(méi)運(yùn)行你的代碼,只是看了看,問(wèn)題應(yīng)該是在這。

2 回復(fù) 有任何疑惑可以回復(fù)我~
#1

qq_長(zhǎng)山_0 提問(wèn)者

對(duì)你說(shuō)的對(duì)。但是 這里錯(cuò)的話程序不會(huì)報(bào)錯(cuò) 只會(huì)出來(lái) 三個(gè)同樣的值。報(bào)錯(cuò)的原因是 我定義了一個(gè)空的 arrays數(shù)組
2018-07-30 回復(fù) 有任何疑惑可以回復(fù)我~
#2

qq_長(zhǎng)山_0 提問(wèn)者

你說(shuō)的這個(gè)也是個(gè)很大的錯(cuò)誤 謝謝謝謝
2018-07-30 回復(fù) 有任何疑惑可以回復(fù)我~
#3

zczzsh 回復(fù) qq_長(zhǎng)山_0 提問(wèn)者

額,數(shù)組為空這個(gè)問(wèn)題我還真沒(méi)發(fā)現(xiàn),抱歉。因?yàn)槲移綍r(shí)用C++比較多,C++中數(shù)組是可以這么初始化為空的吧,Java里的話我沒(méi)這么定義過(guò),都是直接用new給分配空間了,具體為啥不能為空的話可能是Java中不能這么初始化數(shù)組吧。。。
2018-07-31 回復(fù) 有任何疑惑可以回復(fù)我~
#4

qq_長(zhǎng)山_0 提問(wèn)者 回復(fù) zczzsh

恩恩好的謝謝
2018-08-01 回復(fù) 有任何疑惑可以回復(fù)我~
查看1條回復(fù)
import?java.util.Arrays;public?class?HelloWorld?{????????//完成?main?方法????public?static?void?main(String[]?args)?{	???int[]?scores=?{89,-23,64,91,119,52,73};		System.out.println("考試成績(jī)的前三名為:");		HelloWorld?scoresTop3=new?HelloWorld();//創(chuàng)建對(duì)象		scoresTop3.showTop3(scores);????}????????//定義方法完成成績(jī)排序并輸出前三名的功能????	public?void?showTop3(int[]?scores)?{		Arrays.sort(scores);//排序數(shù)組		int?num?=0;//保存有效成績(jī)		for(int?i=scores.length-1;i>=0;i--)?{			if(scores[i]<0||scores[i]>100)?{				continue;			}			num++;			if(num>3)?{				break;			}			System.out.println(scores[i]);					}	}}


0 回復(fù) 有任何疑惑可以回復(fù)我~
#1

qq_長(zhǎng)山_0 提問(wèn)者

你這就是答案啊,我這個(gè)哪里錯(cuò)了?
2018-07-28 回復(fù) 有任何疑惑可以回復(fù)我~

舉報(bào)

0/150
提交
取消

大家看看我這個(gè)是怎么回事啊?我這個(gè) 運(yùn)行不出來(lái)但是看不出哪里有錯(cuò)

我要回答 關(guān)注問(wèn)題
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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