「九宫格代码java」九宫格代码python

博主:adminadmin 2023-03-18 11:36:07 404

本篇文章给大家谈谈九宫格代码java,以及九宫格代码python对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

java 输出九宫格

将你其中某些问题的答案放在代码注释中了.

这个程序输出的是固定的九宫格,我想,是根据固有的九宫格中的数字与数组下标的关系来写的代码。

希望对你有所帮助,加油!

class S{

 public static void main(String[] args) { 

  int arr[][] = new int[3][3]; 

  //创建一个三阶方阵  

  int a = 2;        

  //第3行的行下标

  //???这里是什么意思,2从何而来

  //A:java中数组的下标从0开始

  int b = 3/2;    

  //第2列的列下标

  //???同上 

  //A:这里由于b=1,(int)/(int),java中数组的下标从0开始

  for(int i=1;i=9;i++){  

   //给数组赋值   

   arr[a++][b++] = i;  

   if(i%3==0){     

    //如果i是3的倍数——————???为什么要判断是不是3的倍数 

    a = a-2;

    //————————————————???if...else里面的语句是什么意思,作用是什么   

    b = b-1;//————————————————???同上  

    }

   //使a,b回到起点:a=2,b=1;

   else{       

     //如果i不是3的倍数//————————————————???同上    

    a = a%3;   

    b = b%3;   

    }  

   }

  //????九宫格的每一行、每一列、对角线都等于15,

  //???但是这里连一个15这个数字都没有出现,但还是成功输出

  //————————————————————???他是怎么做到的 

  System.out.println("输出九宫格:");   

  //遍历输出九宫格  

  for(int i=0;i3;i++){ 

   for(int j=0;j3;j++){  

    System.out.print(arr[i][j]+"  "); 

   }

  System.out.print("\n");//从你的程序中将此语句上移到此位置 

  }

 }

 }

用java做个九宫格

定义了一个package名叫aloha

把下面的代码粘贴了,编译运行就可以了

不用谢我了!

/*

* NineGrid.java

* @author libai8723@qq.com

* Created on 2011-12-20, 13:21:36

*/

package aloha;

import java.awt.Dimension;

import java.awt.Toolkit;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JOptionPane;

/**

*

* @author libai

*/

public class NineGrid extends javax.swing.JFrame implements ActionListener{

/** Creates new form NineGrid */

public NineGrid() {

initComponents();

Toolkit tk = Toolkit.getDefaultToolkit();

Dimension d = tk.getScreenSize();

this.setSize(400, 400);

this.setTitle("Nine Grid");

this.setLocation((int)(d.getWidth() - 400)/2, (int)(d.getHeight() - 400)/2);

for(int i = 0;i 15;i++)

{

this.arr[i] = i+1;

}

this.arr[15] = -1;

this.arr[11] = -1;

this.arr[15] = 12;

for(int i = 0;i 15;i++)

{

int idx =(int) (Math.random() * 15);

int tmp = this.arr[7];

this.arr[7] = this.arr[idx];

this.arr[idx] = tmp;

}

for(int i = 0;i 4;i++)

{

for(int j = 0;j 4;j++)

{

if(this.arr[i * 4 + j] != -1)

{

this.Buttons[i][j] = new JButton("" + this.arr[i * 4 + j]);

this.Buttons[i][j].addActionListener(this);

this.getContentPane().add(this.Buttons[i][j]);

}

else

{

this.Buttons[i][j] = new JButton("");

this.Buttons[i][j].addActionListener(this);

this.getContentPane().add(this.Buttons[i][j]);

this.Buttons[i][j].setEnabled(false);

}

}

}

}

/** This method is called from within the constructor to

* initialize the form.

* WARNING: Do NOT modify this code. The content of this method is

* always regenerated by the Form Editor.

*/

@SuppressWarnings("unchecked")

// editor-fold defaultstate="collapsed" desc="Generated Code"

private void initComponents() {

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

getContentPane().setLayout(new java.awt.GridLayout(4, 4));

pack();

}// /editor-fold

/**

* @param args the command line arguments

*/

public static void main(String args[]) {

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

new NineGrid().setVisible(true);

}

});

}

private JButton[][] Buttons = new JButton[4][4];

private int[] arr = new int[16];

private boolean isSucceed()

{

boolean flag = true;

for(int i = 0;i 4;i++)

{

for(int j = 0;j 4;j++)

{

if(!this.Buttons[i][j].getText().equals(""))

if(!this.Buttons[i][j].getText().equals(""+(i * 4 + j + 1)))

{

return false;

}

}

}

return true;

}

public void actionPerformed(ActionEvent e)

{

int i = 0,j = 0;

boolean in = false;

for(i = 0;i 4;i++)

{

for(j = 0;j 4;j++)

{

if(e.getSource() == this.Buttons[i][j])

{

in = true;

break;

}

}

if(in)

break;

}

if((i = 0 (j - 1) = 0)(!this.Buttons[i][j - 1].isEnabled()))

{

String tmp = this.Buttons[i][j].getText();

this.Buttons[i][j].setText(this.Buttons[i][j - 1].getText());

this.Buttons[i][j - 1].setText(tmp);

this.Buttons[i][j - 1].setEnabled(true);

this.Buttons[i][j].setEnabled(false);

if(this.isSucceed())

JOptionPane.showConfirmDialog(this, "You Win!!!!");

return;

}

if((i = 0 (j + 1) 4)(!this.Buttons[i][j + 1].isEnabled()))

{

String tmp = this.Buttons[i][j].getText();

this.Buttons[i][j].setText(this.Buttons[i][j + 1].getText());

this.Buttons[i][j + 1].setText(tmp);

this.Buttons[i][j + 1].setEnabled(true);

this.Buttons[i][j].setEnabled(false);

if(this.isSucceed())

JOptionPane.showConfirmDialog(this, "You Win!!!!");

return;

}

if((i - 1 = 0 j = 0)(!this.Buttons[i - 1][j].isEnabled()))

{

String tmp = this.Buttons[i][j].getText();

this.Buttons[i][j].setText(this.Buttons[i - 1][j].getText());

this.Buttons[i - 1][j].setText(tmp);

this.Buttons[i - 1][j].setEnabled(true);

this.Buttons[i][j].setEnabled(false);

if(this.isSucceed())

JOptionPane.showConfirmDialog(this, "You Win!!!!");

return;

}

if((i + 1 4 j = 0)(!this.Buttons[i + 1][j].isEnabled()))

{

String tmp = this.Buttons[i][j].getText();

this.Buttons[i][j].setText(this.Buttons[i + 1][j].getText());

this.Buttons[i + 1][j].setText(tmp);

this.Buttons[i + 1][j].setEnabled(true);

this.Buttons[i][j].setEnabled(false);

if(this.isSucceed())

JOptionPane.showConfirmDialog(this, "You Win!!!!");

return;

}

}

// Variables declaration - do not modify

// End of variables declaration

}

java 写一个九宫格的代码!不用很复杂,实现了基本的功能就行! 谢谢大家!我自己写的都晕了

/*

九宫格算法代码 C++版

*/

#include stdio.h

int map[9][9] = {0, 0, 3, 8, 1, 0, 0, 0, 9,

5, 0, 0, 4, 0, 0, 0, 8, 0,

0, 6, 0, 9, 0, 0, 1, 0, 0,

0, 0, 8, 0, 3, 0, 0, 0, 6,

0, 0, 0, 0, 0, 0, 0, 0, 0,

9, 0, 0, 6, 0, 0, 5, 0, 0,

0, 0, 6, 0, 0, 9, 0, 1, 0,

0, 1, 0, 0, 0, 5, 0, 0, 4,

2, 0, 0, 0, 4, 8, 7, 0, 0};

void display()

{

int i;

int j;

for (i = 0; i 9; i++)

{

for (j = 0; j 9; j++)

{

if(map[i][j])

{

printf(" %d ", map[i][j]);

}

else

{

printf(" ");

}

}

printf("\n");

}

}

int check(int x, int y, int *mark) //check函数为该格子可以选择的数字

{

int i;

int j;

int gi;

int gj;

int count = 0;

for (i = 1; i = 9; i++)

{

mark[i] = 0; //初始化

}

for (i = 0; i 9; i++)

{

mark[map[x][i]] = 1; //表示该行该数字已存在

mark[map[i][y]] = 1; //表示该列该数字已存在

}

gi = x / 3 * 3; // 九宫格把map分割成9个小块,如果该格子所在行列为(1,4),那么它所在子格子起始为(0,3)到(2,5)的九个小格子

gj = y / 3 * 3;

for (i = 0; i 3; i++)

{

for (j = 0; j 3; j++)

{

mark[map[gi + i][gj + j]] = 1; //此循环为表示所在子格子的九个小格子中已存在该数字

}

}

for (i = 1; i = 9; i++)

{

if(0 == mark[i]) //如果该数字不存在则++

{

count++;

}

}

return count;

}

void crack() //??

{

int i;

int j;

int mark[10]; //标志数字是否已存在

int min = 10; //记录最少可能数字数

int ci = -1; //ci,cj用来记录最少可能数字的格子所在行列

int cj;

for (i = 0; i 9; i++)

{

for (j = 0; j 9; j++)

{

if (map[i][j])

{

continue; //如果该格子已有数字则跳到下一个格子

}

int c = check(i, j, mark); //c为该格子可以选择的数字

if (0 == c)

{

return;

}

if (c min)

{

ci = i;

cj = j;

min = c;

}

}

}

if (-1 == ci)

{

printf("The answer is:\n");

display();

return;

}

check(ci, cj, mark);//这个语句的作用是把mark这个数组设置成最小可能格子缺少的数字,若缺少则为0

for (i = 1; i = 9; i++)

{

if (mark[i] == 0)

{

map[ci][cj] = i; //先填入该数字

crack(); //进行判断

}

map[ci][cj] = 0; //若这个数字不可以得到解则判断下一可能数字(这里类似0-1背包问题)

}

}

int main()

{

printf("The game is:\n");

display();

crack();

return 0;

}

java设计九宫格设计思路啊,江湖救急请大神指点!

这样和你说吧。 其实这个代码的目的是通用的奇数九宫格。

奇数九宫格先做成 斜线方阵

然后将超出部分填入对称的格中,这里就是 1 放到 8,6之间,3 放到4,8之间。

一开始的 a,b是为了定位 (1) 的格子,

a++,b++ 就是在斜向写数字, a,b%3 就是在超出格子后转到对称的位置。

i%3 其实就是写完一个 1,2,3的列,开始写4,5,6的斜行。

你看不明白,是因为这里带有数学算法。

java编程题,在九宫格内填入1—9九个数字,使得横竖排的数字相加之和都相等

/*直接复制运行就可以,每一行的九个数字代表一个九宫格的9个数字,从左到右,从上到下*/

import java.util.ArrayList;

import java.util.Arrays;

public class Test1 {

private static ArrayListString arrangeList = new ArrayListString(); 

 public static void main(String[] args) {

String str = "123456789";//你要排列组合的字符串

char list[] = str.toCharArray();//将字符串转换为字符数组

genernateData(list, 0, list.length - 1);//参数为字符数组和0和字符数组最大下标

int arr[]=new int[9];

for(String str1 : arrangeList){

for(int k=0;k9;k++){

arr[k]=Integer.parseInt(str1.substring(k,k+1));

}

if(arr[0]+arr[1]+arr[2]==15arr[3]+arr[4]+arr[5]==15arr[6]+arr[7]+arr[8]==15arr[0]+arr[3]+arr[6]==15arr[1]+arr[4]+arr[7]==15arr[2]+arr[5]+arr[8]==15arr[0]+arr[4]+arr[8]==15arr[2]+arr[4]+arr[6]==15){

System.out.println(Arrays.toString(arr));

}

}

}

 

public static void genernateData(char list[], int k, int m) {

 if (k  m) {

 StringBuffer sb = new StringBuffer();//创建一个StringBuffer对象sb

 for (int i = 0; i = m; i++) {

 sb.append(list[i]);//循环将字符数组值追加到StringBuffer中

 }

 arrangeList.add(sb.toString());

 } else {

 for (int i = k; i = m; i++) {

 swapData(list, k, i);//将下表为k和i的值调换位置

 genernateData(list, k + 1, m);

 swapData(list, k, i);

 }

 }

}

private static void swapData(char list[], int k, int i) {

char temp = list[k];

list[k] = list[i];

list[i] = temp;

}

}

九宫格代码java的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于九宫格代码python、九宫格代码java的信息别忘了在本站进行查找喔。