输入是程序应打印的元素数
input is number of elements the program should print
我想编写一个程序,打印在一行中用数字 space 分隔的整数序列。程序的输入是一个称为 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);
}
}
}
}
是的,我不知道,这就是我寻求帮助的原因 ;)
我不确定我是否理解你,但是下面的代码
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
因为我假设您的示例行为:
For example, if n = 7, then the program should output 1 2 2 3 3 3 4.
我相信这可以重写为更高效的代码(但对您来说是更脏的代码)。
这里有评论的方式
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
}
试试这个,我用了 2 个 for 循环
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);
}
}
}
输入:5
输出:1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
我想编写一个程序,打印在一行中用数字 space 分隔的整数序列。程序的输入是一个称为 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);
}
}
}
}
是的,我不知道,这就是我寻求帮助的原因 ;)
我不确定我是否理解你,但是下面的代码
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
因为我假设您的示例行为:
For example, if n = 7, then the program should output 1 2 2 3 3 3 4.
我相信这可以重写为更高效的代码(但对您来说是更脏的代码)。
这里有评论的方式
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
}
试试这个,我用了 2 个 for 循环
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);
}
}
}
输入:5 输出:1 2 2 3 3 3 4 4 4 4 5 5 5 5 5