2

我怎样才能把它打印在桌子上?我写它的方式我无法弄清楚如何在最后打印所有结果。这就是我所拥有的:

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

4 回答 4

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);
   }
  }
于 2013-10-23T06:00:08.473 回答
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();
}
于 2013-10-23T05:50:22.023 回答
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());
        }
    

完成实施

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

    }
}
于 2013-10-23T05:51:40.303 回答
1

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

于 2013-10-23T05:40:59.260 回答