利用二維數組輸出學生們的各科成績

在這裏插入圖片描述
package unittest;

import java.util.Scanner;

/**

  • @author Dylaniou
  • 打印同學們的各科成績
  •  		語		數		外		英
    
    第1個同學 81 57 22 34
    第2個同學 90 98 97 65
    第3個同學 80 64 78 70
    */
    public class Test {
    public static void main ( String[] args )
    {
    int[][] scores = new int[3][4];
    for(int i = 0; i<scores.length; i++){
    System.out.println(“請輸入第” + (i+1) + “個同學的各科成績”);
    String courseName = “”;
    for(int j =0; j<scores[i].length; j++){
    Scanner scan = new Scanner(System.in);
    if(j0)
    courseName = “語”;
    if(j
    1)
    courseName = “數”;
    if(j2)
    courseName = “外”;
    if(j
    3)
    courseName = “英”;
    System.out.println(“請輸入”+ courseName + “成績”);
    if(scan.hasNextInt()){
    scores[i][j] = scan.nextInt();
    }else{
    System.out.println(“成績請輸入整數”);
    }
    }
    }
    System.out.println("\t\t 語 \t數 \t外 \t英");
    for(int i = 0; i<scores.length;i++){
    StringBuilder s = new StringBuilder(“第” + (i+1) + “個同學”);
    for(int j = 0; j<scores[i].length; j++){
    s.append("\t" + scores[i][j]);
    }
    System.out.println(s);
    }
    }
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章