2016-02-19 10 views
0

我一直在想這個爲期2天,我仍然不知道如何讓它工作。JAVA - 如何使用沒有參數的方法將用戶輸入保存到數組中?

基本上這是一個遊戲,其中多個玩家可以玩,它有一個菜單,也顯示高分。

當人按下選項1後,它應該加載makeGuess();然後能夠輸入他的猜測來獲得分數。但我無法將這些輸入保存到數組中。我還能如何做到這一點?

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

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: System.out.println("Option 2 selected"); 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; 
    int guess = kb.nextInt(); 
    int diff = (int)(Math.abs(guess - secret)); 
    int score=0; 
    for (int i=0; i < x.length; i++){ 
     System.out.print("Enter your guess player "+(i+1)+": "); 
     x[i]=kb.nextInt(); 
    } 
    if (diff == 0){ 
     score=score+10; 
    }else if(diff<=10){ 
     score=score+5; 
    }else if(diff<=20){ 
     score=score+2; 
    } 
    for (int i=0; i<x.length; i++){ 
     x[i]=score; 

    } 
} 

回答

0

你可以聲明你的數組在類,並使其static;

static int[] guessesArray; 

然後,在你的main方法中,初始化它。

並從你的makeGuess()方法,我猜你需要將它存儲在一個數組x。只需將其更改爲數組guessesArray

+0

嗨感謝您的答案,其正是我想要的。 但是現在我有另一個問題。如果我現在可以將值保存在數組中,那麼如何以「名稱猜分數」的形式輸出值,因爲即使我使用for循環,我只能循環1個數組,但是我不能將整數在旁邊? – ShaoQi

+0

@ShaoQi如果所有數組的大小相同,那麼使用一個控制變量就可以得到所有數組的值。但是,如果我確切知道如何輸出,我將只能給你正確的答案。 – Hackerdarshi

+0

我明白了!非常感謝回覆。 – ShaoQi

0

一個處理這個問題的方法是從你的類的主要領域讓你的變量,讓你的程序附帶了必要的修改。

0

您可以將方法,以便它需要一個數組作爲參數:

public static void makeGuess(int[] x){ 
    //Your current code 
} 

如果你真的不想要的方法有任何參數,你需要移動你的聲明中guessesArray外主要方法,然後在您調用您的方法之前在主要方法內初始化它:

public class game { 
    static int[] guessesArray; 

    public static void main(String[] args) { 
     System.out.print("Number of players? "); 
     Scanner kb = new Scanner(System.in); 
     int numplayers = kb.nextInt(); 
     String [] playersArray = new String[numplayers]; 

     //Initialize it here 
     guessesArray = new int [numplayers]; 

     //Rest of your code 

     //Call your function with no parameter 
     makeGuess(); 
    } 
} 
相關問題