将 and 运算符放入 for 循环中

发布于 2024-11-19 12:42:18 字数 315 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

童话 2024-11-26 12:42:18

查看您的代码,您似乎需要这样:

public void displayData(String [] names, int []scores, char [] grades) {
    for (int i = 0; i < names.length; i++) {
        System.out.println(names[i] + "\t");
        System.out.print(grades[i]);
    }
}

Looking at your code it looks like you need this:

public void displayData(String [] names, int []scores, char [] grades) {
    for (int i = 0; i < names.length; i++) {
        System.out.println(names[i] + "\t");
        System.out.print(grades[i]);
    }
}
浅笑轻吟梦一曲 2024-11-26 12:42:18

假设所有数组都是并行的(长度相同,索引 i 处的 Grades 数组中的值是索引 i 处的 Names 数组中学生的成绩),您可能可以将循环重写为:

for (int i = 0; i < grades.length; ++i) {
  System.out.println(names[i] + "\t" + grades[i]);
}

另外,尽量避免一个接一个的多个 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:

for (int i = 0; i < grades.length; ++i) {
  System.out.println(names[i] + "\t" + grades[i]);
}

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.

何以笙箫默 2024-11-26 12:42:18

您不能使用新式的 for 循环来并行迭代两个数组/集合。您需要使用带有显式索引变量的旧式 for 循环。例如:

public void displayData(String [] names, int []scores, char [] grades) {
    for (int i = 0; i < names.length; i++) {
        System.out.println(names[i] + "\t");
        System.out.print(grades[i]);
    }
}

You can't use the new-style for loop to iterate over two arrays / collections in parallel. You need to use an old-style for loop with an explicit index variable. For example:

public void displayData(String [] names, int []scores, char [] grades) {
    for (int i = 0; i < names.length; i++) {
        System.out.println(names[i] + "\t");
        System.out.print(grades[i]);
    }
}
翻了热茶 2024-11-26 12:42:18

试试这个:

for (int i = 0; i < names.length && i < grades.length; i++){
    System.out.println(names[i]);
    System.out.println(grades[i]);
}

编辑:它可能有助于澄清用户的要求:如何让他的代码工作,或者如何将操作数放入 for 循环中。对于后者,其目的是有争议的,但我已经看到它用于循环不同长度的数组。你可能会质疑这种练习,但这就是它可以用来做的事情。对于前者,他可能应该使用传统的 for 循环,如下所示:

for (int i = 0; i < names.length; i++){
    System.out.println(names[i]);
    System.out.println(grades[i]);
}

Try this:

for (int i = 0; i < names.length && i < grades.length; i++){
    System.out.println(names[i]);
    System.out.println(grades[i]);
}

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:

for (int i = 0; i < names.length; i++){
    System.out.println(names[i]);
    System.out.println(grades[i]);
}
你对谁都笑 2024-11-26 12:42:18

应该是

for (String name : names) {
    System.out.println(name);
    for (char grade : grades)
        System.out.println("\t"+grade);
}

It should be

for (String name : names) {
    System.out.println(name);
    for (char grade : grades)
        System.out.println("\t"+grade);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文