提示用户输入行数、列数,打印蛇形数字。如下所示 1 2 3 4 8 7 6 5 9 10 11 12 ...
答案:4 悬赏:0
解决时间 2021-03-02 13:54
- 提问者网友:前事回音
- 2021-03-02 05:38
提示用户输入行数、列数,打印蛇形数字。如下所示 1 2 3 4 8 7 6 5 9 10 11 12 ...
最佳答案
- 二级知识专家网友:野心和家
- 2021-03-02 06:29
刚编好的 完全符合你的要求 你看看可以不
import java.util.*;
public class Test {
public static void main(String[] args) {
System.out.println("输入列数");
int col = new Scanner(System.in).nextInt();
System.out.println("输入行数");
int row = new Scanner(System.in).nextInt();
for(int i=1; i<=row; i++) {
for(int j=1; j<=col; j++) {
if(i%2 == 1)
System.out.print(j + col * (i-1) +" ");
else
System.out.print(col - (j-1) + col * (i-1) +" ");
}
System.out.println("");
}
}
}
import java.util.*;
public class Test {
public static void main(String[] args) {
System.out.println("输入列数");
int col = new Scanner(System.in).nextInt();
System.out.println("输入行数");
int row = new Scanner(System.in).nextInt();
for(int i=1; i<=row; i++) {
for(int j=1; j<=col; j++) {
if(i%2 == 1)
System.out.print(j + col * (i-1) +" ");
else
System.out.print(col - (j-1) + col * (i-1) +" ");
}
System.out.println("");
}
}
}
全部回答
- 1楼网友:走,耍流氓去
- 2021-03-02 09:53
import java.util.Scanner;
public class SnakePrint {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Please input the row to genereate: ");
int row = scanner.nextInt();
System.out.print("Please input the column to generate: ");
int col = scanner.nextInt();
int number = row * col;
int i = 1;
boolean isRight = true;
while(i <= number){
if(isRight){
for(int j = 0; j < col; j++){
System.out.print(i++ + " ");
}
}else{
StringBuffer sb = new StringBuffer();
for(int j = col; j > 0; j--){
sb.insert(0, " "+i++);
}
System.out.print(sb.toString().trim());
}
System.out.println();
isRight = !isRight;
}
}
}
----------------testing
Please input the row to genereate: 3
Please input the column to generate: 4
1 2 3 4
8 7 6 5
9 10 11 12
- 2楼网友:請叫我丶偏執狂
- 2021-03-02 08:39
int i=0,j=1,n,t,m=1;
scanf("%d",&n);
t=n*n;
while(m<=t)
{if(j%2)
for(i=0;i
- 3楼网友:狙击你的心
- 2021-03-02 08:09
#include
main()
{
int i,n;
scanf("%d",&n);
for(i=1;i<=n;i++)
printf("%d\n",i);
}
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯
• 手机登qq时,显示手机磁盘不足,清理后重新登 |
• 刺客的套装怎么选啊? |