【问题标题】:Write a program to print the pattern for the given N number of rows编写一个程序来打印给定 N 行的模式
【发布时间】:2020-11-10 02:51:54
【问题描述】:

以下模式:

示例输入 2:

5

示例输出 2:

13579
35791
57913
79135
91357

我的java代码:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        int i = 1;

        while(i<=n) {
            int j = 1;
            int odd = 1;

            while(j <= n) {
                System.out.print(odd);
                odd = odd+2;
                j++;
            }

            System.out.println();
            i++;
        }
    }
}

【问题讨论】:

  • 好的,你已经陈述了你的期望并且你有你的代码,但是,呃,你似乎忘记了一个实际的问题。
  • 如果输入大于 5 会发生什么?您需要提供正确的输入/输出场景以及您的代码有什么问题。
  • 样本输入 1:3 样本输出 1:135 351 513 @Jasmeet
  • @Jasmeet 我找不到方法
  • @tejasvir 如果 n 大于 5 怎么办?

标签: java for-loop while-loop


【解决方案1】:
**This is the easiest way that you are looking for**
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter rows : ");
        int n = sc.nextInt();
        int x;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                x = 2 * (i + j);
                if (i + j <= n + 1) {
                System.out.print(x - 3 + " ");
                } else {
                System.out.print(x - (n * 2 + 3) + " ");
                }
            }
        System.out.println();
        }
    }
}

【讨论】:

  • 请补充说明,对初学者和学习者都有帮助。
【解决方案2】:
import java.util.Scanner;
public class Main {
    
    public static void main(String[] args) {
        // `Write your code here
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
       
        for (int i = 1; i <= n; i++) {
            int p = 2 * i - 1;
            for (int j = 1; j <= n; j++) {
                    System.out.print(p);
                    p = p + 2;
                 int maxOdd =(2*n)-1;
                 if(p>maxOdd){
                    p=1;
                }
               
            }
        System.out.println();
        }
    s.close();
        
         

    }
}

【讨论】:

  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
【解决方案3】:
import java.util.Scanner;

public class Test {

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        int maxOdd = 1 + (n - 1) * 2;
        for (int i = 1; i <= n; i++) {
            int p = 2 * i - 1;
            for (int j = 1; j <= n; j++) {
                if (p > maxOdd) {
                    p = 1;
                }
                System.out.print(p);
                p = p + 2;
            }
        System.out.println();
        }
        s.close();
    }
}

【讨论】:

    【解决方案4】:
    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {
            Scanner s = new Scanner(System.in);
            int n = s.nextInt();
            int i = 1;
            int maxOdd = 1+(n-1)*2;
            int oddCount =0;
            while(i<=n){
                int j =1;
                int odd=1;
                odd=odd+oddCount;
                while(j<=n){
                    
                    if(odd>maxOdd){
                         odd=1;
                    }
                    System.out.print(odd);
                    odd=odd+2;
                    j=j+1;
                   
                }
                System.out.println();
                i=i+1;
                oddCount= oddCount+2;
                
            }
        
        }
    }
    

    【讨论】:

      【解决方案5】:

      我刚刚更新了您的代码以正确打印您需要的输出。

      请注意,我使用数组来存储每个计数的奇数,而不是每次都创建它们。这对于跟踪以前的输出也很有用。

      import java.util.Scanner;
      
      public class Main {
          public static void main(String[] args) {
              Scanner s = new Scanner(System.in);
              int n = s.nextInt(); // Get input number n
              
              int[] arr = new int[n]; // Array to store odd numbers
              int odd = 1; // Start of odd number
              for(int i=0;i<n;i++)
              { // Store the odd numbers in array
                  arr[i] = odd;
                  odd += 2;
              }
              
              for(int i=0;i<n;i++)
              {
                  PrintArray(arr,n); // Print array
                  leftRotatebyOne(arr,n); // Rotate the array to left by one
                  System.out.println();
              }
          }
          
          static void leftRotatebyOne(int[] arr, int n) 
          { 
              int i, temp = arr[0]; 
              for (i = 0; i < n - 1; i++) 
                  arr[i] = arr[i + 1]; 
        
              arr[i] = temp; 
          }
          
          static void PrintArray(int[] arr, int n)
          {
              for(int i=0;i<n;i++)
              {
                  System.out.print(arr[i]);
              }
          }
      }
      

      【讨论】:

        【解决方案6】:

        我相信这可能是您正在寻找的:

        public static void main(String[] args) {
            // Open the scanner, set variables
            Scanner scanner = new Scanner(System.in);
            int add = 2, iterations = scanner.nextInt();
        
            // For the amount of times entered
            for (int i = 0; i < iterations; i++) {
                // Again, for the amount of times entered
                for (int j = 0; j < iterations; j++) {
                    // j * add calculates what digit we are at right now. i *
                    // add calculates the offset, so 1 becomes 3, or 5, or 7...
                    int baseValue = j * add + i * add;
                    // Calculates the max value for the amount of times entered: 5 times -> 10, 6
                    // times -> 12...
                    int max = iterations * add;
                    // Prints the rest of dividing baseValue by max, plus one (to make the number
                    // odd)
                    System.out.print(baseValue % max + 1);
                }
        
                // Starts a new line
                System.out.println();
            }
        
            // Closes the scanner, which is good practice
            scanner.close();
        }
        

        【讨论】:

        • 计算过于复杂。只需System.out.print((i + j) % iterations * 2 + 1); 不需要addbaseValuemax
        猜你喜欢
        • 2023-02-05
        • 2017-04-24
        • 2022-10-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-19
        • 2022-06-22
        相关资源
        最近更新 更多