關(guān)于數(shù)組的輸入和輸出(請(qǐng)問(wèn)哪里出錯(cuò)了)
public class s001 {
? public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("請(qǐng)輸入您的分?jǐn)?shù)");
? for(int stuNum=1;stuNum<=5;stuNum++){
? int[] scores={input.nextInt()};}
??
? System.out.println("請(qǐng)輸入您的學(xué)號(hào)");
int i=input.nextInt();
System.out.println(scores[i]);
? }
? }
2015-06-23
你這代碼問(wèn)題相當(dāng)嚴(yán)重,
int[] scores={input.nextInt()};
相當(dāng)于:
int a=input.nextInt();
int[] scores={a};
數(shù)組長(zhǎng)度永遠(yuǎn)是1.
2015-07-06
//
我做了個(gè)相似的
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; ? ?//班級(jí)人數(shù)
? ? System.out.println("請(qǐng)輸入本班的人數(shù):");
? ? sum=input.nextInt();
? ? int scores[]=new int[sum]; ?//定義學(xué)生成績(jī)數(shù)組,并分配空間
? ? for(int j=0;j<sum;j++) ? ? ?//通過(guò)for循環(huán)通過(guò)控制臺(tái)輸入,并錄入學(xué)生成績(jī)于數(shù)組中
? ? {
? ? System.out.println("請(qǐng)輸入學(xué)號(hào)為"+(j+1)+"的成績(jī)");
? ? ? ? scores[j]=input.nextInt();
? ? }
? ? while(choise) ? ? ? ? ? ? ? //是否繼續(xù)查詢學(xué)生成績(jī)
? ? {
? ? ? System.out.println("請(qǐng)輸入查詢成績(jī)學(xué)生的學(xué)號(hào):");
? ? ? int score=input.nextInt();
? ? ? System.out.println("學(xué)號(hào)為"+score+"學(xué)生成績(jī)?yōu)椋?+scores[score-1]);
? ? ? System.out.println("是否繼續(xù)查詢? y/n");
? ? ? choises=input.next();
? ? ? if(choises.equals("y")){}
? ? ? else if(choises.equals("n"))
? ? ? {
? ? ?choise=false;
? ? ? }
? ? }
? ? System.out.println("程序結(jié)束了...");
? }
}
2015-06-23
不清楚你的想法,這是修改后可以運(yùn)行的代碼。