等于不工作Java的方法

时间:2015-02-12 20:31:14

标签: java

所以我在下面的类中有我的equals方法,当if语句为真时,我似乎没有返回true!我似乎无法找出原因......

public class Player {

    private int[] anyplayer = new int[5];

    // constructor for each player at the table

    public Player(int[] anyplayer) {

        this.anyplayer = anyplayer;
    }

    // checks if the player has a winning bet in the european roulette

    public boolean europeanequals(){
        boolean truth = false;  
        for (int i = 0; i < anyplayer.length; i++) {
            if (roulettedriver.eurowinningnumber == anyplayer[i]) {
                truth = true;}
            else {
                truth = false;
            }
        }
                return truth;
    }

这是我的驱动程序,我称之为方法:

public class roulettedriver {

    // declaring the two roulettes
            final static int[] europeanroulettegame = {0,32,15,19,4,21,2,25,17,34,6,27,13,36,11,30,8,23,10,5,24,16,33,1,20,14,31,9,22,18,29,7,28,12,35,3,26};
            final static int[] americanroulettegame = {0,28,9,26,30,11,7,20,32,17,5,22,34,15,3,24,36,13,1,00,27,10,25,29,12,8,19,31,18,6,21,33,16,4,23,35,14,2};

   // declaring the two winning numbers
            public static int eurowinningnumber = europeanroulette.getRandom(europeanroulettegame);
            public static int uswinningnumber = americanroulette.getRandom(americanroulettegame);


    public static void main(String[] args) {


        Scanner keyin = new Scanner(System.in); 

            // initializing the six players (First player) 
        int[] player1 = {-1,-1,-1,-1,-1}; // the numbers are set to -1 because 0 is a winning number
         Player first_player = new vipplayer(player1); // First player is automatically a VIP
            try{

               for(int i=0;i<=5;i++) {

                   player1[i] = Integer.parseInt(keyin.nextLine());

                   }
               }

           catch(NumberFormatException e){  

               System.out.println("Player 2 : ");  
           }    

            // booleans are set to true if the bets match the randomly generated number

                        boolean winbet1 = first_player.europeanequals();

基本上我的平等并不是比较我认为正确的价值......似乎无法使这项工作成功吗?有什么输入?该值应该在布尔值winbet1

中返回

3 个答案:

答案 0 :(得分:8)

您的代码即使在找到匹配项后仍会继续,可能会将truth重置为false并返回该代码。

您需要更改方法:

public boolean europeanequals(){
    for (int i = 0; i < anyplayer.length; i++) {
        if (roulettedriver.eurowinningnumber == anyplayer[i]) {
            return true;
        }
    }
    return false;
}

或使用for-each循环:

public boolean europeanequals(){
    for (int number : anyplayer) {
        if (roulettedriver.eurowinningnumber == number) {
            return true;
        }
    }
    return false;
}

答案 1 :(得分:0)

您可能忘记了break

if (roulettedriver.eurowinningnumber == anyplayer[i]) {
    truth = true;
    break; // here
}

没有它,循环将继续并可能将truth设置为假

答案 2 :(得分:0)

当其他人回答时,我想补充一些注意事项:

  • 数组在Java中从零开始,此代码:

    for(int i=0;i<=5;i++) { player1[i] = Integer.parseInt(keyin.nextLine()); }

    会抛出异常,你应该循环到4(或更好,plater1.length

  • 关注Java Naming Conventions并将您的课程重命名为大写字母,同时尝试将您的变量命名为firstPlayer而不是first_player

  • 缩进代码以获得更好,更安全的世界