課程
/后端開(kāi)發(fā)
/Java
/Java入門(mén)第一季(IDEA工具)
/85/1598/uvK/HelloWorld.java:13: error:expected ? ? ? ? ? ?for(int j=0,int k=0;j
2017-04-23
源自:Java入門(mén)第一季(IDEA工具) 7-1
正在回答
?
//完成 main 方法
? ? public static void main(String[] args) {
? ? ? ? int[] scores={89,-23,64,91,119,52,73};
? ? ? ? System.out.println("考試成績(jī)的前三名為:");
? ? ? ? HelloWorld sort=new HelloWorld(); ? ?
????????sort.sort(scores)?; ? ????????//下面定義了方法你不調(diào)用??怎么輸出
? ? }
? ? /*
你的sort方法:用兩個(gè)for循環(huán)你是想將數(shù)組重新排序么,
我沒(méi)有去看你這方法對(duì)不對(duì),你可以專(zhuān)門(mén)去把這個(gè)排序去實(shí)現(xiàn),
數(shù)組排序有java自帶的方法實(shí)現(xiàn)——Arrays.sort(數(shù)組名),但要先聲明import java.util.Arrays;
*/
? ? //定義方法完成成績(jī)排序并輸出前三名的功能
? ? public void sort(int[] scores){
? ? ? ? for(int i=0;i<scores.length;i++){????????
? ? ? ? ? ? for(int j=0,int k=0;j<scores.length;j++){
? ? ? ? ? ? ? ? if(<scores[j]>scores[i]){????????? ??
? ? ? ? ? ? ? ? ? ? k=scores[i];????????????????????????????
? ? ? ? ? ? ? ? ? ? scores[i]=scores[j];
? ? ? ? ? ? ? ? ? ? scores[j]=k;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
????????????????//?System.out.println(scores[0]+","+scores[1]+","+scores[2]);這個(gè)是對(duì)的
? ? ? ? System.out.println(scores[0]+scores[1]+scores[2]);????//輸出方式這樣輸出是三個(gè)數(shù)的和
以下是我寫(xiě)的,實(shí)現(xiàn)了作業(yè)要求
import java.util.*;
public class HelloWorld {
? ? //完成 main 方法
? ?public static void main(String[] args) {
? ? ? ?int[] scores = {89,-23,64,91,52,73};
? ? ? ?HelloWorld score1 = new HelloWorld();
? ? ? ?score1.print(scores); ? ? ?
? ?}
? ?
? ?//定義方法完成成績(jī)排序并輸出前三名的功能
? ?public void print(int[] scores){
? ? ? ?Arrays.sort(scores);
? ? ? ?int j=0;
? ? ? ?for(int i = scores.length - 1;i >= 0;i--){
? ? ? ? ? ?if(scores[i] < 0 || scores[i] > 100)
? ? ? ? ? ? continue;
? ? ? ? ? System.out.println(scores[i]);
? ? ? ? ? j++;
? ? ? ? ? if(j >=3)
? ? ? ? ? break;
? ? ? ? ? } ? ? ??
? ? ? ?} ??
}
public?static?void?main(String[]?args)?{ ????????int[]?scores={89,-23,64,91,119,52,73}; ????????System.out.println("考試成績(jī)的前三名為:"); ????????HelloWorld?sort=new?HelloWorld();???? ????} ???? ????//定義方法完成成績(jī)排序并輸出前三名的功能 ????public?void?sort(int[]?scores){????????//這個(gè)sort方法你根本沒(méi)調(diào)用 ????????for(int?i=0;i<scores.length;i++){ ????????????for(int?j=0,int?k=0;j<scores.length;j++){???//j已經(jīng)int了,k就不需要再int了 ????????????????if(<scores[j]>scores[i]){?????//就算調(diào)用了這個(gè)方法,i,j都是0,if不會(huì)執(zhí)行 ????????????????//寫(xiě)多了個(gè)"<" ????????????????????k=scores[i]; ????????????????????scores[i]=scores[j]; ????????????????????scores[j]=k; ????????????????} ????????????} ????????} ????????System.out.println(scores[0]+scores[1]+scores[2]); ????}
總體上來(lái)說(shuō),你這個(gè)程序根本不能運(yùn)行,還有程序如果報(bào)錯(cuò),就不要運(yùn)行了。我一放進(jìn)去,一堆報(bào)錯(cuò)。還是照著慕課網(wǎng)的例子學(xué)比較好。
for循環(huán)只能定義一個(gè)變量。你定了兩個(gè),所以錯(cuò)了
????//定義方法完成成績(jī)排序并輸出前三名的功能 ????public?void?sort(int[]?scores){ ????????for(int?i=0;i<scores.length;i++){ ????????????for(int?j=0,int?k=0;j<scores.length;j++){?//?for(int?j=0,k=0;... ????????????????if(<scores[j]>scores[i]){ ????????????????????k=scores[i]; ????????????????????scores[i]=scores[j]; ????????????????????scores[j]=k; ????????????????} ????????????} ????????} ????????System.out.println(scores[0]+scores[1]+scores[2]); ????????//?此處不應(yīng)該直接直接使用+相連,否則三個(gè)數(shù)會(huì)做相加運(yùn)算,可以改成如下形式 ????????System.out.println(scores[0]+","+scores[1]+","+scores[2]); ????}
舉報(bào)
0基礎(chǔ)萌新入門(mén)第一課,從Java環(huán)境搭建、工具使用、基礎(chǔ)語(yǔ)法開(kāi)始
2 回答為什么不能這樣編寫(xiě),為什么這樣編寫(xiě)就是錯(cuò)誤的?
4 回答這樣寫(xiě)為什么會(huì)輸出錯(cuò)誤呀
3 回答幫忙我這樣寫(xiě)有什么錯(cuò)誤嗎?
2 回答這樣寫(xiě)為什么說(shuō)我if else錯(cuò)誤了
1 回答請(qǐng)問(wèn)為什么不能這樣寫(xiě)
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號(hào)-11 京公網(wǎng)安備11010802030151號(hào)
購(gòu)課補(bǔ)貼聯(lián)系客服咨詢(xún)優(yōu)惠詳情
慕課網(wǎng)APP您的移動(dòng)學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號(hào)
2017-05-12
?
//完成 main 方法
? ? public static void main(String[] args) {
? ? ? ? int[] scores={89,-23,64,91,119,52,73};
? ? ? ? System.out.println("考試成績(jī)的前三名為:");
? ? ? ? HelloWorld sort=new HelloWorld(); ? ?
????????sort.sort(scores)?; ? ????????//下面定義了方法你不調(diào)用??怎么輸出
? ? }
? ? /*
你的sort方法:用兩個(gè)for循環(huán)你是想將數(shù)組重新排序么,
我沒(méi)有去看你這方法對(duì)不對(duì),你可以專(zhuān)門(mén)去把這個(gè)排序去實(shí)現(xiàn),
數(shù)組排序有java自帶的方法實(shí)現(xiàn)——Arrays.sort(數(shù)組名),但要先聲明import java.util.Arrays;
*/
? ? //定義方法完成成績(jī)排序并輸出前三名的功能
? ? public void sort(int[] scores){
? ? ? ? for(int i=0;i<scores.length;i++){????????
? ? ? ? ? ? for(int j=0,int k=0;j<scores.length;j++){
? ? ? ? ? ? ? ? if(<scores[j]>scores[i]){????????? ??
? ? ? ? ? ? ? ? ? ? k=scores[i];????????????????????????????
? ? ? ? ? ? ? ? ? ? scores[i]=scores[j];
? ? ? ? ? ? ? ? ? ? scores[j]=k;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
????????????????//?System.out.println(scores[0]+","+scores[1]+","+scores[2]);這個(gè)是對(duì)的
? ? ? ? System.out.println(scores[0]+scores[1]+scores[2]);????//輸出方式這樣輸出是三個(gè)數(shù)的和
? ? }
以下是我寫(xiě)的,實(shí)現(xiàn)了作業(yè)要求
import java.util.*;
public class HelloWorld {
? ? //完成 main 方法
? ?public static void main(String[] args) {
? ? ? ?int[] scores = {89,-23,64,91,52,73};
? ? ? ?HelloWorld score1 = new HelloWorld();
? ? ? ?score1.print(scores); ? ? ?
? ?}
? ?
? ?//定義方法完成成績(jī)排序并輸出前三名的功能
? ?public void print(int[] scores){
? ? ? ?Arrays.sort(scores);
? ? ? ?int j=0;
? ? ? ?for(int i = scores.length - 1;i >= 0;i--){
? ? ? ? ? ?if(scores[i] < 0 || scores[i] > 100)
? ? ? ? ? ? continue;
? ? ? ? ? System.out.println(scores[i]);
? ? ? ? ? j++;
? ? ? ? ? if(j >=3)
? ? ? ? ? break;
? ? ? ? ? } ? ? ??
? ? ? ?} ??
}
2017-04-26
總體上來(lái)說(shuō),你這個(gè)程序根本不能運(yùn)行,還有程序如果報(bào)錯(cuò),就不要運(yùn)行了。我一放進(jìn)去,一堆報(bào)錯(cuò)。還是照著慕課網(wǎng)的例子學(xué)比較好。
2017-04-24
for循環(huán)只能定義一個(gè)變量。你定了兩個(gè),所以錯(cuò)了
2017-04-24