萍聚社区-德国热线-德国实用信息网

 找回密码
 注册

微信登录

微信扫一扫,快速登录

萍聚头条

楼主: abendrobotor

JAVA的SUDOKU问题,求各位高手帮忙指点(见最后一层)

[复制链接]
 楼主| 发表于 2006-7-5 01:05 | 显示全部楼层
我坚持不住了,明天早上再来继续研究你给的tipps,不过import javax.swing.*因为我不会,所以吃力点.
先谢谢你啊,还是从你给的程序里学了不少,因为我接触的程序少,语句掌握的也少,所有有你这么一个高手在前面走,我后边跟着真是占了不少便宜.
Die von den Nutzern eingestellten Information und Meinungen sind nicht eigene Informationen und Meinungen der DOLC GmbH.
 楼主| 发表于 2006-7-6 00:12 | 显示全部楼层
greenflute,我研究了半天你18,19楼给的程序,同时还查java的dokumentation,最后还是失败了,你用的对我来说都太高级了,短时间内你给的那些语句我掌握不了啊.
不过最有建设性意见的还是你给我的那个短消息,super()现在我还不太敢用,(主要接触的例题太少了,没有什么借鉴之处),不过弄了差不多的,先用这个弱智点的,如果不行,再考虑用父类和子类.
又加了一个array,里边通过简单计算存储了预先分配给每个小panel里随机分配的3到5个数字(网上查了查,原来sudoku研究的人挺多的,那些预先安排的数字其实不是随机给的,都是从数据库里随机提出来的样板),然后在添加到小panel里,我先写了只有一个panel的,不过还是有点问题,不知道怎么回事,出错在Math.random上,求高手们指导一下啊.


  1. import java.awt.*;

  2. public class Test extends Frame
  3. {
  4.   public Button [] b = new Button[9];
  5.   public Panel [] p = new Panel[9];


  6.   public static void main (String[] args)
  7.   {
  8.    
  9.     new Test();

  10.   }
  11.   
  12.   public Test() {
  13.     // format
  14.     setLayout(new GridLayout(3,0));
  15.     for (int i = 0; i < 9; i++) {
  16.       p[i] = new Panel(new GridLayout(1,1));
  17.       add(p[i]);
  18.       b[i] = new Button();
  19.       p[i].add(b[i]);
  20.     }
  21.     setSize(180,180);
  22.     show();
  23.    
  24.     //schleife
  25.     boolean [][] gezogen = new boolean[9][9];
  26.     int neuezahl,
  27.       anzahl = 0, mal;
  28.       mal = (int)(Math.random() * 2);
  29.     for (int i = 0; i < 9; i++) {
  30.       for (int j = 0; j < 9; j++) {
  31.         gezogen[i][j] = false;  
  32.       }
  33.     }
  34.     anzahl = 0;
  35.     while (anzahl != (mal + 3)) {
  36.       neuezahl = (int)(Math.random() * 9);
  37.       for (int i = 0; i < 9; i++) {
  38.         if (!gezogen[neuezahl][i]) {
  39.           gezogen[neuezahl][i] = true;
  40.           anzahl++;
  41.         }
  42.       }
  43.     }
  44.     anzahl = 0;
  45.     while (anzahl != (mal + 3)) {
  46.       neuezahl = (int)(Math.random() * 9);
  47.       for (int i = 0; i < 9; i++) {
  48.         if (!gezogen[i][neuezahl]) {
  49.           gezogen[i][neuezahl] = true;
  50.           anzahl++;
  51.         }
  52.       }
  53.     }
  54.     for (int i = 0; i < 9; i++) {
  55.       for (int j = 0; j < 9; j++) {
  56.         if (gezogen[i][j]) {
  57.           b[i].setText(j);
  58.         }
  59.       }
  60.     }   
  61.    
  62.    
  63.   }

  64. }

复制代码

高手们,指导一下嘛.出错的原因说can not resovle symble

[ 本帖最后由 abendrobotor 于 2006-7-6 00:15 编辑 ]
Die von den Nutzern eingestellten Information und Meinungen sind nicht eigene Informationen und Meinungen der DOLC GmbH.
发表于 2006-7-6 08:29 | 显示全部楼层
高手都忙,咱们这是互相帮助,共同提高,呵呵$汗$$汗$

你用的是AWT包下的组件,Button是没有setText()方法的,应该用setLabel()。
而且由于这两个方法都直接受String类型参数,所以要用""+j的方式强制转换一下。
Math.random并没有错,虽然已经不推荐使用这个随机数了。
另外boolean初始就是false,不需要再重置一遍了。

你还是找一个IDE比较好,减小在编码和调试过程中的难度。

关于数独,看一下别人的:《完美数独》(Perfect Sudoku)
Die von den Nutzern eingestellten Information und Meinungen sind nicht eigene Informationen und Meinungen der DOLC GmbH.
发表于 2006-7-6 08:38 | 显示全部楼层
Die von den Nutzern eingestellten Information und Meinungen sind nicht eigene Informationen und Meinungen der DOLC GmbH.
 楼主| 发表于 2006-7-6 23:04 | 显示全部楼层
坐思又想还是觉得22楼那个太傻了,弄的那么复杂,出了几个错误,到现在错误还没有排除,就算排除了,估计也不好用,弄的太麻烦了,这么晕,会死人的.所以决定不在小panel上做文章了,还是以大panel里每个zeile或者每个reihe随机出几个数,只要不重复,就好,这样看着也简单,实际操作起来也方便.
弄了半天在Math.random上出问题了,原来这个random用过的啊,应该没有问题才对,以前还做过个练习,lotto随机出数的程序,现在看看,以前编的程序都有错误了,怎么回事啊.贴上来请高手看看

  1. class Lotto {
  2.   Lotto () {
  3.     boolean [] gezogen = new boolean[49];
  4.     int neuezahl,
  5.         anzahl = 0;
  6.     for (int i = 0;i < 49;i++) gezogen[i] = false;
  7.       while (anzahl != 6) {
  8.         neuezahl = (int)(Math.random() * 49);
  9.         if (!gezogen[neuezahl]) {
  10.           gezogen[neuezahl] = true;
  11.           anzahl++;
  12.         }
  13.       }
  14.     for (int i = 0;i < 49;i++)
  15.       if (gezogen[i]) System.out.println(i + 1);

  16.   }
  17.   public static void main (String [] args){
  18.     new Lotto ();
  19.   }
  20. }
复制代码

还有,greenflute说出随机数还有别的,我查了查,dokumentation里有,但是有命令,没有实例,我弄不太清楚啊,具体怎么用?比如:Random();protected int next(int bits);还有public int nextInt(int n);都怎么用啊,高手们,能不能给个简单的例子啊? 谢谢拉
Die von den Nutzern eingestellten Information und Meinungen sind nicht eigene Informationen und Meinungen der DOLC GmbH.
 楼主| 发表于 2006-7-6 23:20 | 显示全部楼层
靠~java的API上头都什么跟什么啊,按照上边写的根本无效,谁知道这个都是怎么用的?
想简单运用一下,怎么都不行啊,缺什么东西呢?

  1. import java.util.Random;
  2. public class Test {

  3.    
  4.   public static void main(String[] args) {
  5.     new Test();
  6.   }
  7.   public Test() {
  8.     public int nextInt(int n);
  9.     System.out.println(n);
  10.    
  11.     protected int next(int bits) {
  12.       System.out.println(seed);
  13.     }
  14.    
  15.   }
  16. }
复制代码
Die von den Nutzern eingestellten Information und Meinungen sind nicht eigene Informationen und Meinungen der DOLC GmbH.
发表于 2006-7-6 23:31 | 显示全部楼层
不知道你的开发环境是怎么搞的,试了一下,上面的代码没有问题。

关于随机数,除了Math的random方法以外,还有java.util.Random
An instance of this class is used to generate a stream of pseudorandom numbers. The class uses a 48-bit seed, which is modified using a linear congruential formula. (See Donald Knuth, The Art of Computer Programming, Volume 2, Section 3.2.1.)


和java.security.SecureRandom
This class provides a cryptographically strong random number generator (RNG).

A cryptographically strong random number minimally complies with the statistical random number generator tests specified in FIPS 140-2, Security Requirements for Cryptographic Modules, section 4.9.1. Additionally, SecureRandom must produce non-deterministic output. Therefore any seed material passed to a SecureRandom object must be unpredictable, and all SecureRandom output sequences must be cryptographically strong, as described in RFC 1750: Randomness Recommendations for Security.


至于其他软件包,就不在这里讨论了
Die von den Nutzern eingestellten Information und Meinungen sind nicht eigene Informationen und Meinungen der DOLC GmbH.
发表于 2006-7-7 00:09 | 显示全部楼层

  1. public Test() {
  2.     public int nextInt(int n);
  3.     System.out.println(n);
  4.    
  5.     protected int next(int bits) {
  6.       System.out.println(seed);
  7.     }
  8.    
  9.   }
复制代码

你这已经是构造函数了,就不能再声明方法了,也不能在变量前加访问限制符了.
Die von den Nutzern eingestellten Information und Meinungen sind nicht eigene Informationen und Meinungen der DOLC GmbH.
 楼主| 发表于 2006-7-7 00:18 | 显示全部楼层
原帖由 greenflute 于 2006-7-7 00:09 发表

public Test() {
    public int nextInt(int n);
    System.out.println(n);
   
    protected int next(int bits) {
      System.out.println(seed);
    }
   
  }

你这已经是构造函数了,就不 ...


啊,其他方法也不能用了,那它这个有什么用啊,出个数,也用不了?
Die von den Nutzern eingestellten Information und Meinungen sind nicht eigene Informationen und Meinungen der DOLC GmbH.
发表于 2006-7-7 00:32 | 显示全部楼层
Die von den Nutzern eingestellten Information und Meinungen sind nicht eigene Informationen und Meinungen der DOLC GmbH.
您需要登录后才可以回帖 登录 | 注册 微信登录

本版积分规则

手机版|Archiver|AGB|Impressum|Datenschutzerklärung|萍聚社区-德国热线-德国实用信息网 |网站地图

GMT+2, 2024-6-16 01:28 , Processed in 0.058176 second(s), 13 queries , MemCached On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表