請(qǐng)幫忙找一下這里邊的語(yǔ)法錯(cuò)誤。希望大佬提些修改建議,謝謝
public class HelloWorld {
? ??
? ? //完成 main 方法
? ? public static void main(String[] args) {
? ? ? ? int[] scores=new int{89,-23,64,91,119,52,73};
? ? ? ? HelloWorld hello=new HelloWorld();
? ? ? ? hello.Seiseki(scores);
? ? }
? ??
? ? //定義方法完成成績(jī)排序并輸出前三名的功能
? ? public void Seiseki(int[] scores){
? ? ? ? int[] scores=new int[7];
? ? ? ? int max=scores[0];
? ? ? ? int second=scores[1];
? ? ? ? int third=scores[2];
? ? ? ? for(int i=0;i<scores.length;i++){
? ? ? ? ? ? if(scores[i]<0||scores[i]>100){
? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? }
? ? ? ? ? ? if(scores[i]>max){
? ? ? ? ? ? ? ? max=scores[i];
? ? ? ? ? ? }else if(scores[i]>second){
? ? ? ? ? ? ? ? second=scores[i];
? ? ? ? ? ? }else if(scores[i]>third){
? ? ? ? ? ? ? ? third=scores[i];
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? System.out.println("考試成績(jī)的前三名為");
? ? ? ? System.out.println(max);
? ? ? ? System.out.println(second);
? ? ? ? System.out.println(third);
? ? }
}
2019-03-09
有點(diǎn)問(wèn)題,你看如果數(shù)組里的第三個(gè)數(shù)比第一個(gè)大,就會(huì)把第三個(gè)值賦給第一個(gè)而且比較過(guò)的數(shù)之后就用不到了,這時(shí)候你的第一個(gè)數(shù)值就被覆蓋了,默認(rèn)是第一個(gè)數(shù)值比第二個(gè)大,也比第三個(gè)大,但是實(shí)際情況并不是這樣的,修改的話建議你可以用兩層循環(huán)的冒泡排序,當(dāng)然也可以用java特有的排序函數(shù),嗯嗯,就這樣。
2019-03-11
其實(shí)編程要求提示那里已經(jīng)說(shuō)了,可以利用Arrays.sort(scores);進(jìn)行排序,再倒序循環(huán),就方便很多
2019-03-09
import java.util.*;
public class HelloWorld {
? ? //完成 main 方法
? ? public static void main(String[] args) {
? ? int[] scores={89,-23,64,91,119,52,73};
HelloWorld hello=new HelloWorld();
hello.print(scores);? ??
? ? }
? ??
? ? //定義方法完成成績(jī)排序并輸出前三名的功能
? ? public static void print(int[] scores) {
Arrays.sort(scores);
int count=0;
for(int i=scores.length-1;i>=0||count<=3;i--) {
if(scores[i]<= 100 &&scores[i]>=0) {
count++;
if(count<3) {
System.out.print(scores[i]+" ");
? ? }
else if(count==3)
System.out.println(scores[i]);
? ? }
? ? }
? ? }
? ??
}
這是我的代碼你可以看看
2019-03-09
scores你是自己傳進(jìn)去的為什么要重新開(kāi)空間?這里已經(jīng)錯(cuò)了