我有幾個來自用戶的字串輸入(學生的注冊人數),我必須根據他們的學習年份將它們存盤到不同的陣列中并列印輸出。例如,如果字串以“18”開頭,我必須將其存盤為四年級學生。如果它以“19”開頭,我必須將其存盤為三年級學生,依此類推。
import java.util.Scanner;
public class reg {
public static void main(String args[]){
Scanner scan = new Scanner(System.in);
System.out.println("Enter no of students");
int n = scan.nextInt();
scan.nextLine();
String[] arr = new String[n];
System.out.println("Enter registration numbers");
for(int i = 0;i<n;i ){
arr[i]=scan.nextLine();
}
String[] arr1 = new String[n];
String[] arr2 = new String[n];
String[] arr3 = new String[n];
String[] arr4 = new String[n];
String[] arr5 = new String[n];
for(int i = 0;i<n;i ){
if(arr[i].startsWith("18")){
for(int j = 0;j<n;j ){
arr1[j] = arr[i];
}
}
else if(arr[i].startsWith("19")){
for(int j = 0;j<n;j ){
arr2[j] = arr[i];
}
}
else if(arr[i].startsWith("20")){
for(int j = 0;j<n;j ){
arr3[j] = arr[i];
}
}
else if(arr[i].startsWith("21")){
for(int j = 0;j<n;j ){
arr4[j] = arr[i];
}
}
else{
for(int j = 0;j<n;j ){
arr5[j] = arr[i];
}
}
}
System.out.println("4th year students are");
for(int i = 0;i<n;i ){
System.out.println(arr1[i]);
System.out.println();
}
System.out.println("3rd year students are");
for(int i = 0;i<n;i ){
System.out.println(arr2[i]);
System.out.println();
}
System.out.println("2nd year students are");
for(int i = 0;i<n;i ){
System.out.println(arr3[i]);
System.out.println();
}
System.out.println("1st year students are");
for(int i = 0;i<n;i ){
System.out.println(arr4[i]);
System.out.println();
}
}
}
我得到的輸出只是與我的條件匹配的最后一個輸入被列印 n 次(學生輸入的數量)。
我該如何解決這個問題?
編輯 :
輸入和預期輸出:
學生人數:
7
輸入注冊號:
20ABC123
19ABC111
18ABC000
18ABC001
21ABX144
20ABX000
19ABC099
預期產出:四年級學生是
18ABC000
18ABC001
三年級學生是
19ABC111
19ABC099
二年級學生是
20ABC123
20ABX000
一年級學生是
21ABX144
uj5u.com熱心網友回復:
您需要引入新的 5 個變數來跟蹤每個類別中的學生總數,以便您可以將下一個學生放在類別中的下一個空位置。
Scanner scan = new Scanner(System.in);
System.out.println("Enter no of students");
int n = scan.nextInt();
scan.nextLine();
String[] arr = new String[n];
System.out.println("Enter registration numbers");
for(int i = 0;i<n;i ){
arr[i]=scan.nextLine();
}
String[] arr1 = new String[n];
String[] arr2 = new String[n];
String[] arr3 = new String[n];
String[] arr4 = new String[n];
String[] arr5 = new String[n];
int studntFirstYear = 0;
int studntSecondYear = 0;
int studntThirdYear = 0;
int studntForthYear = 0;
int studntElse = 0;
for(int i = 0;i<n;i ){
if(arr[i].startsWith("18")){
arr1[studntForthYear] = arr[i];
studntForthYear ;
}
else if(arr[i].startsWith("19")){
arr2[studntThirdYear] = arr[i];
studntThirdYear ;
}
else if(arr[i].startsWith("20")){
arr3[studntSecondYear] = arr[i];
studntSecondYear ;
}
else if(arr[i].startsWith("21")){
arr4[studntFirstYear] = arr[i];
studntFirstYear ;
}
else{
arr5[studntElse] = arr[i];
studntElse ;
}
}
System.out.println("4th year students are");
for(int i = 0;i<studntForthYear;i ){
System.out.println(arr1[i]);
System.out.println();
}
System.out.println("3rd year students are");
for(int i = 0;i<studntThirdYear;i ){
System.out.println(arr2[i]);
System.out.println();
}
System.out.println("2nd year students are");
for(int i = 0;i<studntSecondYear;i ){
System.out.println(arr3[i]);
System.out.println();
}
System.out.println("1st year students are");
for(int i = 0;i<studntFirstYear;i ){
System.out.println(arr4[i]);
System.out.println();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/414441.html
標籤:
上一篇:當一個物件作為另一個類的建構式的引數傳遞時,它是否參考同一個物件?
下一篇:十進制值問題
