我如何在java中解决这个问题 [英] How do I solve this in java

查看:76
本文介绍了我如何在java中解决这个问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实现一个输入两个整数m和n的程序,并绘制m个2个方块,每个大小为n×n,排列在m×m表中。也就是说,它绘制一个m行m列的表,每个行的元素是n×n的正方形。例如,如果输入整数为4和6,则绘制16个正方形,每个尺寸为6×6,排列在4×4表中。每个方块的周长由符号*(星号)组成,内部空间用符号o(小写字母o)填充。



输入和输出:



你的程序应该提示用户输入两个整数,m和n,在2和8之间,并绘制m 2个方格,每个尺寸n×n。



我的尝试:



有不知道,这个帽子我试过了吗?


 import java.io. *; 
public class JavaApplication19 {

static void generateSquare(int n)
{
int [] [] magicSquare = new int [n] [n];

//初始化1
的位置i = n / 2;
int j = n-1;

//逐个将所有值放在幻方
中(int num = 1; num< = n * n;)
{
if( i == - 1&& j == n)//第3个条件
{
j = n-2;
i = 0;
}
其他
{
//第一个条件助手如果下一个数字
//走出正方形的右边
if(j == n )
j = 0;

//第一个条件助手如果下一个数字是
//则超出正方形的上限
if(i <0)
i = n-1;
}

//第二个条件
if(magicSquare [i] [j]!= 0)
{
j - = 2;
i ++;
继续;
}
else
//设定数字
magicSquare [i] [j] = num ++;

//第一个条件
j ++;一世 - ;
}

// print magic square
System.out.println(The Magic Square for+ n +:);
System.out.println(每行或每列的总和+ n *(n * n + 1)/ 2 +:);
for(i = 0; i< n; i ++)
{
for(j = 0; j< n; j ++)
System.out.print(* );
System.out.println();
}
}
public static void main(String [] args){
//仅当n为奇数时才有效
int n = 3;
generateSquare(n);
}

}

解决方案

仅回答从未答复列表中删除:由OP解决。

Implement a program that inputs two integers, m and n, and draws m 2 squares, each of the size n × n, arranged in an m × m table. That is, it draws a table with m rows and m columns, each element of which is an n × n square. For example, if the input integers are 4 and 6, it draws 16 squares, each of the size 6 × 6, arranged in a 4 × 4 table. The perimeter of each square is composed of the symbol "*" (asterisk), and the space inside is filled with the symbol "o" (lowercase letter o).

Input and output:

Your program should prompt the user to enter two integers, m and n, between 2 and 8, and draw m 2 squares, each of the size n × n.

What I have tried:

have no idea ,this hat i have tried

import java.io.*;
public class JavaApplication19 {

   static void generateSquare(int n)
    {
        int[][] magicSquare = new int[n][n];
         
        // Initialize position for 1
        int i = n/2;
        int j = n-1;
  
        // One by one put all values in magic square
        for (int num=1; num <= n*n; )
        {
            if (i==-1 && j==n) //3rd condition
            {
                j = n-2;
                i = 0;
            }
            else
            {
                //1st condition helper if next number 
                // goes to out of square's right side
                if (j == n)
                    j = 0;
                     
                //1st condition helper if next number is 
                // goes to out of square's upper side
                if (i < 0)
                    i=n-1;
            }
             
            //2nd condition
            if (magicSquare[i][j] != 0) 
            {
                j -= 2;
                i++;
                continue;
            }
            else
                //set number
                magicSquare[i][j] = num++; 
                 
            //1st condition
            j++;  i--; 
        }
  
        // print magic square
        System.out.println("The Magic Square for "+n+":");
        System.out.println("Sum of each row or column "+n*(n*n+1)/2+":");
        for(i=0; i<n; i++)
        {
            for(j=0; j<n; j++)
                System.out.print(" *");
            System.out.println();
        }
    }
    public static void main(String[] args) {
       // Works only when n is odd
        int n = 3;
        generateSquare(n);
    }
    
}

解决方案

Answered only to remove from unanswered list: solved by OP.


这篇关于我如何在java中解决这个问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆