亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

關于數組的輸入和輸出(請問哪里出錯了)

public class s001 {

? public static void main(String[] args) {

Scanner input=new Scanner(System.in);

System.out.println("請輸入您的分數");

? for(int stuNum=1;stuNum<=5;stuNum++){

? int[] scores={input.nextInt()};}

??

? System.out.println("請輸入您的學號");

int i=input.nextInt();

System.out.println(scores[i]);

? }

? }


正在回答

3 回答

你這代碼問題相當嚴重,

int[] scores={input.nextInt()};

相當于:

int a=input.nextInt();

int[] scores={a};

數組長度永遠是1.

2 回復 有任何疑惑可以回復我~
#1

最后的地球人 提問者

非常感謝!
2015-06-23 回復 有任何疑惑可以回復我~

//


我做了個相似的

import java.util.Scanner;

?

public class shuZu

{

? public static void main(String[] args)?

? {

? ? Scanner input=new Scanner(System.in);

? ? boolean choise=true; ?

? ? String choises;

? ? int sum; ? ?//班級人數

? ? System.out.println("請輸入本班的人數:");

? ? sum=input.nextInt();

? ? int scores[]=new int[sum]; ?//定義學生成績數組,并分配空間

? ? for(int j=0;j<sum;j++) ? ? ?//通過for循環通過控制臺輸入,并錄入學生成績于數組中

? ? {

? ? System.out.println("請輸入學號為"+(j+1)+"的成績");

? ? ? ? scores[j]=input.nextInt();

? ? }

? ? while(choise) ? ? ? ? ? ? ? //是否繼續查詢學生成績

? ? {

? ? ? System.out.println("請輸入查詢成績學生的學號:");

? ? ? int score=input.nextInt();

? ? ? System.out.println("學號為"+score+"學生成績為:"+scores[score-1]);

? ? ? System.out.println("是否繼續查詢? y/n");

? ? ? choises=input.next();

? ? ? if(choises.equals("y")){}

? ? ? else if(choises.equals("n"))

? ? ? {

? ? ?choise=false;

? ? ? }

? ? }

? ? System.out.println("程序結束了...");

? }

}


1 回復 有任何疑惑可以回復我~
#1

玄鑒

太牛了
2016-05-04 回復 有任何疑惑可以回復我~
import?java.util.Scanner;

public?class?Answer1?{
??public?static?void?main(String[]?args)?{
	Scanner?input=new?Scanner(System.in);
	System.out.println("請輸入您的分數");
	int?[]?scores?=?new?int[6];
	??for(int?stuNum=1;stuNum<=5;stuNum++){
????????????scores[stuNum]=input.nextInt();
	??}
	??
	System.out.println("請輸入您的學號");
	int?i=input.nextInt();
	System.out.println(scores[i]);
??}
}

不清楚你的想法,這是修改后可以運行的代碼。

1 回復 有任何疑惑可以回復我~
#1

askshow2

這能運行??
2015-07-06 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消
Java入門第一季(IDEA工具)升級版
  • 參與學習       1167461    人
  • 解答問題       18748    個

0基礎萌新入門第一課,從Java環境搭建、工具使用、基礎語法開始

進入課程

關于數組的輸入和輸出(請問哪里出錯了)

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號