我將無效成績設為“”,但在輸出中似乎顯示了這樣的空間https://i.stack.imgur.com/3kHZW.png如何僅顯示有效成績?無效的成績在輸出中產生空白
這是問題https://i.stack.imgur.com/uxLhG.png
這是所需的輸出https://i.stack.imgur.com/HNZpZ.png
這是我的程式
String [][] student = new String[100][3];
int stu = 0, totalp=0, totalf=0;
for(int h=0; h<100; h ) {
System.out.print("Enter Name: ");
student[h][0] = sc.nextLine();
System.out.print("Enter Grade: ");
student[h][1] = sc.nextLine();
int grade = Integer.parseInt(student[h][1]);
if(grade >100 || grade <50) {
student[h][0] = "";
student[h][1] = "";
student[h][2] = "";
System.out.println("Invalid Grade");
}
else if(grade >=75) {
student[h][2] = ("Passed");
totalp ;
}
else if(grade <=74) {
student[h][2] = ("Failed");
totalf ;
}
System.out.print("Add new record (Y/N)?: ");
char choice = sc.nextLine().charAt(0);
System.out.println("");
if(choice == 'y' || choice =='Y') {
stu ;
continue;
}
else if(choice == 'n' ||choice =='N') {
break;
}
}
System.out.println("\nGRADE SUMMARY REPORT");
System.out.println("\nName\tGrades\tRemarks");
for(int i =0; i<stu 1; i ) {
for(int h =0; h<student[i].length; h ) {
System.out.print(student[i][h] "\t");
}
System.out.println();
}
System.out.println("\nTotal Passed: " totalp);
System.out.println("Total Failed: " totalf);
uj5u.com熱心網友回復:
如果用戶輸入的成績無效,請不要將其添加到student陣列中。然后它根本不會被列印出來。您還應該處理用戶在要求輸入成績時沒有輸入數字的情況。
當詢問用戶是否要添加新記錄時,您只需檢查她是否輸入了Y. 其他任何事情都可以被認為是no。
下面的代碼使用方法printf來格式化報告。此外,為了格式化報告,我會跟蹤用戶輸入的最長名稱。
import java.util.Scanner;
public class Students {
private static final int MAX_ROWS = 100;
private static final int NAME = 0;
private static final int GRADE = 1;
private static final int REMARK = 2;
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
String[][] student = new String[MAX_ROWS][3];
int totalp = 0, totalf = 0, longest = 0;
int h = 0;
while (h < MAX_ROWS) {
System.out.print("Enter Name: ");
String name = sc.nextLine();
int len = name.length();
if (len > longest) {
longest = len;
}
System.out.print("Enter Grade: ");
String grade = sc.nextLine();
try {
int mark = Integer.parseInt(grade);
if (mark >= 50 && mark <= 100) {
student[h][NAME] = name;
student[h][GRADE] = grade;
if (mark >= 75) {
student[h][REMARK] = "Passed";
totalp ;
}
else {
student[h][REMARK] = "Failed";
totalf ;
}
h ;
if (h == MAX_ROWS) {
System.out.println("Maximum students entered.");
}
else {
System.out.print("Add new record (Y/N)?: ");
String choice = sc.nextLine();
if (!"Y".equalsIgnoreCase(choice)) {
break;
}
}
}
else {
throw new NumberFormatException();
}
}
catch (NumberFormatException xNumberFormat) {
System.out.println("Invalid. Grade is a number between 50 and 100.");
}
System.out.println();
}
System.out.println("\nGRADE SUMMARY REPORT\n");
if (longest < 4) {
longest = 4;;
}
System.out.printf("%-" longest "s", "Name");
System.out.println(" Grade Remarks");
String format = "%-" longest "s %4s %s%n";
for (int i = 0; i < h; i ) {
System.out.printf(format, student[i][NAME], student[i][GRADE], student[i][REMARK]);
}
System.out.println();
System.out.println("Total passed: " totalp);
System.out.println("Total failed: " totalf);
}
}
這是上述代碼的示例運行的輸出:
Enter Name: Superman
Enter Grade: 99
Add new record (Y/N)?: y
Enter Name: Batman
Enter Grade: 23
Invalid. Grade is a number between 50 and 100.
Enter Name: Iron Man
Enter Grade: 74
Add new record (Y/N)?: n
GRADE SUMMARY REPORT
Name Grade Remarks
Superman 99 Passed
Iron Man 74 Failed
Total passed: 1
Total failed: 1
您可能還想參考Oracle 的 Java 教程中的例外課程。
uj5u.com熱心網友回復:
現在您仍在一行中列印無效成績。您想要的是在列印之前檢查并確保等級有效。由于您將所有無效成績都設為空字串,因此您可以像這樣簡單地檢查
if(!student[i][h].isEmpty()){
System.out.print(student[i][h] "\t");
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/510369.html
標籤:爪哇数组for循环if 语句
下一篇:Python主要方法選項
