2014-10-17 61 views
0
public class IndividualProject2 { 

    public static void main(String[] args) { 
    int i; 
    for(i = 0; i < 7; i++){ 
     Scanner gradeInput = new Scanner(System.in); 
     System.out.println("Enter midterm 1 score: "); 
     int num1 = gradeInput.nextInt(); 
     System.out.println("Enter midterm 2 score: "); 
     int num2 = gradeInput.nextInt(); 
     System.out.println("Enter final score: "); 
     int num3 = gradeInput.nextInt(); 
    } 


} 

public static char function (int grade){ 
    String[] name = new String[7]; 
    char[] finalGrade = new char[7]; 
    char letter; 
    if (grade >= 90){ 
     letter = 'A'; 
     System.out.println(letter); 
    } 
    else if (grade >= 80 && grade < 90){ 
     letter = 'B';    
     System.out.println(letter); 
    } 
    else if(grade >= 70 && grade < 80){ 
     letter = 'C';    
     System.out.println(letter); 
    } 
    else{ 
     letter = 'F';    
     System.out.println(letter); 
    } 
    for(int counter=0; counter < finalGrade.length; counter++){ 
     finalGrade[counter] = letter; 
    } 
    return letter; 

    } 

    public static int function (int midtermOne, int midtermTwo) { 
    int result; 
    int[] midterm = new int[7]; 
    if (midtermOne > midtermTwo) 
    { 
     result = midtermOne; 
    } 
    else{ 
     result = midtermTwo; 
    } 
    for(int counter=0; counter < midterm.length; counter++){ 
     midterm[counter] = result; 
    } 
    return result; 
    } 
public static double function (int num1, int num2, int num3){ 
    double result; 
    double[] average = new double[7]; 
    result = (num1 + num2 + num3)/3.0; 
    for(int counter=0; counter < average.length; counter++){ 
     average[counter] = result; 
    } 
    return result; 
    } 
    } 

輸出應該是這樣的http://i.imgur.com/GHo2YS6.png 我有困難創造符合這個特定output.How一個循環做我走在循環中的投入,同時使用三種功能的方法來存儲在值陣列。我對如何做到這一點感到非常不滿。如何使用for循環來簡化將數據存儲在數組中的特定輸出?

+0

邊注:在Java中你真的很少需要使用數組。改用'List'。 – m0skit0 2014-10-17 20:25:48

+0

你在課堂上有多遠?你允許使用Java Collections(例如java.util.List) - 你允許使用額外的類嗎? – 2014-10-17 20:27:15

+0

聽起來像你需要創建一個學生班。 – brso05 2014-10-17 20:28:41

回答

0

mainfor循環之前移動Scanner gradeInput = new Scanner(System.in);。通過這種方式,你可以避免做同樣的事情,無論如何,這是無用的。

聲明四個陣列 - namemidTerm1midTerm2avg舉行4值爲每個學生。您可以在class級別或main內定義它們。

填寫爲每一個學生的東西這四個陣列狀

for(int i = 0; i < 7; i++){ 
    System.out.println("Student number " + (i + 1)); 
    System.out.print("Enter student's name: "); 
    name[i] = gradeInput.next(); 
    System.out.print("Enter midterm 1 score: "); 
    midTerm1[i] = gradeInput.nextInt(); 
    System.out.print("Enter midterm 2 score: "); 
    midTerm2[i] = gradeInput.nextInt(); 
    System.out.print("Enter final score:  "); 
    avg[i] = gradeInput.nextInt(); 
} 

一次,這樣做是做如下

System.out.printf("%-8s %6s %8s %8s\n", "name", "final", "midterm", "average"); 
for(int i = 0; i < 7; i++) { 
    System.out.printf("%-8s %6c %8d %2.6f\n", name[i] , function(avg[i]), function(md1[i], md2[i]), function(md1[i], md2[i], avg[i])); 
} 

function (int grade)刪除所有print語句。

單面筆記也可以不硬編碼學生人數的值 - 在你的案例7並從命令行傳遞它。在這種情況下,您可以使用該數字來初始化所有四個數組大小並適當地修改for循環。

例如 - 內main,你可以這樣做

public static void main(String[] args) { 
    int noOfStudents; 
    if (args.length == 1) { 
     noOfStudents = Integer.parseInt(args[0]); 
     name = new String[noOfStudents]; 
     midTerm1 = new int[noOfStudents]; 
     midTerm2 = new int[noOfStudents]; 
     avg = new int[noOfStudents]; 
    } else { 
     System.out.println("please provide a value for no of students data that need to be processed"); 
     return; 
    } 
} 
相關問題