大家看看我這個(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));
? ? ? ?
? ? }
? ??
2018-07-30
另外,你運(yùn)行的效果應(yīng)該是報(bào)錯(cuò)數(shù)組下限超出范圍吧,因?yàn)槟愣x的arrays【】給的是個(gè){}---空,然后我給你改了下,自己看下效果是怎樣的
2018-07-30
2018-07-30
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));
}
}
2018-07-29
在你的代碼的循環(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)該是在這。
2018-07-28