Java 阵列计数器输出故障

Java 阵列计数器输出故障,java,arrays,Java,Arrays,我是Java新手,我有一个程序,玩家猜一件物品的价格,准确的价格是先输入的。我有一个名为winnerCount的数组,它跟踪每个玩家赢得的回合数。我想使用JOptionPane.showMessageDialog输出这个,部分代码涉及根据赢得的回合数给每位玩家一个奖品。因此,对于我的伪代码,如果roundsWon=1,则赢得的奖金为:15125美元。当我在Java中尝试同样的想法时,它给了我一个不可比较的错误类型,因为它比较int和int[]。由于Java不能比较不同的数据类型,我可以通过什么方

我是Java新手,我有一个程序,玩家猜一件物品的价格,准确的价格是先输入的。我有一个名为winnerCount的数组,它跟踪每个玩家赢得的回合数。我想使用JOptionPane.showMessageDialog输出这个,部分代码涉及根据赢得的回合数给每位玩家一个奖品。因此,对于我的伪代码,如果roundsWon=1,则赢得的奖金为:15125美元。当我在Java中尝试同样的想法时,它给了我一个不可比较的错误类型,因为它比较int和int[]。由于Java不能比较不同的数据类型,我可以通过什么方式输出期望的结果winnerCount和每位玩家赢得的奖品

这就是我的工作

import javax.swing.JOptionPane;
import java.lang.Math;
import java.util.*;

public class priceIsRight {

 public static void main(String[] args) {

  double [] numPlayers = new double [4];
  double exactPrice = getExactPrices();
  double [] guessPrice = getGuessPrices(exactPrice);
  int roundsWon = getRoundsWon(guessPrice, exactPrice);
  int numRounds = 0;
  int [] winnersCount = new int [4];
  int roundWinner = getRoundsWon(guessPrice, exactPrice); 
  int prizesWon = calculatePrizesWon(winnersCount, roundWinner);

  }

 public static int getRoundsWon(double[] guessPrice, double exactPrice) {

    int roundWinner = getRoundsWon(guessPrice, exactPrice);
    int [] winnersCount = new int [4];
    int numRounds = 0;
    do {
       double minValue = Math.abs(exactPrice-guessPrice[0]);
       roundWinner = 0;
       for (int i=1;i < guessPrice.length; i++) {
           double diff = Math.abs(exactPrice-guessPrice[i]);
           if (diff<minValue) {
               minValue=diff;
               roundWinner=i;
           }
           winnersCount[roundWinner]++;
       }
       }
       while (numRounds <=3);
       return roundWinner;

   }


//Outputs result
     public static int calculatePrizesWon(int [] winnersCount, int roundWinner) {

     int prizesWon = 0;
     if (winnersCount = 0) {
        JOptionPane.ShowMessageDialog(null, "Prize won: $106 consolidation prize!");
        }
     else if (winnersCount = 1) {    
     JOptionPane.ShowMessageDialog(null, "Prize won: $15,125!");
        } 
     else if (winnersCount = 2) {    
     JOptionPane.ShowMessageDialog(null, "Prize won: $30,110!");
        } 
     else if (winnersCount = 3) {    
     JOptionPane.ShowMessageDialog(null, "Prize won: $15,120,000!");
        } 

    }
}

您需要指定要比较的阵列中的哪个位置。试试这个:

public static int calculatePrizesWon(int[] winnersCount, int roundWinner) {

    int prizesWon = 0;
    if (winnersCount[roundWinner] == 0) {
        JOptionPane.showMessageDialog(null, "Prize won: $106 consolidation prize!");
    } else if (winnersCount[roundWinner] == 1) {
        JOptionPane.showMessageDialog(null, "Prize won: $15,125!");
    } else if (winnersCount[roundWinner] == 2) {
        JOptionPane.showMessageDialog(null, "Prize won: $30,110!");
    } else if (winnersCount[roundWinner] == 3) {
        JOptionPane.showMessageDialog(null, "Prize won: $15,120,000!");
    }
    return prizesWon; // Replace this with the real prize won number.
}

使用switch语句可能会做得更好。如果您想查看或接收有关其他内容的评论,您可以更好地发布评论。

此代码中有些内容没有意义。例如,为什么要将numPlayers设置为双[]?这不是更有意义吗

int numPlayers = 4;
对于你的赢家数,这是否存储了每个玩家赢了多少回合?如果是这样,那么我假设您的calculatePrizesWon方法应该迭代每个玩家,并输出他们的奖品

差不多

public static int calculatePrizesWon(int [] winnersCount, int roundWinner) 
{
    int prizesWon = 0;

    for (int i = 0; i < winnersCount.length; i++)
    {
        if (winnersCount[i] == 0) 
        {
            JOptionPane.ShowMessageDialog(null, "Player "+i+" - Prize won: $106 consolidation prize!");
        }
        else if (winnersCount[i] == 1) 
        {    
            JOptionPane.ShowMessageDialog(null, "Player "+i+" - Prize won: $15,125!");
        } 
        else if (winnersCount[i] == 2) 
        {    
            JOptionPane.ShowMessageDialog(null, "Player "+i+" - Prize won: $30,110!");
        } 
        else if (winnersCount[i] == 3) 
        {    
            JOptionPane.ShowMessageDialog(null, "Player "+i+" - Prize won: $15,120,000!");
        } 
    }
}
另外,您传递给calculatePrizesWon的回合赢家参数是什么? prizesWon变量的作用是什么?例如,如果一个玩家赢了三轮,那么他们不是也赢了三个奖吗?那么,赢家计数和大奖赛有什么区别呢?
我会尽力帮助你,但你需要更清楚地解释一下你在做什么。

在你的calculatePrizesWon方法中,当你应该使用==相等运算符时,你正在使用=赋值运算符。我也尝试过,但它仍然给我相同的错误。似乎我们不明白是只有roundWinner获得奖品,还是所有玩家都获得奖品。很抱歉,但你所说的切换语句是什么意思?这将显示每个玩家赢得的回合,对吗?A是一种结构,允许你将一个值与许多其他值进行比较,并在满足其中一个条件时进行操作。它基本上是许多if-else的替代品,不会影响功能。我的解决方案对你有用吗?它至少不再给你一个错误了吗?还有什么我能做得更好的呢?我现在唯一的错误是它需要一个返回语句。我可以将这个方法变成一个空的,这样它就会输出我想要的结果,但是我会失去calculatePrizesWon方法,因为我无法传递参数。如何将此方法返回到main中?当我尝试使用int-numPlayer时,我得到一个错误,说它需要double,但找到int,所以我将其更改为double。roundWinner是一种确定哪个玩家赢了多少轮的方法。我忘了编辑国际大奖赛了对不起!