Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 尝试使用循环显示从1开始的数字序列_Java_Loops_For Loop_Input - Fatal编程技术网

Java 尝试使用循环显示从1开始的数字序列

Java 尝试使用循环显示从1开始的数字序列,java,loops,for-loop,input,Java,Loops,For Loop,Input,我试图编写一个程序,从用户那里接受一个从5到9的整数。如果用户输入6,则用户的输出将为: 一, 1 2 1 2 3 1234 123445 123456 654321 65432 65433 654 6.5 六, 我尝试使用如下循环: for(int i = 0; i < no; i++) { for(int j = 0; j < no; j++) { System.out.print(no); } System.out.print

我试图编写一个程序,从用户那里接受一个从5到9的整数。如果用户输入6,则用户的输出将为:

一,

1 2

1 2 3

1234

123445

123456

654321

65432

65433

654

6.5

六,

我尝试使用如下循环:

for(int i = 0; i < no; i++) 
{
    for(int j = 0; j < no; j++) 
    {
        System.out.print(no);
    }
    System.out.println();
}
但我得到的结果是:

666666

import java.util.*;
public class numberSequence
{
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter a number in the range 5 to 9: ");
        int no = Integer.parseInt(in.nextLine());
        if(no < 5 || no > 9)
            System.out.println("Please enter a number in the range 5 to 9.");
        else
        {
            // continuing from here
        }
    }
}
666666

import java.util.*;
public class numberSequence
{
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter a number in the range 5 to 9: ");
        int no = Integer.parseInt(in.nextLine());
        if(no < 5 || no > 9)
            System.out.println("Please enter a number in the range 5 to 9.");
        else
        {
            // continuing from here
        }
    }
}
666666

import java.util.*;
public class numberSequence
{
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter a number in the range 5 to 9: ");
        int no = Integer.parseInt(in.nextLine());
        if(no < 5 || no > 9)
            System.out.println("Please enter a number in the range 5 to 9.");
        else
        {
            // continuing from here
        }
    }
}
666666

import java.util.*;
public class numberSequence
{
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter a number in the range 5 to 9: ");
        int no = Integer.parseInt(in.nextLine());
        if(no < 5 || no > 9)
            System.out.println("Please enter a number in the range 5 to 9.");
        else
        {
            // continuing from here
        }
    }
}
666666

import java.util.*;
public class numberSequence
{
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter a number in the range 5 to 9: ");
        int no = Integer.parseInt(in.nextLine());
        if(no < 5 || no > 9)
            System.out.println("Please enter a number in the range 5 to 9.");
        else
        {
            // continuing from here
        }
    }
}
循环将是-

for(int i = 1; i <= no; i++){
    for(int j = 1; j <= i; j++) 
    {
        System.out.print(j+" ");
    }
    System.out.println();
}
for(int i = 1; i <= no; i++){
    for(int j = no; j >= i; j--) 
    {
        System.out.print(j+" ");
    }
    System.out.println();
}
输入-否=6

输出-

一,

1 2

1 2 3

1234

123445

123456

654321

65432

65433

654

6.5


6

对于循环,需要使用2,第一个用于增加到n值,第二个用于从n值减少到1

导入java.util.Scanner

    public class numberSequence
    {
        public static void main(String[] args)
        {
            Scanner in = new Scanner(System.in);
            System.out.print("Enter a number in the range 5 to 9: ");
            int no = Integer.parseInt(in.nextLine());
            if(no < 5 || no > 9)
                System.out.println("Please enter a number in the range 5 to 9.");
            else
            {
                for(int i = 1; i <=no; i++) 
                {
                    for(int j = 1; j <=i; j++) 
                    {
                        System.out.print(j);
                    }
                    System.out.println();
                }

                for(int i = no; i > 0; i--) 
                {
                    for(int j = i; j >=1; j--) 
                    {
                        System.out.print(j);
                    }
                    System.out.println();
                }
            }
        }
    }
你可以试试这个

for (int i = 1; i <= no; i++) {
     for (int j = 1; j <= i; j++) {
         System.out.print(j);
     }
     System.out.println("");
}
for (int i = no; i >= 1; i--) {
     for (int j = i; j >= 1; j--) {
         System.out.print(j);
     }
     System.out.println("");
}

你正在打印不,你应该打印j

程序打印期望输出为

公共类简单{

public static void main(String[] args) {
    int no= 6;
    for(int i = 1; i <= no; i++) 
    {
        for(int j = 1; j <= i; j++) 
        {
            System.out.print(j);
        }
        System.out.println();

    }

    for(int i = no; i >= 1; i--) 
    {
        for(int j = i; j >= 1; j--) 
        {
            System.out.print(j);
        }
        System.out.println();

    }

  }
}

好的,在编写代码之前你考虑过解决方案吗?j需要从0到i,而不是从0到no。而且你只打印出no,这是输入的数字。所以如果你输入6,你的输出都是6i。我打赌如果你调用变量“no”,比如“terminationValue”,你会很快发现问题。