从循环创建表(java)

时间:2013-10-23 05:31:25

标签: java loops

如何在表格中打印?我写它的方式我无法弄清楚如何在最后打印所有结果。这就是我所拥有的:

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

4 个答案:

答案 0 :(得分:1)

创建某种数据容器类,让我们调用它Student,用getter和settors放入aproopriate字段(名称,权重等)或声明那些字段public。在每个循环传递中创建new Student,并将已初始化的Student对象添加到公共集合,例如List,例如LinkedList<Student> students。迭代完成后,通过例如迭代创建的列表。 for(Student s:students)并打印您想要的数据。

答案 1 :(得分:1)

我认为这个练习可以教你2D阵列

您需要将值保存到2D数组

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

答案 2 :(得分:1)

五步:

  1. 创建一个类来存储值

    静态班学生{         私有字符串名称;         私人双重开始;         私人双端;         私人双重变化;

        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());
        }
    
  6. 完成实施

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

答案 3 :(得分:1)

我建议您使用新课程:

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