[英]Can I run a while loop inside a for loop? [Java] If so, how would I do it in this scenario?
Im doing some simple maths using loops and my task is as follows:我使用循环做一些简单的数学运算,我的任务如下:
"Given an integer,N , print its first 10 multiples. Each multiple N * i (where 1 <= N <= ) should be printed on a new line in the form: N xi = result." “给定一个整数 N ,打印它的前 10 个倍数。每个倍数 N * i (其中 1 <= N <= )应以以下形式打印在新行上:N xi = result。”
My input has to be :我的输入必须是:
N
My output:我的输出:
Print 10 lines of output; each line 'i' (where 1 <= N <= ) contains the result N * i of in the form: N x i = result.
Sample Input:样本输入:
2
Sample Output:示例输出:
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
This is what I have done so far:这是我到目前为止所做的:
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();
}
}
Would I be able to use a while loop to state the the integer, N, should be able to print out all the numbers in the inclusive range of 1 to 10 to the calculation of the (sample) input: 2x1=2.我是否能够使用while循环来声明整数N,应该能够打印出1到10的包含范围内的所有数字来计算(样本)输入:2x1 = 2。 2x2=4 etc.
2x2=4 等
If I can do it without the while loop ie just using the for loop or for-each, please advise on how I can do it.如果我可以在没有 while 循环的情况下做到这一点,即只使用 for 循环或 for-each,请告诉我如何做到这一点。 Im really confused with this.
我真的很困惑这个。 Thanks.
谢谢。
I really don't see the problem, you don't need a while loop at all, you just need simple maths:我真的没有看到问题,您根本不需要 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();
}
As i can understand you want to show the mult table of a number, showing multiples from 1 to 10 of a given number.据我所知,您想显示一个数字的多表,显示给定数字从 1 到 10 的倍数。 What i dont understand is why you need a while inside the for, as you have the base number (input) and a constant for loop (1 <= N <= 10).
我不明白的是为什么你需要在 for 里面一段时间,因为你有基数(输入)和一个常量 for 循环(1 <= N <= 10)。 If i understood well the question, this is the code you need to achive this:
如果我很好地理解了这个问题,那么这是实现此目的所需的代码:
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();
}
}
Reading your question the comment from @Toastrackenigma is the right answer, yes you can use nested while or for loops inside other loops.阅读您的问题@Toastrackenigma 的评论是正确的答案,是的,您可以在其他循环中使用嵌套的 while 或 for 循环。 What you need to do is to be more clear in your questions to obtain better help from the community
你需要做的是把你的问题说得更清楚,以获得更好的社区帮助
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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.