2013-10-23 99 views
2

如何獲取此表以便在表中打印?我寫的方式我無法弄清楚如何在最後打印所有結果。這是我有:從循環創建表(java)

import java.util.Scanner; 
public class FFLAverage{ 
    public static void main(String[] args){ 
    Scanner input = new Scanner(System.in); 
    System.out.println("Input the number of students"); 
    int numofstu = input.nextInt(); 
    double totalchange = 0; 
    for (int count = numofstu; count != 0; count--){ 
     if (count == numofstu) 
     System.out.println("Last Name \t\t StartWeight \t\t End Weight \t\t Weight Change"); 
     System.out.print("Enter student name: "); 
     String name = input.next(); 
     System.out.print("Input start weight: "); 
     double start = input.nextDouble(); 
     System.out.print("Enter end weight: "); 
     double end = input.nextDouble(); 
     double change = (start - end); 
     totalchange += change; 
     System.out.println(name + "\t\t" + start + "\t\t\t" + end + "\t\t\t" + change); 
    } 
    double avg = totalchange/numofstu; 
    System.out.println("Average weight change: " + avg); 
    } 
} 

這是輸出應該是什麼樣子(控制檯)

Last Name Start Weight End Weight Weight Change 
Noble  222.5  202.4  -23.1 
Tyler  189.5  194.4  +4.9 
Pond  345.1  190.0  -155.1 
Average weight change: --57.33 
+0

得到什麼打印的表?你認爲什麼是結果 - 也許提供一些樣本輸出? – John3136

+0

如果你想要所有的名字等,你需要將它們存儲到一個集合中,而不僅僅是單個變量(這將在下一次通過循環時丟失) – John3136

回答

1

五步驟:

  1. 創建一個類來存儲值

    static class Student { private String name;私人雙人開始; 私人雙頭;私人雙重變更;

    public String getName() { 
         return name; 
        } 
    
        public void setName(String name) { 
         this.name = name; 
        } 
    
        public double getStart() { 
         return start; 
        } 
    
        public void setStart(double start) { 
         this.start = start; 
        } 
    
        public double getEnd() { 
         return end; 
        } 
    
        public void setEnd(double end) { 
         this.end = end; 
        } 
    
        public double getChange() { 
         return start - end; 
        } 
    
    } 
    
  2. 創建學生的每個輸入的對象。

    Student student = new Student(); 
    
  3. 存儲對象

     System.out.print("Enter student name[ "+(count+1) + "]: "); 
         student.setName(input.next()); 
         System.out.print("Input start weight[ "+(count+1) + "]: "); 
         student.setStart(input.nextDouble()); 
         System.out.print("Enter end weight[ "+(count+1)+ "]: "); 
         student.setEnd(input.nextDouble()); 
    
  4. 儲存於數組的對象

    students[count] = student; 
    
  5. 迭代陣列內的輸入,用於學生

    System.out 
          .println("Last Name\t\tStartWeight\t\tEnd Weight\t\tWeight Change"); 
        // PARSE THE ARRAY 
        for (int i = 0; i < students.length; i++) { 
         System.out.println(students[i].getName() + "\t\t" 
           + students[i].getStart() + "\t\t" + students[i].getEnd() 
           + "\t\t" + students[i].getChange()); 
        } 
    

完全實現

package com; 

import java.util.Scanner; 

public class FFLAverage { 
    public static void main(String[] args) { 
     Scanner input = new Scanner(System.in); 
     System.out.println("Input the number of students"); 
     int numofstu = input.nextInt(); 
     double totalchange = 0; 
     Student[] students = new Student[numofstu]; 
     for (int count = 0; count<numofstu;count++) { 
      //Put data into A CLASS 
      System.out.println("--------------------------------"); 
      Student student = new Student(); 
      System.out.print("Enter student name[ "+(count+1) + "]: "); 
      student.setName(input.next()); 
      System.out.print("Input start weight[ "+(count+1) + "]: "); 
      student.setStart(input.nextDouble()); 
      System.out.print("Enter end weight[ "+(count+1)+ "]: "); 
      student.setEnd(input.nextDouble()); 
      totalchange += student.getChange(); 
      // PUT THE OBJECT IN ARRAY 
      students[count] = student; 
      System.out.println("--------------------------------"); 
     } 

     System.out 
       .println("Last Name\t\tStartWeight\t\tEnd Weight\t\tWeight Change"); 
     // PARSE THE ARRAY 
     for (int i = 0; i < students.length; i++) { 
      System.out.println(students[i].getName() + "\t\t" 
        + students[i].getStart() + "\t\t\t" + students[i].getEnd() 
        + "\t\t\t" + students[i].getChange()); 
     } 

     double avg = totalchange/numofstu; 
     System.out.println("Average weight change: " + avg); 
    } 

    static class Student { 
     private String name; 
     private double start; 
     private double end; 
     private double change; 

     public String getName() { 
      return name; 
     } 

     public void setName(String name) { 
      this.name = name; 
     } 

     public double getStart() { 
      return start; 
     } 

     public void setStart(double start) { 
      this.start = start; 
     } 

     public double getEnd() { 
      return end; 
     } 

     public void setEnd(double end) { 
      this.end = end; 
     } 

     public double getChange() { 
      return start - end; 
     } 

    } 
} 
+1

請注意Shastri寫作'for '循環。 – andy256

1

建立某種形式的數據容器類的讓叫它Student,把aproopriate領域中它(名,重量等)與獲得者和定居者或宣佈這些領域public。在每個循環中創建new Student,並將初始化的對象添加到像List這樣的常見集合中,讓其說LinkedList<Student> students。迭代完成後,通過例如迭代創建的列表。 for(Student s:students)並打印您想要的數據。

1

我覺得這個運動的時候,你想打印它教你二維數組

您需要將您的值保存到一個二維數組

String[][] students = String[numofstu][4] 

// inputs 
// All of these inputs have to go inside a nested for loop using indexes i and j 
String name = input.nextLine(); 
students [i][j] = name; 

String beingWeight = String.valueOf(input.nextDouble()); 
students [i][j] = beginWeight; 

String endWeight = String.valueOf(input.nextDouble()); 
students [i][j] = endWeight; 

double wieghtChange = 
     Double.parseDouble(beginWeight)) - Double.parseDouble(endWeight)); 
String weightChange = String.valueOf(weightChange)); 
students [i][j] = weightChange; 

for (int i = 0; i < students.length; i++) { 
    for (int j = 0; j < 4; j++) { 
     // Print in your desired format 
     System.out.print(students[i][j] + "  "); 
    } 
    System.out.println(); 
} 
+0

+1對於Matrix .. – Shashi

2

我建議你使用一個新類:在你的主類

class student{ 
     public String name ; 
     public double startWeight ; 
     public double endWeight 
     public student(){ 
      name = new String() ; 
     } 

     public double change(){ 
      return (endWeight - startWeight) ; 
     } 
    } 

後:

import java.util.Scanner; 
    public class FFLAverage{ 
     public static void main(String[] args){ 
     Scanner input = new Scanner(System.in); 
     System.out.println("Input the number of students"); 
     int numofstu = input.nextInt(); 
     student[] std = new student[numofstu] ; 
     double totalchange = 0; 
     for (int count = numofstu; count != 0; count--){ 
      std[count] = new student() ; 


      System.out.print("Enter student name: "); 
      std[count].name = input.next(); 
      System.out.print("Input start weight: "); 
      std[count].startWeight = input.nextDouble(); 
      System.out.print("Enter end weight: "); 
      std[count].endWeight = input.nextDouble(); 
     } 
     for (int count = numofstu; count != 0; count--){ 
      if (count == numofstu) 
       System.out.println(name + "\t\t" + start + "\t\t\t" + end + "\t\t\t" + change); 
      System.out.println(std[count].name + "\t\t" + std[count].startWeight + "\t\t\t" + std[count].endWeight + "\t\t\t" + std[count].change()); 
      totalchange+=std[count].change() ; 
     } 
     double avg = totalchange/numofstu; 
     System.out.println("Average weight change: " + avg); 
    } 
    }