國文,英文,管理學,程設,經濟學,
3,2,3,3,2,
60,61,62,63,64,
70,71,72,73,74,
85,90,95,80,70,

package chap1;

import java.io.*;
 
public class test1218 {

  public static void main(String[] args) throws Exception {
   BufferedReader fr = new BufferedReader(new FileReader("data1.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 = "", subjectname[] = new String[20];

   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(str1 + "\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++;
    }
   }
   while ((str = fr.readLine()) != null) {
    System.out.println();
    j = 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 < str.length(); k++) {
     total = total + score[k] * credit[k];
     creditsum = creditsum + credit[k];
    }
    avg = total / creditsum;
    System.out.print("學期成績" + avg);
   }
  }
 }

---練習---

package chap1;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class t1218 {
  public static void main(String args[])throws Exception{
  BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
   
  String subjectname[] = new String [5];
  int score[] = new int [5];
  int credit[] = new int[5];
  
 for(int i=0;i<5;i++){
   System.out.println("請輸入科目名稱");
   subjectname[i]=br.readLine();
   System.out.println("請輸入成績");
   score[i]=Integer.parseInt(br.readLine());
   System.out.println("請輸入學分數");
   credit[i]=Integer.parseInt(br.readLine());
  
   System.out.println("科目名稱:"+subjectname[i]+"\t成績"+score[i]+"\t學分數"+credit[i]);
  
  
 }
 
 int sum=0,creditsum=0,avg;
 
 for(int j=0;j<score.length;j++){
 
 sum=sum+score[j]*credit[j];
  System.out.println("加權分數:"+sum);
    creditsum=creditsum+credit[j];
  System.out.println("學分數加總:");
 
  avg=sum/creditsum; 
 
  System.out.println("加權平均:"+avg);
 
 
   }}}

----------------------------

 資料檔

國文,英文,管理學,程設,經濟學,
85,90,95,80,70,
3,2,3,3,2,

---

package chap1;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;

public class t1218 {
  public static void main(String args[])throws Exception{
    BufferedReader fr = new BufferedReader (new FileReader("data.txt"));

  String str;
  String subjectname[] = new String [5];
  int score[] = new int [5];
  int credit[] = new int[5];
  int i=0;
 
  
 while((str=fr.readLine())  !=null){
 
   System.out.println(str);
  
 }

---

 

 

package chap1;

import java.io.BufferedReader;
import java.io.FileReader;

public class t1218 {
  public static void main(String args[])throws Exception{
    BufferedReader fr = new BufferedReader (new FileReader("data.txt"));

  String str = null,str1="";  //"" = 空字串
  String subjectname[] = new String [5];
  int score[] = new int [5];
  int credit[] = new int[5];
  int j=0,num;
  
  str = fr.readLine();
  for (int i=0;i<str.length();i++){
  if (str.charAt(i)  !=','){
  str1= str1 + str.charAt(i);  //+ 為字元串接
  }
  else{
   subjectname[j] = str1;
   j++;
     System.out.println(subjectname[j]);
  str1 = "";

   
  
 }}}}
 

---

package chap1;

import java.io.BufferedReader;
import java.io.FileReader;

public class t1218 {
  public static void main(String args[])throws Exception{
    BufferedReader fr = new BufferedReader (new FileReader("data.txt"));

  String str = null,str1="";  //"" = 空字串
  String subjectname[] = new String [5];
  int score[] = new int [5];
  int credit[] = new int[5];
  int j=0,num=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;
   j++;
    // System.out.println(subjectname[j]);
  str1 = "";
  System.out.println(str);
  }
  } 
  
  
  
 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{
    score[j] = num;
 num=0;
 j++;
 System.out.println(score[j]);
  }
  }}}

---

 

 

package chap1;

import java.io.BufferedReader;
import java.io.FileReader;

public class t1218 {
 public static void main(String args[]) throws Exception {
  BufferedReader fr = new BufferedReader(new FileReader("data.txt"));

  String str = null, str1 = ""; // "" = 空字串
  String subjectname[] = new String[5];
  int score[] = new int[5];
  int credit[] = new int[5];
  int  num = 0;

  str = fr.readLine();
  for (int i = 0; i < str.length(); i++) {
   if (str.charAt(i) != ',') {
    str1 = str1 + str.charAt(i); // + 為字元串接
   } else {
int j=0;
    subjectname[j] = str1;
    j++;

    System.out.print(str1);
    str1 = "";
   }

  }

  str = fr.readLine();
  for (int i = 0; i < str.length(); i++) {
   if (str.charAt(i) != ',') {
    num = num * 10 + str.charAt(i) - 48;
   } else {
    int j=0;
    score[j] = num;
    num = 0;
    j++;
    System.out.println(score[j]);

   }
  }
 }
}

 ---

 

package chap1;

import java.io.BufferedReader;
import java.io.FileReader;

public class t1218 {
 public static void main(String args[]) throws Exception {
  BufferedReader fr = new BufferedReader(new FileReader("data.txt"));

  String str = null, str1 = ""; // "" = 空字串
  String subjectname[] = new String[5];
  int score[] = new int[5];
  int credit[] = new int[5];
  int  num = 0,total=0,avg;
int l=0;
  str = fr.readLine();
  for (int i = 0; i < str.length(); i++) {
   if (str.charAt(i) != ',') {
    str1 = str1 + str.charAt(i); // + 為字元串接
   } else {
    int j=0;
    subjectname[j] = str1;
    System.out.print(str1);
    str1 = "";
    j++; 
   }
  }

  str = fr.readLine();
  for (int i = 0; i < str.length(); i++) {
   if (str.charAt(i) != ',') {
    num = num * 10 + str.charAt(i) - 48;
   } else {
    int j=0;
    score[j] = num;
    num = 0;
   
    System.out.println(score[j]);
    j++;
   }
  }
  
  str = fr.readLine();
  for (int i = 0; i < str.length(); i++) {
   if (str.charAt(i) != ',') {
    l = str.charAt(i)-48;
   }
  else {
   int j=0;
   credit[j] = l;
   num = 0;
  System.out.println(credit[j]);
   j++;
  } 
       
 }
   

    
   }
  }

 ---------

 

package chap1;
import java.io.*;


public class t1218 {
 public static void main(String[] args) throws Exception {
  BufferedReader fr=new BufferedReader(new FileReader("data1.txt"));
  String str="";String str1="";
  int i=0;int num=0;int j=0; int k=0;int total=0;int creditsum=0;int avg;
  String subjectname[]=new String[10];
  int score[]=new int[10];
  int credit[]=new int[10];
  
   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("");
   str=fr.readLine();
   for (i=0;i<str.length();i++){
    if(str.charAt(i)!=','){
    k=(str.charAt(i)-48);
   // System.out.print(k);
    }
    else{
     j=0;
     credit[j]=k;
     System.out.print(credit[j]+"\t");
     j++;
   
    }
   }
   System.out.println("");
   while((str=fr.readLine())!=null){
   for (i=0;i<str.length();i++){
    if(str.charAt(i)!=','){
     num=num*10+str.charAt(i)-48;
   // System.out.println(num);
    }
    else{
     j=0;
     score[j]=num;
     num=0; 
     System.out.print(score[j]+"\t");
     j++;
    }
   } 

   System.out.println("");
   for(int m=0;m<score.length;m++){
     total=total+score[m]*credit[m];
     creditsum = creditsum+credit[m];
    }
    avg = total/creditsum;
    System.out.println("學期成績"+avg);
    }
 }


}

 

----------------------------------------

 

正確版:

國文,英文,管理學,程設,經濟學,
3,2,3,3,2,
60,61,62,63,64,
70,71,72,73,74,
85,90,95,80,70,

---

package chap1;

import java.io.*;
 
public class test1218 {

  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 = "", subjectname[] = new String[20];

   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);
   }
  }
 }

 

 

arrow
arrow
    全站熱搜

    玥君 發表在 痞客邦 留言(0) 人氣()