關于數組的輸入和輸出(請問哪里出錯了)
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]);
? }
? }
2015-06-23
你這代碼問題相當嚴重,
int[] scores={input.nextInt()};
相當于:
int a=input.nextInt();
int[] scores={a};
數組長度永遠是1.
2015-07-06
//
我做了個相似的
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("程序結束了...");
? }
}
2015-06-23
不清楚你的想法,這是修改后可以運行的代碼。