使用数组生成球员得分

时间:2017-12-07 16:27:27

标签: java

我尝试使用数组方法来跟踪游戏中的分数,当我尝试编译和运行时,我很难这样做。

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

public class score
{

    public static void main (String[]args)
    {

    Scanner scan = new Scanner(System.in);

    int [] arrPlayer = new int[7];

    System.out.println("Please Enter Your Score");
    arrPlayer[0]=scan.nextInt();
    System.out.println("Please Enter Your Score");
    arrPlayer[1]=scan.nextInt();
    System.out.println("Please Enter Your Score");
    arrPlayer[2]=scan.nextInt();
    System.out.println("Please Enter Your Score");
    arrPlayer[3]=scan.nextInt();
    System.out.println("Please Enter Your Score");
    arrPlayer[4]=scan.nextInt();
    System.out.println("Please Enter Your Score");
    arrPlayer[5]=scan.nextInt();
    System.out.println("Please Enter Your Score");
    arrPlayer[6]=scan.nextInt();

    System.out.println("Player 1 : Score: "arrPlayer[0];
    System.out.println("Player 1 : Score: "arrPlayer[1];
    System.out.println("Player 1 : Score: "arrPlayer[2];
    System.out.println("Player 1 : Score: "arrPlayer[3];
    System.out.println("Player 1 : Score: "arrPlayer[4];
    System.out.println("Player 1 : Score: "arrPlayer[5];
    System.out.println("Player 1 : Score: "arrPlayer[6];

    for(int i=0;i<7;i++);

    }

}

5 个答案:

答案 0 :(得分:4)

你错过了+将数组值与字符串连接起来。而且你也没有关闭System.out.println()方法。

以下是更正:

    System.out.println("Player 1 : Score: " + arrPlayer[0]);
    System.out.println("Player 1 : Score: " + arrPlayer[1]);
    System.out.println("Player 1 : Score: " + arrPlayer[2]);
    System.out.println("Player 1 : Score: " + arrPlayer[3]);
    System.out.println("Player 1 : Score: " + arrPlayer[4]);
    System.out.println("Player 1 : Score: " + arrPlayer[5]);
    System.out.println("Player 1 : Score: " + arrPlayer[6]);

奖金:

您可以使用循环进行以下更改来简化代码。

for (int i = 0; i < 7; i++) {
    System.out.println("Please Enter Your Score");
    arrPlayer[i] = scan.nextInt();
}

for (int i = 0; i < 7; i++) {
    System.out.println("Player 1 : Score: " + arrPlayer[i]);
}

答案 1 :(得分:0)

我不熟悉Java,但您可以使用GVariantBuilder* builder = g_variant_builder_new(G_VARIANT_TYPE("a(ia(s))")); for (int i = 0; i < count; i++) { GVariantBuilder* builderStr = g_variant_builder_new(G_VARIANT_TYPE("a(s)")); for (int j = 0; j < nr_of_string; j++) { g_variant_builder_add(builderStr, "(s)", "Test"); } g_variant_builder_add(builder, "(ia(s))", 32, **builderStr**); } GVariant* retVariant = g_variant_builder_end(builder); 循环缩短它:

for

答案 2 :(得分:0)

你的问题是你没有将球员得分与文字联系起来:

System.out.println("Player 1 : Score: " + arrPlayer[0]);

此外,为什么不循环收集他们的分数,而不是费力地输入相同的声明:

int [] arrPlayer = new int[7];
for(int i=0; i<arrPlayer.length;i++){
    System.out.println("Please Enter Your Score");
    arrPlayer[i] = scan.nextInt();
}

答案 3 :(得分:0)

你可以这样做

  for(int i=0; i<7; I++){
      System.out.println("player            score1 :"+arrayplayer[i]);
     }

答案 4 :(得分:0)

for(int i = 0; i&lt; 7; i ++){System.out.println(&#34;玩家得分:&#34; + arrayplayer [i]);}