如何仅使用数组和方法输出具有名称和分数的TABLE?

时间:2016-02-20 02:34:58

标签: java arrays

我一直试图找到这个问题的答案,但无济于事! 基本上我必须写一个程序,其中' x'玩家数量可以进入猜谜游戏并输入猜测然后得分。

然而,在他们输入猜测之后,我必须以这样的表格形式输出它" NAME GUESS SCORE"

我不知道如何使用for循环执行此操作,因为for循环println只能打印来自playersArray的值。如何在其旁边打印另一个像guessesArray这样的数组?

我只能使用数组和方法来执行此操作。

下面我将向您展示我现在拥有的东西:

import java.util.Scanner;
import java.util.Random;
import java.lang.Math;
public class game
{   
static int[] guessesArray;
static int guess;
static String [] playersArray;
static int[] currscoresArray;
static int [] addscoresArray;
static int [] finalscoresArray;
 public static void main(String [] args){
   System.out.print("Number of players? ");
   Scanner kb = new Scanner(System.in);
   int numplayers = kb.nextInt();


   //Initialize
   playersArray = new String[numplayers];
   guessesArray = new int [numplayers];

   currscoresArray = new int [numplayers];
   addscoresArray = new int [numplayers];
   finalscoresArray = new int [numplayers];

   populateArray(playersArray);
   displayMenu();
}

public static void populateArray( String[] x){
   Scanner kb = new Scanner(System.in);
    for (int i = 0; i<x.length ; i++){
        System.out.print("Enter Player "+(i+1)+": ");
        x[i]=kb.nextLine();  
   }
}

public static void displayMenu(){
    int choice=0;
    Scanner kb = new Scanner(System.in);
    String[] args = {};
    while(true){
        System.out.println("Menu ");
        System.out.println("1. Make Guess");
        System.out.println("2. List Winner");
        System.out.println("0. Exit");
        System.out.print("Enter choice: ");
        choice = kb.nextInt();
        if (choice==0){
            System.out.print("Do you want to play a new game? Y/N: ");
            String ans = kb.next();
            if (ans.equals ("Y") || ans.equals ("y")){
                main(args);
            }
            break;
        } 

        switch (choice){
            case 1: makeGuess(); break;
            case 2: listWinner(); break;
            default: System.out.println("Invalid choice");
        }
    }
     System.out.println("End of program");System.exit(0);
}

public static void makeGuess(){   
    Scanner kb = new Scanner(System.in);
    Random rand = new Random();
    int secret = rand.nextInt(10)+1;
    for (int i=0; i < guessesArray.length; i++){
       System.out.print("Enter your guess "+playersArray[i]+": ");
       guessesArray[i]=kb.nextInt();
    } 
    int diff = (int)(Math.abs(guess - secret));
    int score=0; 

    if (diff == 0){
        score=score+10;
    }else if(diff<=1){
        score=score+5;
    }else if(diff<=2){
        score=score+2;
    }
    for (int i=0; i< currscoresArray.length; i++){
        currscoresArray[i]=score;

    }
    System.out.println();
    System.out.println("Generated number is "+secret);
    System.out.println("Current Score Listing");
    System.out.println("     Name          Guess Score Added Final Score");
    System.out.println("1.   "+playersArray[0]+"     \t       "+guessesArray[0]+"  \t"+currscoresArray[0]+"");
    System.out.println("1.   "+playersArray[1]+"     \t      "+guessesArray[1]+"  \t"+currscoresArray[1]+"");
}

public static void listWinner(){
}
}

2 个答案:

答案 0 :(得分:0)

您可以使用已编入索引的循环来代替使用增强型for循环(例如for (String player : playersArray) {}

for (int i = 0; i < playersArray.length; i++) {
    String name = playerArray[i];
    double score = scoresArray[i];
}

话虽如此,你应该制作一个Player类,它包含单个玩家的所有信息,然后只有一个这种类型的数组。这样更好,不仅因为你可以使用增强的fors,而且因为你不需要确保数组总是同步,并且你的代码变得更容易理解。

答案 1 :(得分:0)

从每个数组打印时只重用一个int x变量吗?

for( int x = 0; x < playersArray.length; x++ ) {
    System.out.println( playersArray[ x ] + ”    ” + guessArray[ x ] + "    " + finalScoresArray[ x ] );
}

您已在代码中举例说明了使用单一打印方法打印3个值的示例。你使用for循环索引数组中的元素。所以梳理这两种技术不应该太难掌握。