import java.util.Scanner;
public class OneArrayDemo{
public static void main(String[] args){
Shirt[] myShop = new Shirt[10];
Scanner scanner = new Scanner(System.in);
String temp1;
myShop[0] = new Shirt();
System.out.print("Please input the description: ");
temp1 = scanner.nextLine();
myShop[0].description = temp1;
System.out.print("Please input the ShirtID: ");
myShop[0].shirtID = scanner.nextInt();
System.out.print("Please input the colorCode: ");
temp1 = scanner.next();
myShop[0].colorCode = temp1.charAt(0);
System.out.print("Please input the price: ");
myShop[0].price = scanner.nextDouble();
myShop[0].displayInformation();
}
}
Shirt 請參考另一篇文章!
二位陣列使用方式:
import java.util.Scanner;
public class TwoArrayDemo{
public static void main(String[] args){
int courses = 3;
int students = 10;
int[][] scores = new int[courses][students];
System.out.println("請輸入數學科分數:");
TwoArrayDemo.inputScore(1,students,scores);
System.out.println();
System.out.println("請輸入國文科分數:");
TwoArrayDemo.inputScore(2,students,scores);
System.out.println();
System.out.println("請輸入英文科分數:");
TwoArrayDemo.inputScore(3,students,scores);
System.out.println();
System.out.println("每位學生的平均分數:");
TwoArrayDemo.avgScore(courses,students,scores);
}
public static void inputScore(int course,int students, int[][] scores){
Scanner scanner = new Scanner(System.in);
int sum = 0;
for (int i = 0; i < students; i++){
System.out.printf("請輸入第 %d 位學生成績:",(i+1));
scores[course-1][i] = scanner.nextInt();
sum += scores[course-1][i];
}
System.out.println("本科平均分數:" + ((double)sum)/students);
}
public static void avgScore(int courses,int students,int[][] scores){
int temp = 0;
for (int i = 0 ; i < students ; i++){
for (int j = 0 ; j < courses ; j++){
temp += scores[j][i];
}
System.out.printf("第 %d 位學生平均分數: %.2f\n",i, ((double)temp)/courses);
temp = 0;
}
}
}
ArrayList 的使用方式:
import java.util.*;
public class ArrayListDemo{
public static void main(String[] args){
ArrayList myList = new ArrayList();
myList.add("Peter");
myList.add("James");
myList.add("Piggy");
myList.add("Janeson");
myList.add("Tordy");
myList.remove(0);
myList.remove(myList.size()-1);
myList.remove("Piggy");
System.out.println(myList);
}
}