gpt4 book ai didi

java - 数组无法正确显示

转载 作者:行者123 更新时间:2023-12-01 18:09:52 25 4
gpt4 key购买 nike

当我运行该程序时,它打印出索引中的数字,而不是打印出单词。该程序的想法是创建随机句子,有人知道我在这里做错了什么吗?

 public class TestShoutBox5 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
String Subjects[] = new String[]{"My Teacher","I","You"
};

Random RandomSubject=new Random();
int S = RandomSubject.nextInt(4);

String Objects[] = new String[]{"Homework", "Work","Quiz"
};

Random Randomobjects=new Random();
int O = Randomobjects.nextInt(4);

String Verbs[] = new String[]{"Studying", "Playing","Working"
};

Random Randomverbs=new Random();
int R = Randomverbs.nextInt(4);

String Adjectives[] = new String[]{"Smart", "Tall","Hard"
};

Random RandomAdjectives=new Random();
int A = RandomAdjectives.nextInt(4);

String Adverbs[] = new String[]{"quickly", "everywhere","fast"
};

Random RandomAdverb=new Random();
int Ad = RandomAdverb.nextInt(4);



System.out.print("Your Sentence"+S+O+R+A+Ad);

}

}

最佳答案

问题是您正在打印索引而不是数组中软管索引处的单词。而不是:

System.out.print("Your Sentence"+S+O+R+A+Ad);

这样做:

System.out.print("Your Sentence"+ Subjects[S]+Objects[O]+Verbs[R]+Adjectives[A]+Adverbs[Ad]);

编辑(来自@kyriacos_k的评论):

当数组索引从 0 到数组长度 - 1 时,Random 对象应该创建 0 - 2 之间的值,而不是 0 - 3 之间的值。要解决此问题,请使用 .nextInt(3) 而不是 .nextInt(4)

关于java - 数组无法正确显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33850596/

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