gpt4 book ai didi

java - 数组和Java字符串错误 : [Ljava. lang.String;@19c42c4b

转载 作者:行者123 更新时间:2023-11-29 09:37:50 25 4
gpt4 key购买 nike

我创建了一个程序,允许用户输入日记条目(最多 7 天),然后允许用户在输入条目后调用其中一天。不幸的是,这给我留下了一些我不熟悉的奇怪的字符串错误。

代码如下:

public class eDiary{
public static void main (String args[]){
int[] days = new int[7];//get our days
days[0] = 1;//start with 1 and not 0
days[1] = 2;
days[2] = 3;
days[3] = 4;
days[4] = 5;
days[5] = 6;
days[6] = 7;
String [] events = new String[7];//events for the days
int i = 0;

//asks for input and counts
for(i=0; i<7; i++){
String event = Console.readString("Tell me the major event of day " + days[i] + "\n");
events[i] = event;

}

int journal_entry = Console.readInt("Enter what day you want to hear or Enter 0 to stop \n");
while (journal_entry != 0) {
System.out.println(events);
journal_entry = Console.readInt("Enter what day you want to hear or Enter 0 to stop \n");
//get r dun!

输入输出:

Tell me the major event of day 1
one
Tell me the major event of day 2
two
Tell me the major event of day 3
thre
Tell me the major event of day 4
four
Tell me the major event of day 5
five
Tell me the major event of day 6
six
Tell me the major event of day 7
seven
Enter what day you want to hear or Enter 0 to stop
1
[Ljava.lang.String;@10181f5b
Enter what day you want to hear or Enter 0 to stop
0

大家好!

非常感谢您的快速回复。它现在似乎正在做的一件事是更换

System.out.println(events);

System.out.println(events[journal_entry]);

现在给我这样的输入:

Tell me the major event of day 1 
first day
Tell me the major event of day 2
second day
Tell me the major event of day 3
third day
Tell me the major event of day 4
fourth day
Tell me the major event of day 5
fifth day
Tell me the major event of day 6
sixth day
Tell me the major event of day 7
seventh day
Enter what day you want to hear or Enter 0 to stop
1//the day im asking for
second day//spitting out the next day's entry instead of the first day's entry
Enter what day you want to hear or Enter 0 to stop
0//this is me stopping it

最佳答案

这不是错误。

System.out.println(events);

在这一行中,您尝试打印数组,但是该语句不打印数组内容,它只打印对象类名及其哈希码。

要打印你必须使用的数组内容

System.out.println(Arrays.toString(events));

或者,如果你愿意,遍历数组并打印它的值

关于java - 数组和Java字符串错误 : [Ljava. lang.String;@19c42c4b,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19477869/

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