【问题标题】:Can I run a while loop inside a for loop? [Java] If so, how would I do it in this scenario?我可以在 for 循环中运行 while 循环吗? [Java] 如果是这样,在这种情况下我会怎么做?
【发布时间】:2021-07-06 17:22:49
【问题描述】:

我正在使用循环做一些简单的数学运算,我的任务如下:

“给定一个整数 N ,打印它的前 10 个倍数。每个倍数 N * i (其中 1

我的输入必须是:

N

我的输出:

Print 10 lines of output; each line 'i' (where 1 <= N <= ) contains the result N * i of  in the form: N x i = result.

示例输入:

2

样本输出:

2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20

这是我到目前为止所做的:

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {



    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        int N = scanner.nextInt();
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
        
        for (int i = 0; i >= 1 && i <= 10; i++){
            
            
        }

        scanner.close();
    }
}

我能否使用 while 循环来说明整数 N,应该能够打印出 1 到 10 范围内的所有数字来计算(样本)输入:2x1=2 . 2x2=4 等等。

如果我可以在没有 while 循环的情况下做到这一点,即只使用 for 循环或 for-each,请告诉我如何做到这一点。我真的很困惑。谢谢。

【问题讨论】:

  • "我可以在 for 循环中运行 while 循环吗?"是的。您可以拥有任意数量的嵌套控制结构和循环。
  • 所以不需要使用'break'或'continue'来停止循环然后继续另一个while或for循环?
  • 不,你可以在另一个循环中拥有任意数量的循环
  • 是的,我相信 C 有 128 个嵌套事物的限制,但在 Java 中只有内存限制,实际上你永远不会,永远 想要嵌套那么深。同样,您可以在嵌套循环中使用breakcontinue,但您没有必须使用。值得注意的是,它们只会中断或继续它们直接所在的循环,而不是外部循环。
  • 我明白了,现在这更有意义了。谢谢你的澄清

标签: java for-loop foreach while-loop


【解决方案1】:

我真的不明白问题所在,你根本不需要 while 循环,你只需要简单的数学:

public static void main(String[] args) {
    int N = scanner.nextInt();
    scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
        
    // I removed the i >= 1 because it hinders the for loop from even staring,
    // you had i = 0 before, so the i >= 1 would output false. So the for loop would stop
    // instantly. You can just start the loop with the number 1 so you dont multiply by zero.
    for (int i = 1; i <= 10; i++) {
        System.out.println(N + " x " + i + " = " + (N * i));
        /*
        lets say N is 2, then it will output the following:
        2 x 1 = 2
        2 x 2 = 4
        2 x 3 = 6
        2 x 4 = 8
        2 x 5 = 10
        2 x 6 = 12
        2 x 7 = 14
        2 x 8 = 16
        2 x 9 = 18
        2 x 10 = 20
        Just as you want it to.
        */
    }
    
    scanner.close();
}

【讨论】:

  • 是的,其他人也一直在说不需要while循环。感谢您的深入解释。
【解决方案2】:

据我所知,您想显示一个数字的 mult 表,显示给定数字的 1 到 10 的倍数。我不明白为什么你需要在 for 中使用一段时间,因为你有基数(输入)和一个常量 for 循环(1

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {



    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        int n = scanner.nextInt();
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
    
        for (int i = 1; i < 11; i++){
            System.out.println(String.format("%d x %d = %d", n, i, n*i);
        
        }

        scanner.close();
    }
}

阅读您的问题,@Toastrackenigma 的评论是正确答案,是的,您可以在其他循环中使用嵌套的 while 或 for 循环。你需要做的是在你的问题中更清楚,以便从社区获得更好的帮助

【讨论】:

  • 谢谢你,一开始我很困惑是否应该,我也尝试过,但给了我一个错误。
  • 顺便说一句,你得到了一个编译错误:变量'n'没有定义,它实际上是大写的N,但我知道你来自哪里,你的答案是正确的
  • 编译错误是因为我把你的 N 改成了 n 因为它是一个变量而不是一个常量,并且由于命名约定,变量属性可能用驼峰式小写名称和常量命名(变量标记为最终的)具有完整的大写名称。如果您复制/粘贴我发布的代码,则没有编译错误。只有你错过了那个变化:)
  • 是的,我是在建议哈哈。遇到问题的时候我也做了和你一样的事情:)
【解决方案3】:
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;



public class Solution {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));

        int n = Integer.parseInt(bufferedReader.readLine().trim());
        
        for (int i=1; i<=10; i++){
            System.out.println(n + " x " + i + " = " + n*i);
        }

        bufferedReader.close();
    }
}

【讨论】:

    猜你喜欢
    • 2021-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多