我正在编写一个简短的 Java 程序,它应该接收 5 个整数的列表,并将每个整数分配给 6 个成员之一。我想最大化在他们的价值min
和价值之间获得价值的成员数量max
。
无论如何,出于某种我不知道的原因,我的私人member
课程似乎正在抛出 NullPointerException,但我完全不知道为什么。任何建议,将不胜感激。
import java.util.*;
public class assign {
private class member {
private String name;
private int min;
private int max;
public member(String name, int min, int max) {
this.name = name;
this.min = min;
this.max = max;
}
}
public member[] members = {
new member("Phil", 1, 20),
new member("Molly", 1, 20),
new member("Connor", 21, 40),
new member("Sam", 21, 40),
new member("Dan", 41, 60),
new member("Theresa", 41, 60)
};
public void main(String[] args) {
System.out.println("test");
String s = StdIn.readLine();
String[] temp = s.split(" ");
ArrayList<Integer> nums = new ArrayList<Integer>(5);
for (int i = 0; i < 5; i++) {
nums.add(Integer.parseInt(temp[i]));
}
ArrayList<member> mems = new ArrayList<member>(Arrays.asList(members));
for (int n : nums) {
for (member m : mems) {
if (n > m.min && n < m.max) {
System.out.println(m.name + " : " + n);
nums.remove(n);
mems.remove(m);
}
}
}
for (int n : nums) {
member m = mems.remove(0);
System.out.println(m.name + " : " + n);
}
}
}
从OP的评论:
java.lang.NullPointerException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)