一維陣列的練習
阿新 • • 發佈:2018-12-10
public class Array1 { public static void main(String[] args){ Pritimive d=new Pritimive(); for (int i=0;i<d.t.length;i++){ System.out.println(d.t[i]); } d.t[0]=true; d.t[1]=true; d.t[2]=true; for (int j=0;j<d.t.length;j++){ System.out.println(d.t[j]); } } }class Pritimive{ boolean[] t=new boolean[3]; }
import java.util.Scanner; public class w { public static void main(String[] args){ //從鍵盤輸入學生的個數 Scanner s=new Scanner(System.in); System.out.println("請輸入學生人數:"); int count=s.nextInt();//寫入學生個數 System.out.println("請輸入"+count+"個成績:");//定義一個數組儲存分數 int[] scores=new int[count]; //定義最高分 int maxscore=0; //找到最高分 for (int i=0;i< scores.length;i++){ int score=s.nextInt();//寫入學生成績 scores[i]=score; if (scores[i]>maxscore){ maxscore=scores[i]; } }//輸出最高分 System.out.println("最高分是:"); System.out.println(maxscore); //輸出學生成績 for (int i=0;i<scores.length;i++){ char grade; if (scores[i]>=maxscore-10){ grade='A'; }else if (scores[i]>=maxscore-20){ grade='B'; }else if (scores[i]>=maxscore-30){ grade='C'; }else{ grade='D'; } System.out.println("student "+i+" score is "+scores[i]+" grade is "+grade); } } }