
正文
九宫格java语言代码 java九宫格拼图
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
九九乘法表怎么用JAVA语言编写
如果把九九乘法表中如“1*1=1”等式全部看作一个个整体的话,九九乘法表可看作一个直角三角形,实现直角三角形可用两个for循环嵌套来实现,那么我们最后输出应为System.out.print(变量1+"*"+变量2+"="+(变量1*变量2)+" ")。
输入代码如下:package ch02;public class TEST{public static void main(String[] args) {for (int i = 1; i =9; i++) {for (int j = 1; j = i; j++) {System.out.print(j+"*"+i+"="+(i*j)+" ");}System.out.println();}}}
测试结果 :
1*1=1、1*2=2 2*2=4、1*3=3 2*3=6 3*3=9、1*4=4 2*4=8 3*4=12 4*4=16、1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36、1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49、1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
扩展资料:
Java 技术具有卓越的通用性、高效性、平台移植性和安全性,广泛应用于PC、数据中心、游戏控制台、科学超级计算机、移动电话和互联网,同时拥有全球最大的开发者专业社群。
public static void main(String[] args){for (int i=1;i=9;i++){for (int j=1;j=i;j++){System.out.print(j+"*"+i+"="+i*j+" ");}System.out.println();}}
参考资料:JAVA语言百度百科
相关问答
Q1: 用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
}
Q2: 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;
}
}
Q3: 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");//从你的程序中将此语句上移到此位置
}
}
}
Q4: java编程九宫格问题
要求:根据输入的数字n,如:3,5,7...以矩阵显示n行n列数,这些数由1~n*n构成,要求矩阵的每行每列及对角线上n个数之和相等 预备知识: 在距阵中,1在第一行正中,随后的数字应放到上一个数字的右上方方格中,如果向上不行,就放到该列的最下方格子;如果向右不行,就放到该行的最左边;如果都不行,就放到上一个数字的正下方;如果目标格子中已经有数字,也放到上一个数字的正下方 思路: 1) 使用2维数组预备存储1~n*n这些数字 2) 1是放到第一行正中的,所以其索引号是:[0][(n-1)/2] 3) 随后的数字,其索引号原则如下 1 num的行索引为 num-1 的 (行索引-1) , num的列索引为 num-1 的 (列索引+1) 2如果发现num的行,列索引都越位(-1或n),则 num的行索引为 num-1 的 (行索引+1) , num的列索引为 num-1 的 (列索引) 3如果发现num的行,列索引指向的位置已经有数字,则 num的行索引为 num-1 的 (行索引+1) , num的列索引为 num-1 的 (列索引) 4如果发现num的行越位(-1),则 num的行索引为n-1 5如果发现num的列越位(n),则 num的列索引为0 import java.util.Scanner; public class JiuGong { public static void main(String[] args) { Scanner s = new Scanner(System.in); int x = s.nextInt(); //输入长度 int h = 0; //行 //在距阵中,1在第一行正中 int l = x / 2; //列 int[][] a = new int[x][x]; for (int i = 1; i = x * x; i++) { a[h][l] = i; //运行提示溢出 //随后的数字应放到上一个数字的右上方方格中 h--; l++; //3.如果都不行,就放到上一个数字的正下方 if (h 0 l = x) { //先返回上一个数字 h++; l--; //再下移一行 h++; } //1.如果向上不行,就放到该列的最下方格子 else if (h 0) { h = x - 1; } //2.如果向右不行,就放到该行的最左边 else if (l = x) { l = 0; } //4.如果目标格子中已经有数字,也放到上一个数字的正下方 else if (a[h][l] 0) { //先返回上一个数字 h++; l--; //再下移一行 h++; } } //打印九宫格 for (int j = 0; j x; j++) { for (int k = 0; k x; k++) { System.out.print(a[j][k] + " "); } //换行 System.out.println(); } } }
采纳哦
Q5: 新手紧急求助,编一个九宫格游戏的java程序
import java.util.HashMap;
/**
* 知识点1 静态方法的调用
* 知识点2 带参数的静态方法调用
* 知识点3 for循环
* 知识点4 HashMap的遍历
* 知识点5 格式化输出
* @author Administrator
* 补充要学习的知识点
* 1.Character 和 char的区别
* 2.HashMap对象中的常用方法
*/
public class Helper {
/**
* 主方法
* 获取下面两个静态方法的值按格式打印
* @param args
*/
public static void main(String[] args) {
//调用最下面的createCharArray()方法获得的结果负值给Character数组
Character[] charArray = createCharArray();
//调用下面的countLetters(Character[] charArray)带参数方法获得结果负值给HashMap对象
HashMaplt;Character, Integer counts = countLetters(charArray);
//匹配住所有的HashMap对象
for (Character c : counts.keySet()) {
//循环格式化打印显示
System.out.printf(quot;(%c,%d)quot;, c, counts.get(c));
}
}
/**
* 验证所有含元素次数的静态方法
* @param charArray
* @return HashMaplt;Character, Integer result
*/
private static HashMaplt;Character, Integer countLetters(
Character[] charArray) {
//声明一个HashMap对象
HashMaplt;Character, Integer result = new HashMaplt;Character, Integer();
//匹配住传进来的参数charArray中所有的元素
for (Character c : charArray) {
//非空验证
if (result.get(c) == null)
//空的话把匹配住的元素放进HashMap对象中 c为键 负值为1
result.put(c, 1);
else
//已经存在把获取的值取出并且重新复制 替代掉原由的
result.put(c, result.get(c) + 1);
}
//返回给调用者
return result;
}
/**
* 随即生成100个字母的静态方法
* @return Character[]chars
*/
private static Character[] createCharArray() {
//声明一个有100个元素的Character数组
Character[] chars = new Character[100];
//循环100次
for (int i = 0; i lt; chars.length; i++) {
//随即获取一个字母负值给char对象c
char c = (char) (#39;a#39; + Math.random() * (#39;z#39; - #39;a#39; + 1));
//把随即获得的值放入Character数组
chars[i] = c;
}
//返回给调用者
return chars;
}
}
希望对你的学习有所帮助
关于九宫格java语言代码和java九宫格拼图的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。







