【问题标题】:bad operand types for binary operator+(JAVA PROGRAM)二元运算符的错误操作数类型(JAVA PROGRAM)
【发布时间】:2016-05-06 03:27:42
【问题描述】:

我正在用 java 做我的课堂实验室。

这个程序是通过总分相加来输出获胜的球队。 (输入文件中有蓝队和白队,有成员的名字和每个成员的分数。)

二元运算符的操作数类型错误。(sumArray) 我认为这是因为 int 类型 + int []。有人可以帮帮我吗?如果您发现任何其他问题,请告诉我。我猜除了这个还有很多错误。

import java.io.*;
import java.util.*;

// declaration of the class
public class Bowling
{

// declaration of main program 
public static void main(String[] args) throws FileNotFoundException
{

// 1. connect to input file
Scanner fin = new Scanner(new FileReader("bowling.txt"));

    // declare arrays below
String Team, Member;
int teamw, teamb, Score;

    // 2) initialize array accumulators to zero
    teamw=0;
    teamb=0;
    int [] white_score = new int[3]; 
    String [] white_member = new String[3]; 
    int [] blue_score = new int[3];
    String [] blue_member = new String[3];
    // 3) display a descriptive message
System.out.println(
        "This program reads the lines from the file bowling.txt to determine\n"
        + "the winner of a bowling match.  The winning team, members and scores\n"
           + "are displayed on the monitor.\n");

    // 4) test Scanner.eof() condition
    while (fin.hasNext())
       {
       // 5) attempt to input next line from file
       Member = fin.next();
       Team = fin.next();
       Score = fin.nextInt();
       // 6) test team color is blue
       // 7) then store blue member and score
       // 8) increase blue array accumulator
       // 9) else store white member and score
        // 10) increase white array accumulator
       if (Team.equals("Blue"))
       {
           blue_member[teamb]=Member;
           blue_score[teamb] = Score;
           teamb++;
       }
       else
       {
           white_member[teamw]= Member;
           white_score[teamw]= Score;
           teamw++;
       }
    }

    // 11) if blue team score is larger
    // 12) then display blue team as winner
    // 13) else display white team as winner
    if(sumArray(blue_score)>sumArray(white_score))
    {
        printArray("Blue", blue_member, blue_score);
    }
    else
    {
        printArray("White", white_member, white_score);
    }

    // 14 disconnect from the input file
fin.close();
}
public static int sumArray(int[] Score)
{
    int sum=0;

    for ( int i=0; i<Score.length; i++)
     sum = sum+Score;           ////HERE IS problem!!!!!!

    return sum;
}
public static void printArray(String Team, String[] Member, int[] Score)
    {
    for(int i=0; i<Member.length; i++)
    System.out.printf("winning team:"+Team+"\n"+Member+":"+Score);
    }
}

【问题讨论】:

    标签: java arrays


    【解决方案1】:

    只需使用:

    sum += Score[i];
    

    for循环中添加数组元素。

    您收到错误是因为您无法将 int 添加到数组中。在 Java 中,您不能将 + 运算符直接用于数组。

    【讨论】:

    • 谢谢!这里有更多问题。用 Score[i] 修复后,没有语法错误。但是,运行程序会产生很多奇怪的字符。(比如.. 获胜团队:Blue [Ljava.lang.String;@63fd26:[I@c0235awinning team:Blue [Ljava.lang.String;@63fd26:[I@c0235awinning team:Blue [Ljava.lang.String;@63fd26:[I@c0235a) 什么问题??
    • @JustinJuntaeKim 当您尝试打印数组时会发生这种情况。您必须对其进行迭代并单独打印每个元素。
    • 正如 Bethany Louise 指出的那样,您应该遍历数组中的每个元素以正确字符串化数组的元素,或者如果您不想迭代,请使用 List&lt;Integer&gt;
    • @BethanyLouise 我知道了。这是第一次看到java。我这周才开始学习数组。如果我一遍又一遍地看,我希望它会变得更好;)
    猜你喜欢
    • 2014-05-30
    • 1970-01-01
    • 2022-07-01
    • 2018-10-02
    • 1970-01-01
    • 2016-06-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多