英文:
input is number of elements the program should print
问题
以下是您要翻译的内容:
我想编写一个程序,以单行打印一系列以空格分隔的整数。程序的输入是一个称为 n 的正整数,这是程序应该打印的序列元素数。
例如,如果 n = 7,则程序应输出 1 2 2 3 3 3 4。
这是我的代码:
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n;
n = scanner.nextInt();
while (n == 0) {
n = scanner.nextInt();
for (int i = n; i <= n; i++) {
System.out.println(i * n);
}
}
}
}
是的,我一点也不清楚,所以才寻求帮助 😉
英文:
I would like to write a program that prints a sequence of integer numbers written in a single line with the numbers space-separated. The input of the program is a positive integer called n, this is the number of the elements of the sequence the program should print.
For example, if n = 7, then the program should output 1 2 2 3 3 3 4.
THIS IS MY CODE:
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n;
n = scanner.nextInt();
while (n == 0) {
n = scanner.nextInt();
for (int i = n; i <= n; i++) {
System.out.println(i * n);
}
}
}
}
yeah I have no clue, that's why I am asking for help
答案1
得分: 0
我不确定是否理解您的意思,但以下是代码部分:
import java.io.IOException;
import java.util.Scanner;
public class Main {
public static void main(final String[] args) throws IOException {
Scanner scanner = new Scanner(System.in);
int n;
while ((n = scanner.nextInt()) > 0) {
int count = 0;
for (int i = 1; i < n && count < n; i++) {
for (int j = 1; j <= i && count < n; j++) {
System.out.print(i + " ");
count++;
}
}
System.out.println();
}
}
}
应该打印出如下内容:
7
1 2 2 3 3 3 4
8
1 2 2 3 3 3 4 4
因为我根据您的示例假设了程序的行为:
例如,如果 n = 7,则程序应输出 1 2 2 3 3 3 4。
我认为这段代码可以重写为更高效的代码(但对您来说可能会更难读)。
英文:
I am not sure if I understand you, but following code
import java.io.IOException;
import java.util.Scanner;
public class Main {
public static void main(final String[] args) throws IOException {
Scanner scanner = new Scanner(System.in);
int n;
while ((n = scanner.nextInt()) > 0) {
int count = 0;
for (int i = 1; i < n && count < n; i++) {
for (int j = 1; j <= i && count < n; j++) {
System.out.print(i + " ");
count++;
}
}
System.out.println();
}
}
}
should print this:
7
1 2 2 3 3 3 4
8
1 2 2 3 3 3 4 4
Because I am assuming behavior from your example:
> For example, if n = 7, then the program should output 1 2 2 3 3 3 4.
I believe this could be rewritten to more efficient code (but more dirty code for you).
答案2
得分: 0
以下是带有注释的代码部分的翻译:
n = scanner.nextInt();
int viewNumber = 1; // 用于打印数字
while (n > 0) { // 如果还有要打印的数字
for (int i = 1; i <= viewNumber; i++) { // 这个循环打印 viewNumber 这个数字 viewNumber 次
System.out.print(viewNumber + " ");
n--; // 打印一个数字后减少 n 的值
if (n == 0) { // 如果已经打印了 n 个数字,就退出循环
break;
}
}
viewNumber++; // 显示的数字次数增加
}
英文:
Here is a way with comments
n = scanner.nextInt();
int viewNumber = 1;// for printing number
while (n > 0) { // if any number printing left
for (int i = 1; i <= viewNumber; i++) { // This loop print viewNumber viewNumber's times
System.out.print(viewNumber + " ");
n--; // decrease n value after printing a number
if(n == 0) { // break the loop if print n numbers already
break;
}
}
viewNumber++; // increase view number after showing it's that times
}
答案3
得分: 0
请注意,我只会返回翻译好的代码部分,如下所示:
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入任何数字");
int n;
n = scanner.nextInt();
for (int a = 0; n >= a; a++) {
for (int i = 0; a > i; i++) {
System.out.print(" " + a);
}
}
}
输入: 5
输出: 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
英文:
Try This, I have used 2 for loops
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter any digit");
int n;
n = scanner.nextInt();
for(int a = 0;n>=a;a++) {
for (int i = 0; a > i; i++) {
System.out.print(" "+a);
}
}
}
Input: 5
Output: 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论