8-1最后一個編程
以下是自己的思路編的,哪位大神看下哪里有錯,編完后只顯示運行成功,沒有出現結果。
import java.util.Arrays;
public class HelloWorld {
? ??
? ? //完成 main 方法
? ? public static void main(String[] args) {
? ? ? ??
? ? ? ?int[] scores={89,-23,64,91,119,52,73};?
? ? ? ?HelloWorld hello=new HelloWorld();?
? ? ? ?hello.Top(scores);?
? ? }
? ??
? ? //定義方法完成成績排序并輸出前三名的功能
? ? public void Top(int[] scores){
? ? ? ? Arrays.sort(scores);
? ? ? ? for(int i=0;i<scores.length;i++){
? ? ? ? ? ? int j=0;
? ? ? ? ? ? if(scores[i]>0&&scores[i]<100){
? ? ? ? ? ? ? ? ?j=j+1;
? ? ? ? ? ? if(j>3){
? ? ? ? ? ? ? ?System.out.println("考試成績前三名是:");?
? ? ? ? ? ? ? for(int k=scores.length;k>scores.length-4;k--){
? ? ? ? ? ? ? ? ? ?System.out.println(scores[k]);
? ? ? ? ? ? ? ?}
? ? ? ? ? ? }
? ? ? ? ? ?}?
? ? ? ? ? ??
? ? ? ? }
? ? ? ??
? ? }
2018-09-04
你把j=0一直放在for循環里面了,導致不會執行if(j>3)之后的命令,所以沒有結果。而且后面的程序也有問題
2018-09-02
我試了一遍 ,可以運行
2018-09-02
package com.imooc;
import java.util.Arrays;
public class aaa {
//導入java.util.Arrays;
? ?public static void main(String[] args) { ? ? ? ?
? ? ? ? // 創建對象,對象名為hello
? ?aaa hello = new aaa(); ? ? ? ?
? ? ? ?// 調用方法并將返回值保存在變量中
int[] nums = hello.getArray(8); ? ? ? ?
? ? ? ?// 將數組轉換為字符串并輸出
System.out.println(Arrays.toString(nums));?
}
/*
* 功能:創建指定長度的int型數組,并生成100以內隨機數為數組中的每個元素賦值
* 定義一個帶參帶返回值的方法,通過參數傳入數組的長度,返回賦值后的數組
*/
public int[] getArray(int length) {
? ? ? ?// 定義指定長度的整型數組
int[] nums = new int[length]; ? ? ? ?
? ? ? ?// 循環遍歷數組賦值
for (int i=0;i<nums.length;i++ ) { ? ? ? ? ? ?
// 產生一個100以內的隨機數,并賦值給數組的每個成員
? ?nums[i]=(int)(Math.random()*100); ? ? ? ?
}
return nums; // 返回賦值后的數組
}
}
2018-09-02
System.out.println(Arrays.toString(nums));? ?這一行這么輸出和題目要求不符。
該用一個for循環輸出一個數后換行輸出下一個數,應該就可以過了。