請問哪里錯了??/
public class HelloWorld {
? ??
? ? //完成 main 方法
? ? public static void main(String[] args) {
? ? int[] score={89,23,64,91,119,52,73} ;? ?
? ? ?HelloWorld hello=new HelloWorld();? ?
? ? ? ? hello.print(score);
? ? ? ??
? ? }
? ??
? ? //定義方法完成成績排序并輸出前三名的功能
? ? public void print(int scores[])
? ? {int a;
? ? ? ? for(int i=0;i<scores.length;i++){
? ? ? ? ? ? if(scores[i]>scores[i-1]){
? ? ? ? ? ? ? ? a=scores[i];
? ? ? ? ? ? ? ? scores[i]=scores[i-1];
? ? ? ? ? ? ? ? scores[i-1]=a;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? for(int i=0;i<3;i++){
? ? ? ? ? ? System.out.print(scores[i]);
? ? ? ? }
? ? }
? ??
? ??
? ??
2020-02-19
import java.util.Arrays;
public class HelloWorld {
? ??
? ? //完成 main 方法
? ? public static void main(String[] args) {
? ? ? ??
? ? ? ? int[] scores = {89,-23,64,91,119,52,73};
? ? ? ? HelloWorld hw = new HelloWorld();
? ? ? ? hw.sort3(scores);
? ? ? ? for(int ii= 3; ii>0; ii--)
? ? ? ? {
? ? ? ? ? ? System.out.println(scores[scores.length-ii]);
? ? ? ? }
? ? }
? ??
? ? //定義方法完成成績排序并輸出前三名的功能
? ??
? ? public void sort3(int[] scores)
? ? {
? ? ? ? Arrays.sort(scores);
? ? }
2020-02-18
你這個也不是冒泡排序法啊, 而且還存在數組越界
?for(int i=0;i<scores.length;i++){
? ? ? ? ? ? if(scores[i]>scores[i-1]){
? ? ? ? ? ? ? ? a=scores[i];
? ? ? ? ? ? ? ? scores[i]=scores[i-1];
? ? ? ? ? ? ? ? scores[i-1]=a;
就是這段
真正的冒泡排序是這樣的
?for(int i=1;i<scores.length;i++)
????for(int j=0; j<scores.length-i;j++)? ? ?
?? ? ? if(scores[j]<scores[j+1]){
? ? ? ? ? ? ? ? a=scores[j];
? ? ? ? ? ? ? ? scores[j]=scores[j+1];
? ? ? ? ? ? ? ? scores[j+1]=a;
????????????????}