如何解决我可以在 for 循环中运行 while 循环吗? [Java] 如果是这样,在这种情况下我会怎么做?
我正在使用循环做一些简单的数学运算,我的任务如下:
“给定一个整数 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,请告诉我如何做到这一点。我真的很困惑这个。谢谢。
解决方法
我真的没有看到问题,你根本不需要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();
}
,
据我所知,您想显示一个数字的复数表,显示给定数字的 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 循环。你需要做的是把你的问题说得更清楚,以获得更好的社区帮助
,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();
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。