小编pur*_*rme的帖子

这两种随机方法有什么区别?

这是一个摇滚,纸张,剪刀游戏.我的问题是,如果我私下移动武器.长度随机r =新随机(武器.长度); 它会给我一个错误.如果我在方法中移动了weapon.length,它将成功运行.有什么不同?

 public class Game {

private String[] weapons = {"rock", "paper", "scissor"};
private Random r = new Random(weapons.length);


public void thePick() {

    System.out.println(weapons[r.nextInt()]);

 }

}
Run Code Online (Sandbox Code Playgroud)

VS

public class Game {

private String[] weapons = {"rock", "paper", "scissor"};
private Random r = new Random();


public void thePick() {

    System.out.println(weapons[r.nextInt(weapons.length)]);

 }
}
Run Code Online (Sandbox Code Playgroud)

java random

2
推荐指数
1
解决办法
190
查看次数

我试图在java中使用并学习关键字"this".在我的代码中,我收到了一个错误

这段代码的主要目标是使用这个关键字并设置全局变量(十,零,二十等于int 10,int 0,int 20.)那么我会调用该方法,它会将它们加在一起. (总价值30)

package javaapplication53;

public class NewClass {

public int ten = 10;
public int zero = 0;
public int twenty = 20;

public int yourMethod(int ten, int zero, int twenty) {



    this.ten = ten;
    this.zero = zero;
    this.twenty = twenty;

   return(ten +zero+ twenty);
}
}
Run Code Online (Sandbox Code Playgroud)

然后我在main方法中调用了构造函数.

   package javaapplication53;

    public class JavaApplication53 {


    public static void main(String[] args) {
    NewClass nc = new NewClass();
    nc.NewClass(ten, zero, twenty);
}
Run Code Online (Sandbox Code Playgroud)

}

它说我必须输入3 int,我以为我在其他类中做了.

我是计算机编程的新手

java this

1
推荐指数
1
解决办法
186
查看次数

解释代码

这是代码:

基本上,这个代码打印出52张西装+等级的牌.

package javaapplication52;

public class JavaApplication52 {

    public static void deck() {

        String[] suit = { "Clubs", "Diamonds", "Hearts", "Spades" };
        String[] rank = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack",
                "Queen", "King", "Ace" };

        // avoid hardwired constants
        int SUITS = suit.length;
        int RANKS = rank.length;
        int N = SUITS * RANKS;
        // initialize deck
        String[] deck = new String[N];
        for (int i = 0; i < RANKS; i++) {
            for (int j = …
Run Code Online (Sandbox Code Playgroud)

java arrays poker

0
推荐指数
1
解决办法
230
查看次数

标签 统计

java ×3

arrays ×1

poker ×1

random ×1

this ×1