【问题标题】:Printing on same line在同一行打印
【发布时间】:2018-03-20 14:24:52
【问题描述】:

我是 Java 新手,我正在尝试在 1 行上打印学生编号和编号(在本例中为 cijfer)。但由于某种原因,我得到了奇怪的迹象等。另外,当我尝试其他东西时,我得到一个非静态上下文错误。这是什么意思,具体是如何工作的?

下面是我的代码:

import java.text.DecimalFormat;
import java.util.Arrays;


public class Student {
public static final int AANTAL_STUDENTEN = 50;
public  int[] studentNummer = new int[AANTAL_STUDENTEN];
public String[] cijfer;


public int[] StudentNummers() {

    for (int i = 0; i < AANTAL_STUDENTEN; i++) {
        studentNummer[i] = (50060001 + i);

    }
    return studentNummer;
}

 public  String[] cijfers(){

    for (int i = 0; i < AANTAL_STUDENTEN; i++) {
        DecimalFormat df = new DecimalFormat("#.#");

        String cijferformat = df.format(Math.random() * ( 10 - 1 ) + 1);
        cijfer[i++] = cijferformat;
    }
    return cijfer;
}

public static void main(String[] Args) {
    System.out.println("I cant call the cijfer and studentnummer.");
}
}

我也知道我的 cijfer 数组给出了一个空指针异常。我仍然需要解决这个问题。

【问题讨论】:

标签: java


【解决方案1】:

只需将所有方法和类变量设为static。然后您可以通过main 方法访问它们。此外,您的代码中有一些错误:

public class Student {
    public static final int AANTAL_STUDENTEN = 50;
    // NOTE: static variables can be moved to local ones

    // NOTE: only static method are available from static context
    public static int[] StudentNummers() {
        int[] studentNummer = new int[AANTAL_STUDENTEN];

        for (int i = 0; i < AANTAL_STUDENTEN; i++)
            studentNummer[i] = 50060001 + i;

        return studentNummer;
    }

    // NOTE: only static method are available from static context
    public static String[] cijfers() {
        // NOTE: it is better to use same `df` instance
        DecimalFormat df = new DecimalFormat("#.#");
        String[] cijfer = new String[AANTAL_STUDENTEN];

        for (int i = 0; i < AANTAL_STUDENTEN; i++)
            // NOTE: remove `i++`, because we have it in the loop
            cijfer[i] = df.format(Math.random() * (10 - 1) + 1);

        return cijfer;
    }

    // NOTE: this is `static` method, therefore it has access only to static methods and variables
    public static void main(String[] Args) {
        String[] cijfer = cijfers();
        int[] studentNummer = StudentNummers();

        // TODO you can pring two arrays one element per line
        for(int i = 0; i < AANTAL_STUDENTEN; i++)
            Sytem.out.println(cijfer[i] + '-' + studentNummer[i]);

       // TODO as alternative, you can print whole array
       System.out.println(Arrays.toString(cijfer));
       System.out.println(Arrays.toString(studentNummer));
    }
}

【讨论】:

    【解决方案2】:

    除了我在 cmets 中提到的内容之外,您的设计还需要工作。您有一个 class Student,其中包含 50 个 studentNummercijfer 成员。据推测,Student 将只有一个studentNummer 和一个cijfer。您需要 2 个类:1 个用于单个 Student,另一个用于保存所有 Student 对象(例如 StudentBody)。

    public class StudentBody {
        // An inner class (doesn't have to be)
        public class Student {
            // Just one of these
            public  int studentNummer;
            public String cijfer;
    
            // A constructor. Pass the student #
            public Student(int id) {
                studentNummer = id;
                DecimalFormat df = new DecimalFormat("#.#");
                cijfer = df.format(Math.random() * ( 10 - 1 ) + 1);
            }
    
            // Override toString
            @Override
            public String toString() {
                return studentNummer + " " + cijfer;
            }
        }
    
        public static final int AANTAL_STUDENTEN = 50;
        public Student students[] = new Student[AANTAL_STUDENTEN];
    
        // StudentBody constructor
        public StudentBody() {
            // Create all Students
            for (int i = 0; i < AANTAL_STUDENTEN; i++) {
                students[i] = new Student(50060001 + i);
            }
        }
    
        // Function to print all Students
        public void printStudents(){
            for (int i = 0; i < AANTAL_STUDENTEN; i++) {
                System.out.println(students[i]);
           }
        }
    
        public static void main(String[] Args) {
            // Create a StudentBody object
            StudentBody allStudents = new StudentBody();
            // Print
            allStudents.printStudents();
        }
    }
    

    【讨论】:

      【解决方案3】:

      你可以循环System.out.print。否则,使您的函数静态以从 main 访问它们。同时初始化你的 cijfer 数组。

      【讨论】:

        【解决方案4】:

        我不是java开发者,但尝试

        System.out.print
        

        【讨论】:

          猜你喜欢
          • 2011-11-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多