新聞中心
Java數(shù)獨游戲代碼
public class ShuDu {
創(chuàng)新互聯(lián)是一家專注于成都做網(wǎng)站、網(wǎng)站制作與策劃設(shè)計,米脂網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)十多年,網(wǎng)設(shè)計領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:米脂等地區(qū)。米脂做網(wǎng)站價格咨詢:13518219792
/**存儲數(shù)字的數(shù)組*/
static int[][] n = new int[9][9];
/**生成隨機數(shù)字的源數(shù)組,隨機數(shù)字從該數(shù)組中產(chǎn)生*/
static int[] num = {1,2,3,4,5,6,7,8,9};
public static void main(String[] args) {
//生成數(shù)字
for(int i = 0;i 9;i++){
//嘗試填充的數(shù)字次數(shù)
int time = 0;
//填充數(shù)字
for(int j = 0;j 9;j++){
//產(chǎn)生數(shù)字
n[i][j] = generateNum(time);
//如果返回值為0,則代表卡住,退回處理
//退回處理的原則是:如果不是第一列,則先倒退到前一列,否則倒退到前一行的最后一列
if(n[i][j] == 0){
//不是第一列,則倒退一列
if(j 0){
j-=2;
continue;
}else{//是第一列,則倒退到上一行的最后一列
i--;
j = 8;
continue;
}
}
//填充成功
if(isCorret(i,j)){
//初始化time,為下一次填充做準(zhǔn)備
time = 0;
}else{ //繼續(xù)填充
//次數(shù)增加1
time++;
//繼續(xù)填充當(dāng)前格
j--;
}
}
}
//輸出結(jié)果
for(int i = 0;i 9;i++){
for(int j = 0;j 9;j++){
System.out.print(n[i][j] + " ");
}
System.out.println();
}
}
/**
* 是否滿足行、列和3X3區(qū)域不重復(fù)的要求
* @param row 行號
* @param col 列號
* @return true代表符合要求
*/
public static boolean isCorret(int row,int col){
return (checkRow(row) checkLine(col) checkNine(row,col));
}
/**
* 檢查行是否符合要求
* @param row 檢查的行號
* @return true代表符合要求
*/
public static boolean checkRow(int row){
for(int j = 0;j 8;j++){
if(n[row][j] == 0){
continue;
}
for(int k =j + 1;k 9;k++){
if(n[row][j] == n[row][k]){
return false;
}
}
}
return true;
}
/**
* 檢查列是否符合要求
* @param col 檢查的列號
* @return true代表符合要求
*/
public static boolean checkLine(int col){
for(int j = 0;j 8;j++){
if(n[j][col] == 0){
continue;
}
for(int k =j + 1;k 9;k++){
if(n[j][col] == n[k][col]){
return false;
}
}
}
return true;
}
/**
* 檢查3X3區(qū)域是否符合要求
* @param row 檢查的行號
* @param col 檢查的列號
* @return true代表符合要求
*/
public static boolean checkNine(int row,int col){
//獲得左上角的坐標(biāo)
int j = row / 3 * 3;
int k = col /3 * 3;
//循環(huán)比較
for(int i = 0;i 8;i++){
if(n[j + i/3][k + i % 3] == 0){
continue;
}
for(int m = i+ 1;m 9;m++){
if(n[j + i/3][k + i % 3] == n[j + m/3][k + m % 3]){
return false;
}
}
}
return true;
}
/**
* 產(chǎn)生1-9之間的隨機數(shù)字
* 規(guī)則:生成的隨機數(shù)字放置在數(shù)組8-time下標(biāo)的位置,隨著time的增加,已經(jīng)嘗試過的數(shù)字將不會在取到
* 說明:即第一次次是從所有數(shù)字中隨機,第二次時從前八個數(shù)字中隨機,依次類推,
* 這樣既保證隨機,也不會再重復(fù)取已經(jīng)不符合要求的數(shù)字,提高程序的效率
* 這個規(guī)則是本算法的核心
* @param time 填充的次數(shù),0代表第一次填充
* @return
*/
public static int generateNum(int time){
//第一次嘗試時,初始化隨機數(shù)字源數(shù)組
if(time == 0){
for(int i = 0;i 9;i++){
num[i] = i + 1;
}
}
//第10次填充,表明該位置已經(jīng)卡住,則返回0,由主程序處理退回
if(time == 9){
return 0;
}
//不是第一次填充
//生成隨機數(shù)字,該數(shù)字是數(shù)組的下標(biāo),取數(shù)組num中該下標(biāo)對應(yīng)的數(shù)字為隨機數(shù)字
int ranNum = (int)(Math.random() * (9 - time));
//把數(shù)字放置在數(shù)組倒數(shù)第time個位置,
int temp = num[8 - time];
num[8 - time] = num[ranNum];
num[ranNum] = temp;
//返回數(shù)字
return num[8 - time];
}
}
c++或java 寫一個解3階數(shù)獨的程序
123456789101112131415161718192021public static Geocache[] createGeocaches(int a) { if(a = 0) return new Geocache[0]; Random rand = new Random(); Geocache[] result = new Geocache[a]; for(int i = 0; i a; i++) { //因為題目沒有描述,這里假設(shè)x, y是隨機整數(shù),Geocache有a href=";tn=44039180_cprfenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1dhrjIWn1D4n19hmWDzm1R0IAYqnWm3PW64rj0d0AP8IA3qPjfsn1bkrjKxmLKz0ZNzUjdCIZwsrBtEXh9GuA7EQhF9pywdQhPEUiqkIyN1IA-EUBtkPWm4rjR4rHbLPWR1nH63P16L" target="_blank" class="baidu-highlight"構(gòu)造函數(shù)/a(int, int) int x = rand.nextInt(); int y = rand.nextInt(); result[i] = new Geocache(x, y); } return result; }
求編寫一個超級簡單的Java的程序源代碼
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class ConstructFrame extends JFrame
{
private static final long serialVersionUID = 1L;
String value1="",result,value2="";
int flag=0,fix=0,sum=1;
Boolean happy;
JTextField text=new JTextField(30);
int flagsum=0;
Container c=getContentPane();
JButton buttonx;
ConstructFrame()
{ super("計算器");
c.setLayout(null);
c.setBackground(Color.blue);
this.setSize(400, 400);
c.add(text);
text.setHorizontalAlignment(JTextField.RIGHT);
final JButton buttonx=new JButton("BackSpace");
c.add(buttonx);
buttonx.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
int count=0;
String temp;
if(flag==0)
{
count=value1.length();
if(count!=1)
temp=value1.substring(0, count-1);
else
temp="0";
value1=temp;
}
else
{
count=value2.length();
if(count!=1)
temp=value2.substring(0, count-1);
else
temp="0";
value2=temp;
}
text.setText(temp);
}
});
final JButton buttony=new JButton("CE");
c.add(buttony);
buttony.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
value1="";
value2="";
flag=0;
text.setText("0");
}
});
final JButton button1=new JButton("1");
c.add(button1);
button1.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
String temp;
if(flag==0)
{
value1=value1+1;
temp=value1;
}
else
{
value2=value2+1;
temp=value2;
}
text.setText(temp);
}
});
final JButton button2=new JButton(" 2 ");
c.add(button2);
button2.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
String temp;
if(flag==0)
{
value1=value1+2;
temp=value1;
}
else
{
value2=value2+2;
temp=value2;
}
text.setText(temp);
}
});
final JButton button3=new JButton("3");
c.add(button3);
button3.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
String temp;
if(flag==0)
{
value1=value1+3;
temp=value1;
}
else
{
value2=value2+3;
temp=value2;
}
text.setText(temp);
}
});
final JButton button4=new JButton("4");
c.add(button4);
button4.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
String temp;
if(flag==0)
{
value1=value1+4;
temp=value1;
}
else
{
value2=value2+4;
temp=value2;
}
text.setText(temp);
}
});
final JButton button5=new JButton("5");
c.add(button5);
button5.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
String temp;
if(flag==0)
{
value1=value1+5;
temp=value1;
}
else
{
value2=value2+5;
temp=value2;
}
text.setText(temp);
}
});
final JButton button6=new JButton("6");
c.add(button6);
button6.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
String temp;
if(flag==0)
{
value1=value1+6;
temp=value1;
}
else
{
value2=value2+6;
temp=value2;
}
text.setText(temp);
}
});
final JButton button7=new JButton("7");
c.add(button7);
button7.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
String temp;
if(flag==0)
{
value1=value1+7;
temp=value1;
}
else
{
value2=value2+7;
temp=value2;
}
text.setText(temp);
}
});
final JButton button8=new JButton("8");
c.add(button8);
button8.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
String temp;
if(flag==0)
{
value1=value1+8;
temp=value1;
}
else
{
value2=value2+8;
temp=value2;
}
text.setText(temp);
}
});
final JButton button9=new JButton("9");
c.add(button9);
button9.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
String temp;
if(flag==0)
{
value1=value1+9;
temp=value1;
}
else
{
value2=value2+9;
temp=value2;
}
text.setText(temp);
}
});
final JButton button0=new JButton("0");
c.add(button0);
button0.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
String temp;
if(flag==0)
{
value1=value1+0;
temp=value1;
}
else
{
value2=value2+0;
temp=value2;
}
text.setText(temp);
}
});
final JButton buttonadd=new JButton(" + ");
c.add(buttonadd);
buttonadd.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
flag=1;
fix=1;
flagsum=0;
}
});
final JButton buttonsubtract=new JButton(" - ");
c.add(buttonsubtract);
buttonsubtract.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
flag=1;
fix=2;
flagsum=0;
}
});
final JButton buttoncheng=new JButton(" * ");
c.add(buttoncheng);
buttoncheng.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
flag=1;
fix=3;
flagsum=0;
}
});
final JButton buttonchu=new JButton(" / ");
c.add(buttonchu);
buttonchu.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
flag=1;
flagsum=0;
fix=4;
}
});
final JButton buttonequal=new JButton(" = ");
c.add(buttonequal);
buttonequal.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
double temp1,temp2;
double temp=0;
flagsum=0;
temp1=Double.parseDouble(value1);
temp2=Double.parseDouble(value2);
flag=0;
switch(fix)
{
case 1: temp=temp1+temp2;break;
case 2: temp=temp1-temp2;break;
case 3: temp=temp1*temp2;break;
case 4: temp=temp1/temp2;break;
}
result=Double.valueOf(temp).toString();
value1=result;
value2="";
flag=1;
text.setText(result);
}
});
final JButton buttonpoint=new JButton(".");
c.add(buttonpoint);
buttonpoint.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{ if(flagsum==0)
{
String temp;
if(flag==0 )
{
value1=value1+".";
temp=value1;
}
else
{
value2=value2+".";
temp=value2;
}
flagsum=1;
text.setText(temp);
}
}
});
JButton buttonz=new JButton("Start");
c.add(buttonz);
buttonz.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{ if(sum%2==1)
{
happy=true;
text.setText("0.");
flag=0;
}
else
{
happy=false;
value1="";
value2="";
text.setText("");
}
text.setEnabled(happy);
button1.setEnabled(happy);
button2.setEnabled(happy);
button3.setEnabled(happy);
button4.setEnabled(happy);
button5.setEnabled(happy);
button6.setEnabled(happy);
button7.setEnabled(happy);
button8.setEnabled(happy);
button9.setEnabled(happy);
button0.setEnabled(happy);
buttonx.setEnabled(happy);
buttony.setEnabled(happy);
buttonadd.setEnabled(happy);
buttonsubtract.setEnabled(happy);
buttonpoint.setEnabled(happy);
buttonequal.setEnabled(happy);
buttoncheng.setEnabled(happy);
buttonchu.setEnabled(happy);
sum++;
}
});
button1.setEnabled(false);
button2.setEnabled(false);
button3.setEnabled(false);
button4.setEnabled(false);
button5.setEnabled(false);
button6.setEnabled(false);
button7.setEnabled(false);
button8.setEnabled(false);
button9.setEnabled(false);
button0.setEnabled(false);
buttonx.setEnabled(false);
buttony.setEnabled(false);
buttonadd.setEnabled(false);
buttonsubtract.setEnabled(false);
buttonpoint.setEnabled(false);
buttonequal.setEnabled(false);
buttoncheng.setEnabled(false);
buttonchu.setEnabled(false);
text.setEnabled(false);
text.setBounds(20, 20, 200, 40);
buttonx.setBounds(20, 60,100, 40);
buttony.setBounds(140, 60,100, 40);
buttonz.setBounds(260, 60,80, 40);
button1.setBounds(20, 120,60, 40);
button2.setBounds(100, 120,60, 40);
button3.setBounds(180, 120,60, 40);
buttonadd.setBounds(260, 120,60, 40);
button4.setBounds(20, 180,60, 40);
button5.setBounds(100, 180,60, 40);
button6.setBounds(180, 180,60, 40);
buttonsubtract.setBounds(260, 180,60, 40);
button7.setBounds(20, 240,60, 40);
button8.setBounds(100, 240,60, 40);
button9.setBounds(180, 240,60, 40);
buttoncheng.setBounds(260,240,60,40);
button0.setBounds(20, 300,60, 40);
buttonpoint.setBounds(100, 300, 60, 40);
buttonequal.setBounds(180,300,60, 40);
buttonchu.setBounds(260, 300,60, 40);
setVisible(true);
}
class MYMouseEvent extends MouseAdapter
{
public void mousePressed(MouseEvent e)
{
value1=e.toString();
text.setText(value1);
}
}
}
class Calutator
{
public static void main(String[] args)
{
new ConstructFrame();
}
}
你自己慢慢的看吧!
本文名稱:簡單數(shù)獨java源代碼 數(shù)獨代碼Java
當(dāng)前路徑:http://www.dlmjj.cn/article/doddpje.html