我尝试使用Scanner
从主题详细信息中获取用户的多个数据。例如:用户输入数字3,然后用户必须输入主题名称,主题代码和主题费用3次,我希望将详细信息存储在一个数组中并最后显示。
修改 感谢@Ksap,代码运行正常但小问题是输入的第一个值未显示(null)仅显示最后一个输入。 截图:
到目前为止,这是主题类:public class Subject {
public String[] subjectName;
public String[] subjectCode;
public Double[] subjectFee;
//default constructor
Subject(){
}
Subject(String[] subjectCode, String[] subjectName, Double[] subjectFee){
this.subjectCode = subjectCode;
this.subjectName = subjectName;
this.subjectFee = subjectFee;
}
//**********************************Setter Methods******************************\\
public void setSubjectCode(String[] subjectCode){
this.subjectCode=subjectCode;
}
public void setSubjectName(String[] subjectName){
this.subjectName=subjectName;
}
public void setSubjectFee(Double[] subjectFee){
this.subjectFee=subjectFee;
}
//**********************************Getter Methods******************************\\
public String[] getSubjectCode(){
return subjectCode;
}
public String[] getSubjectName(){
return subjectName;
}
public double[] getSubjectFee(){
return subjectFee;
}
}
和SubjectDriver:
public class SubjectDriver {
public static void main(String[] args) {
int subjectAmount;
Subject a = new Subject();
Scanner input = new Scanner(System.in);
System.out
.println("Please input the amount of subjects are you resposible for ?( Maximum 4)");
subjectAmount = Integer.parseInt(input.nextLine());
a.subjectName = new String[subjectAmount];
a.subjectCode = new String[subjectAmount];
a.subjectFee = new Double[subjectAmount];
for (int index = 0; index < subjectAmount; index++) {
System.out.println("enter subject name: ");
a.subjectName[index] = input.nextLine();;
System.out.println("enter subject code: ");
a.subjectCode[index] = input.nextLine();
System.out.println("enter subject fee: ");
a.subjectFee[index] = Double.parseDouble(input.nextLine());
}
System.out.println("Subject name: " + Arrays.asList(a.getSubjectName())
+ "\nSubject Code: " + Arrays.asList(a.getSubjectCode()) + "\nSubject fee: "
+ Arrays.asList(a.getSubjectFee()));
}
}
你的帮助会非常明显
答案 0 :(得分:0)
我假设您因为未初始化的数组而得到Nullpointerexception
,要解决这个问题,请将这些行添加到for loop
a.subjectName = new String[subjectAmount];
a.subjectCode = new String[subjectAmount];
a.subjectFee = new double[subjectAmount];
看起来Scanner
因为nextInt
和nextLine
的混合使用而无法按预期工作,以解决此问题
将subjectAmount=input.nextInt();
和subjectAmount = Integer.parseInt(input.nextLine())
替换为a.subjectFee[index]=input.nextDouble()
a.subjectFee[index] = Double.parseDouble(input.nextLine());
array
修改强>
打印array
时,它将打印Arrays.asList(a.getSubjectName())
的引用,如果我们要查看数组的内容,请将其转换为List并将其打印为这样
print
所以System.out.println("Subject name: " + Arrays.asList(a.getSubjectName())
+ "\nSubject Code: " + Arrays.asList(a.getSubjectCode()) + "\nSubject fee: "
+ Arrays.asList(a.getSubjectFee()));
语句看起来像这样
subjectFee
为了您的信息,primitive double
是Double
数组,因此它仍会打印引用,以查看值,将其声明为public class SubjectDriver {
public static void main(String[] args) {
int subjectAmount;
Subject a = new Subject();
Scanner input = new Scanner(System.in);
System.out
.println("Please input the amount of subjects are you resposible for ?( Maximum 4)");
subjectAmount = Integer.parseInt(input.nextLine());
a.subjectName = new String[subjectAmount];
a.subjectCode = new String[subjectAmount];
a.subjectFee = new Double[subjectAmount];
for (int index = 0; index < subjectAmount; index++) {
System.out.println("enter subject name: ");
a.subjectName[index] = input.nextLine();;
System.out.println("enter subject code: ");
a.subjectCode[index] = input.nextLine();
System.out.println("enter subject fee: ");
a.subjectFee[index] = Double.parseDouble(input.nextLine());
}
System.out.println("Subject name: " + Arrays.asList(a.getSubjectName())
+ "\nSubject Code: " + Arrays.asList(a.getSubjectCode()) + "\nSubject fee: "
+ Arrays.asList(a.getSubjectFee()));
}
}
数组。
$('img').on('click', function() {
var num = $(this).parent().attr('class').match(/\d+/)[0];
window.num = num;
$('.popup').fadeIn();
});
//Click li to update the brand and name on the page
$('.popup li').on('click', function() {
//Check the name and split it
var nameComplete = $(this).attr('name');
var Array = nameComplete.split(" ");
//Check the first word and identify it as brand, also update on the page
var brand = Array[0];
$('.num-' + window.num + ' .brand').text(brand);
//Check the rest of the array for the name and update it on the page
var name = '';
for(var i=1; i<Array.length ;i++) {
name = name + Array[i] + ' ';
}
name = $.trim(name);
$('.num-' + num + ' .name').text(name);
$('.popup').fadeOut();
});