gpt4 book ai didi

java - toString 不打印 JApplet 中的实际数组值

转载 作者:行者123 更新时间:2023-12-02 13:00:34 25 4
gpt4 key购买 nike

这是我的小程序。

JApplet

public void paint(Graphics g){
super.paint(g);

Font font = new Font("monospaced", Font.BOLD, 12);
g.setFont(font);

g.setColor(Color.YELLOW);
g.fillRect(0, 105, 500, 500);
g.setColor(Color.BLACK);

g.drawString("hello", 120, 300);

for(int c = 0; c < 1;){

for(int b = 250; c < 9; b = b + 10){
g.drawString(roster[c].toString(), 10, b);
c++;
}

}
g.drawString(roster[0].toString(), 0, 250);



}
public Student[] setup(){
count = 10;
roster = new Student[count];
int x = 0; int e = 0;
int[] gtest = new int[5];

for(e = 0; e < 5; e++){
gtest[e] = 0;
}


for(x = 0; x < 31; x++){
roster[x] = new Student("", 0, gtest);
}

int[] help = new int[5];
help[0] = 84; help[1] = 85; help[2] = 86; help[3] = 87; help[4] = 88;

roster[0] = new Student("Pocket, beam", 2017, new int[] {84, 85, 84, 87, 88, 88});
roster[1] = new Student("Zuckerberg, Marcus", 2017, help);
roster[2] = new Student("Timelapse, Random", 2013, help);
roster[3] = new Student("Timelapse, Random2", 2013, help);
roster[4] = new Student("Timelapse, Random3", 2016, help);
roster[5] = new Student("god, not", 2016, help);
roster[6] = new Student("Webster, Jacques", 2016, help);
roster[7] = new Student("East, Kanayo", 2016, help);
roster[8] = new Student("Blue, Canoe", 2017, help);
roster[9] = new Student("West, East", 2017, help);

return(roster);

}

我也粘贴了具体的代码,.toString在Graphics方法中。

这是构造函数类中我的 toString 方法。

public String toString(){
DecimalFormat tens = new DecimalFormat("0.00");
String ans = String.format("%-20s%-8s %-5s%-5s%-5s%-5s%-5s%-10s", name, year,
grades[0], grades[1], grades[2], grades[3], grades[4], tens.format(gpa));
return(ans);

}

所以我相信问题在于我如何调用.toString,我的谷歌搜索显示我应该使用java.util.Arrays?我已经尝试过了,但我不知道出了什么问题。我知道这是一个简单的语法错误,但我无法找出它是什么。

所发生的情况是,名册编号被打印到小程序上,但所有值在各自的格式中都是 0.00 或 0 或任何 0。

此外,我的 actionperformed 方法中还调用了 repaint()

最佳答案

setup函数中,摆脱这个:

for(x = 0; x < 31; x++){
roster[x] = new Student("", 0, gtest);
}

它导致 ArrayIndexOutOfBoundsException,因为 roster 只有 10 个元素。

然后,将文本绘画外观更改为如下所示:

// Ger rid of the outer loop on c
// for (int c = 0; c < 1) {
// Change your initial b value from 250 to 115. 250 is too low for your window
for(int b = 115, c = 0; c < 9; b = b + 10){
g.drawString(roster[c].toString(), 10, b);
c++;
}
// }

关于java - toString 不打印 JApplet 中的实际数组值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44319737/

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