----------------------------課前練習------------
李正合,蘇炎坤,李天祥,王平,
國文,英文,管理學,程設,經濟學,
3,2,3,3,3,
80,85,90,95,100,
70,75,80,85,90,
90,90,90,90,90,
95,95,95,95,95,
---
package chap1;
import java.io.*;
public class t1223 {
public static void main(String[] args) throws Exception {
BufferedReader fr = new BufferedReader(new FileReader("data.txt"));
int creditsum = 0, avg = 0, total = 0, k = 0, i = 0, j = 0;
int num = 0, score[] = new int[20], credit[] = new int[20];
String str = "", str1 = "", name[]=new String [20],subjectname[] = new String[20];
str=fr.readLine();
for (i=0;i<str.length();i++){
if(str.charAt(i) !=','){
str1=str1+str.charAt(i);
}else{
name[j]=str1;
System.out.print(name[j]+"\t");
j++;
str1="";
}
}
System.out.println();
j=0;
str = fr.readLine();
for (i = 0; i < str.length(); i++) {
if (str.charAt(i) != ',') {
str1 = str1 + str.charAt(i);
} else {
subjectname[j] = str1;
System.out.print(subjectname[j]+"\t");
j++;
str1 = "";
}
}
System.out.println();
j = 0;
str = fr.readLine();
for (i = 0; i < str.length(); i++) {
if (str.charAt(i) != ',') {
num = num * 10 + str.charAt(i) - 48;
} else {
credit[j] = num;
num = 0;
System.out.print(credit[j] + "\t");
j++;
}
}
for (k = 0; k < credit.length; k++) {
creditsum = creditsum + credit[k];
}
while ((str = fr.readLine()) != null) {
System.out.println();
j = 0;
total=0;
for (i = 0; i < str.length(); i++) {
if (str.charAt(i) != ',') {
num = num * 10 + str.charAt(i) - 48;
} else {
score[j] = num;
num = 0;
System.out.print(score[j] + "\t");
j++;
}
}
for (k = 0; k < score.length; k++) {
total = total + score[k] * credit[k];
}
avg = total / creditsum;
System.out.print("學期成績" + avg);
}
}
}
------------------------
課程
package chap1;
import java.io.BufferedReader;
import java.io.FileReader;
public class t1223 {
public static void main(String[] args) throws Exception {
BufferedReader fr = new BufferedReader(new FileReader("data.txt"));
int totalcredit=0,creditsum=0,avg=0,num=0,total=0,j=0,k=0;
int score[]=new int [20],credit[]=new int [20];
String stuname[]=new String [20],subjectname[]=new String [20];
String str="",str1="";
str=fr.readLine();
for (int i=0;i<str.length();i++){
if(str.charAt(i) !=','){
str1=str1+str.charAt(i);
}else{
stuname[j]=str1;
System.out.print(stuname[j]+"\t");
str1=""; j++;
}
}
System.out.println();
j=0;
str = fr.readLine();
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) != ',') {
str1 = str1 + str.charAt(i);
} else {
subjectname[j] = str1;
str1 = "";j++;
}
}
System.out.println();
j = 0; str = fr.readLine();
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) != ',') {
num = num * 10 + str.charAt(i) - 48;
} else {
credit[j] = num;
num = 0;
totalcredit=totalcredit+credit[j];
System.out.print(credit[j] + "\t");
}}
System.out.print(totalcredit);
}}
---
package chap1;
import java.io.BufferedReader;
import java.io.FileReader;
public class t1223 {
public static void main(String[] args) throws Exception {
BufferedReader fr = new BufferedReader(new FileReader("data.txt"));
int totalcredit=0,creditsum=0,fscore=0,avg=0,num=0,total=0,j=0,k=0;
int score[]=new int [20],credit[]=new int [20];
String stuname[]=new String [20],subjectname[]=new String [20];
String str="",str1="";
str=fr.readLine();
for (int i=0;i<str.length();i++){
if(str.charAt(i) !=','){
str1=str1+str.charAt(i);
}else{
stuname[j]=str1;
System.out.print(stuname[j]+"\t");
str1=""; j++;
}
}
System.out.println();
j=0;
str = fr.readLine();
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) != ',') {
str1 = str1 + str.charAt(i);
} else {
subjectname[j] = str1;
str1 = "";j++;
}
}
System.out.println();
j = 0; str = fr.readLine();
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) != ',') {
num = num * 10 + str.charAt(i) - 48;
} else {
credit[j] = num;
num = 0;
totalcredit=totalcredit+credit[j];
System.out.print(credit[j] + "\t");
}}
System.out.print(totalcredit);
System.out.println();//排版用 無意義
System.out.print(stuname[0]+"\t");
str=fr.readLine();j=0;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) != ',') {
num = num * 10 + str.charAt(i) - 48;
} else {
score[j]=num;
System.out.print(score[j]+"\t");
num=0; j++;
}
}
for (int i = 0; i < score.length; i++) {
total=total+score[i]*credit[i];
}
fscore=total/totalcredit;
System.out.print(total+"\t"+fscore);
}
}
---
package chap1;
import java.io.BufferedReader;
import java.io.FileReader;
public class t1223 {
public static void main(String[] args) throws Exception {
BufferedReader fr = new BufferedReader(new FileReader("data.txt"));
int totalcredit=0,creditsum=0,fscore=0,avg=0,num=0,total=0,j=0,k=0;
int score[]=new int [20],credit[]=new int [20];
String stuname[]=new String [20],subjectname[]=new String [20];
String str="",str1="";
str=fr.readLine();
for (int i=0;i<str.length();i++){
if(str.charAt(i) !=','){
str1=str1+str.charAt(i);
}else{
stuname[j]=str1;
System.out.print(stuname[j]+"\t");
str1=""; j++;
}
}
System.out.println();
j=0;
str = fr.readLine();
for (int i = 0;i<str.length(); i++) {
if (str.charAt(i) != ',') {
str1 = str1 + str.charAt(i);
} else {
subjectname[j] = str1;
System.out.print(subjectname[j]+"\t");
str1 = "";j++;
}
}
System.out.println();
j = 0; str = fr.readLine();
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) != ',') {
num = num * 10 + str.charAt(i) - 48;
} else {
credit[j] = num;
num = 0;
totalcredit=totalcredit+credit[j];
System.out.print(credit[j] + "\t");
}}
System.out.print(totalcredit);
while((str=fr.readLine()) !=null){
System.out.println();
System.out.print(stuname[k]+"\t");
k++;
j=0;
for (int i = 0; i < str.length(); i++) { //分數
if (str.charAt(i) != ',') {
num = num * 10 + str.charAt(i) - 48;
} else {
score[j]=num;
System.out.print(score[j]+"\t");
num=0; j++;
}
}
for (int i = 0; i < j; i++) {
total=total+score[i]*credit[i];
}
fscore=total/totalcredit;
System.out.print(total+"\t"+fscore);
}
}
}
------------------------二維陣列
package chap1;
import java.io.BufferedReader;
import java.io.FileReader;
public class t1223 {
public static void main(String[] args) throws Exception {
BufferedReader fr = new BufferedReader(new FileReader("data.txt"));
int totalcredit=0,creditsum=0,fscore=0,avg=0,num=0,total=0,j=0,k=0;
int score[][]=new int [10][10],credit[]=new int [20];
String stuname[]=new String [20],subjectname[]=new String [20];
String str="",str1="";
str=fr.readLine();
for (int i=0;i<str.length();i++){
if(str.charAt(i) !=','){
str1=str1+str.charAt(i);
}else{
stuname[j]=str1;
System.out.print(stuname[j]+"\t");
str1=""; j++;
}
}
System.out.println();
j=0;
str = fr.readLine();
for (int i = 0;i<str.length(); i++) {
if (str.charAt(i) != ',') {
str1 = str1 + str.charAt(i);
} else {
subjectname[j] = str1;
System.out.print(subjectname[j]+"\t");
str1 = "";j++;
}
}
System.out.println();
j = 0; str = fr.readLine();
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) != ',') {
num = num * 10 + str.charAt(i) - 48;
} else {
credit[j] = num;
num = 0;
totalcredit=totalcredit+credit[j];
System.out.print(credit[j] + "\t");
}}
System.out.print(totalcredit);
while((str=fr.readLine()) !=null){
System.out.println();
System.out.print(stuname[k]+"\t");
k++;
j=0;
for (int i = 0; i < str.length(); i++) { //分數
if (str.charAt(i) != ',') {
num = num * 10 + str.charAt(i) - 48;
} else {
score[k][j]=num;
System.out.print(score[k][j]+"\t");
num=0; j++;
}
}
}}
}