程序打印太多

时间:2016-07-24 00:54:02

标签: java programming-languages

`在这里输入代码:

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[i]+":"+Score[i]);
        {

您好!我的程序一直在输出

获奖团队:蓝色 弗雷德:20 获奖团队:蓝色 希尔达:24 获奖团队:蓝色 帕特:51

任何帮助都将非常感谢我是java编程的新手!

2 个答案:

答案 0 :(得分:1)

您想要删除

"Winning team"+Team+"\n"
来自for循环的

(在上面的行上打印)。其余的可以保持不变(除了在'+"\n"之后添加Score[i])以循环其余部分。

答案 1 :(得分:0)

如果您的文件bowling.txt看起来像这样

Fred Blue 20 Hilda Blue 24 Pat Blue 51

然后您可以使用以下Java代码来读取和输出它。

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

public class Main {

    public static void main(String[] args) throws FileNotFoundException {

        Scanner fin = new Scanner(new FileReader("bowling.txt"));

        String Team, Member;
        int teamw, teamb, Score;
        String[] blue_members = new String[3];
        String[] white_members = new String[3];
        int[] blue_scores = new int[3];
        int[] white_scores = new int[3];


        int b = 0;
        int w = 0;

        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");


        while (fin.hasNext()) {

            Member = fin.next();
            Team = fin.next();
            Score = fin.nextInt();

            if (Team.equals("Blue")) {


                blue_members[b] = Member;
                blue_scores[b] = Score;


                b++;
            } else {
                white_members[w] = Member;
                white_scores[w] = Score;

                w++;
            }
        }

        if (sumArray(blue_scores) > sumArray(white_scores)) {
            printArray("Blue", blue_members, blue_scores);
        } else {
            printArray("White", white_members, blue_scores);
        }

        fin.close();

    }


    public static int sumArray(int[] Score) {
        int sum = 0;
        for (int i = 0; i < Score.length; i++)
            sum += Score[i];
        return sum;
    }

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

输出

This program reads the lines from the file bowling.txt to determine
the winner of a bowling match.  The winning team, members and scores
are displayed on the monitor.

Winning team:Blue

Player Score

Fred:20
Hilda:24
Pat:51

Process finished with exit code 0