将 and 运算符放入 for 循环中
public void displayData(String [] names, int []scores, char [] grades){
for (String name : names && char grade : grades)
System.out.println(name + "\t");
System.out.print(grade);
上面是我的代码。我需要在 for 循环中放置一个 and 运算符,但上面的代码不正确。请帮忙。如果需要,我会提供更多详细信息。
public void displayData(String [] names, int []scores, char [] grades){
for (String name : names && char grade : grades)
System.out.println(name + "\t");
System.out.print(grade);
Above is my code. I need to put an and operator in the for loop but the code above is not correct. Please help. I will give additional details if needed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
查看您的代码,您似乎需要这样:
Looking at your code it looks like you need this:
假设所有数组都是并行的(长度相同,索引 i 处的 Grades 数组中的值是索引 i 处的 Names 数组中学生的成绩),您可能可以将循环重写为:
另外,尽量避免一个接一个的多个 System.out.print 语句,因为每个语句都需要调用操作系统,这会稍微减慢您的应用程序的速度,因为整个程序当操作系统处理您的打印请求时,会停止很多时间 细绳。相反,请尽可能连接(用“
+
”连接在一起)字符串。Assuming all the arrays are parallel (all of the same length, with the value in the grades array at index i being the grade of the student in the names array at index i), you probably could just re-write the loop as:
Also, try to avoid multiple System.out.print statements one after another, as each requires a call to the operating system which slows your application down a bit, because the entire program much stop while the operating system handles your request to print the string. Concatenate (stick together with '
+
') strings where possible, instead.您不能使用新式的
for
循环来并行迭代两个数组/集合。您需要使用带有显式索引变量的旧式for
循环。例如:You can't use the new-style
for
loop to iterate over two arrays / collections in parallel. You need to use an old-stylefor
loop with an explicit index variable. For example:试试这个:
编辑:它可能有助于澄清用户的要求:如何让他的代码工作,或者如何将操作数放入 for 循环中。对于后者,其目的是有争议的,但我已经看到它用于循环不同长度的数组。你可能会质疑这种练习,但这就是它可以用来做的事情。对于前者,他可能应该使用传统的 for 循环,如下所示:
Try this:
Edit: It might help to clarify what the user is asking: how to get his code to work, or how to put an operand in a for loop. In the case of the latter, the purpose is arguable, but I have seen it used to loop through arrays of different lengths. Question that practice you may, but that's what one could use it for. In the case of the former, he probably should just use a traditional for loop like so:
应该是
It should be