gpt4 book ai didi

java - Joptionpane 未正确输出存储的值。

转载 作者:行者123 更新时间:2023-11-29 05:38:10 25 4
gpt4 key购买 nike

下面是我遇到问题的代码,它可以正常编译并运行,但是当我使用选项 1)init 在 joptionpane 框中输入信息并尝试使用选项 2) listinformation 显示它时,它会弹出错误“没有学生进入”。

我想要它做的是用 jOptionPane.showMessageDialog 列出所有学生、gpas 和需要

请帮忙。

import javax.swing.JOptionPane;
public class Test {
public static void main (String[] args) {

String[] firstname = new String[1000];
String[] lastname = new String[1000];
double[] gpa = new double[1000];
int[] award = new int[1000];
int awardsum = 0;
boolean[] need = new boolean[1000];
int numStudent = 0;
double classaverage;
int schtotal = 0;



String choice = "";
while (!choice.equals("4")) {
choice = getMenuChoice ();
if(choice.equals("1")){
init(firstname,lastname, gpa, need, numStudent);
}else if(choice.equals("2")){
listinformation(firstname, lastname, gpa, need, numStudent);
}else if(choice.equals("3")){
total(schtotal,award, numStudent);
}
}


}//main

public static int init (String[] firstname, String[] lastname, double[] gpa, boolean[] need, int count){
do {
firstname[count] = JOptionPane.showInputDialog("First name?");
lastname[count] = JOptionPane.showInputDialog("Last name?");
gpa[count] = Double.parseDouble(JOptionPane.showInputDialog("Gp a; Input as X.XX"));
need[count] = Boolean.parseBoolean(JOptionPane.showInputDialog(" Financial need? ;True or False"));
count++;
}while (JOptionPane.showConfirmDialog(null,"Add another student?")==JOptionPane.YES_OPTION);

return count;
}//init

public static int[] awardcal(int awardsum, int[] award, double[] gpa, boolean[] need, int count){
if (count > 0){
for (int index = 0; index < count; index++){
if(gpa[index] == 4.0){
awardsum = awardsum + 1000;
award[index] = award[index] + awardsum;

}
}
}return award;
}

public static int total(int schtotal,int[]award, int count){
for(int index = 0; index < count; index ++){
schtotal = schtotal + award[index];

}
JOptionPane.showMessageDialog(null, schtotal);
return schtotal;

}

public static void listinformation(String[] firstname, String[] lastname, double[] gpa, boolean[] need, int count){
String output = "";
if (count > 0){
for (int index = 0; index < count; index++) {
output += firstname[index] + " " + lastname[index] + " " + gpa[index] + " " + need[index] + "\n";
}
}else output = "No students entered";
JOptionPane.showMessageDialog(null, output);

}

public static String getMenuChoice () {

String menu = "1) Add Student Information\n2)View info for all students\n3)Total scholarships awarded\n4) Exit";
String inputValue = JOptionPane.showInputDialog(menu);
return inputValue;

} // getMenuChoice()



}//class

最佳答案

方法参数仅按值传递,因此您的 int numStudent 字段不会被 init 方法更改。相反,一个局部变量,count 参数只会改变,而 numStudent 不会受到影响。您需要在 init(...) 之后检查 numStudent 的值完成后,您会看到它仍为 0。

  • 一种解决方案是不将其作为参数传递,而是使用类字段(对于基于静态方法的程序,static 字段)并直接在 init 方法中推进。
  • 更好的解决方案可能是创建一个 Student 类,然后使用 ArrayList<Student>保存学生数据。那么你甚至不需要 numStudent 字段,而是可以直接检查 ArrayList 的 size() .

例如:

public class CounterTest {
private static int counter1 = 0;
private static int counter2 = 0;

public static void incrCounter1ViaParameter(int counter) {
counter++; // increments the **parameter**
}

public static void incrCounter2Directly() {
counter2++; // directly increments the **static class field**
}

public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
incrCounter1ViaParameter(counter1);
incrCounter2Directly();
}

System.out.println("Counter 1: " + counter1);
System.out.println("Counter 2: " + counter2);

}
}

关于java - Joptionpane 未正确输出存储的值。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18687998/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com