标题:Ramdom类报错,不知道问题出在哪里?
只看楼主
大脸猫xiang
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2014-3-30
结帖率:33.33%
已结贴  问题点数:20 回复次数:4 
Ramdom类报错,不知道问题出在哪里?
package my_first_java;
import java.util.Random;
/*****
 * 洗牌算法
 * @author xiang
 *
 */
public class MyFirstJava {

    public static void main(String[] args) {
        String [] cards = {"黑桃A","红桃2","梅花7","梅花7","方块10","黑桃9"};
        Ramdom r = new Random();
        for(int i=cards.length-1; i>=1; i--){
            int j = r.nextInt(i);//j=[0,i)
            String t = cards[i];
            cards[i] = cards[j];
            cards[j] = t;
        }
        
        System.out.println("My first Java program!");// TODO Auto-generated method stub
        System.out.print(cards);
    }

}


编译报错:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    Ramdom cannot be resolved to a type

    at my_first_java.MyFirstJava.main(MyFirstJava.java:12)
搜索更多相关主题的帖子: package public Random import xiang 
2014-03-30 21:07
hhwz
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:87
帖 子:687
专家分:4502
注 册:2011-5-22
得分:10 
Ramdom r = new Random(); // 是random 不是 ramdom

2014-03-31 11:14
日知己所无
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:38
帖 子:427
专家分:2071
注 册:2014-3-22
得分:10 
程序代码:
import java.util.*;

/**

 * ***

 * 洗牌算法

 *

 * @author xiang

 * 
*/
public class MyFirstJava {

    public static void main(String[] args) {
        String[] cards = {"黑桃A", "红桃2", "梅花7", "梅花7", "方块10", "黑桃9"};
        Random r = new Random();
        for (int i = cards.length - 1; i >= 1; i--) {
            int j = r.nextInt(i);//j=[0,i)
            String t = cards[i];
            cards[i] = cards[j];
            cards[j] = t;
        }

        System.out.println("My first Java program!");
        for (String card : cards) {
            System.out.print(card);
        }
        System.out.println();
    }

}


输出:
My first Java program!
黑桃9梅花7红桃2方块10黑桃A梅花7

My first Java program!
方块10梅花7梅花7黑桃9红桃2黑桃A

My first Java program!
红桃2梅花7黑桃9黑桃A梅花7方块10
2014-03-31 19:46
大脸猫xiang
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2014-3-30
得分:0 
哎,低级错误,谢谢楼上
2014-04-01 07:18
日知己所无
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:38
帖 子:427
专家分:2071
注 册:2014-3-22
得分:0 
不客气,给结贴就好
2014-04-02 21:07



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-429405-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.320884 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved