2015-05-25 4 views
-2

Я пишу основную игрушку, пытаясь научить себя Java. Я пытаюсь сделать это наиболее эффективным способом, и, пытаясь избежать повторения, мне нужно знать, как увеличивать массив String каждый раз, когда мои циклы кода. Как и в [0] ++ или, тем не менее, вы переходите к следующей строке в массиве. Это возможно?Как я могу увеличить свой массив строк?

package Base; 

import java.util.Scanner; 

class Game { 

public static void main(String[] args){ 

    Data gameData = new Data(); 



    Scanner input = new Scanner(System.in); 

    int score = 100; 
    int x = 50; 
    int position = 0; 

     System.out.println("Unscramble the state Capitals\n");  
     System.out.println(gameData.Qs[0]); 
      // start  


    do 
     { 

     String Answer = input.nextLine(); 

    if(!gameData.As[0].equals(Answer)) 
     {System.out.println(gameData.Hint[0]); 
     score = (score-1); 
    Answer = input.nextLine();} 

    if(!gameData.As[0].equals(Answer)) 
     {System.out.println("Lansing is the capital of Michigan\n"); 
     score = (score-1); 
     System.out.println(gameData.Qs[1]);} 
    else 
     {System.out.println("Correct!\n"); 
     System.out.println(gameData.Qs[1]);} 
    x--;} 
    while(x>0); 
    if(x==0) 
     System.out.println("your score is " + score); 




} 
} 

package Base; 

public class Data { 

    String[] As = new String[50]; 


    String[] Qs = new String[50]; 

    String[] Hint = new String[50]; 



    public String[] getAs() {return As;} 

    public String[] getQs() {return Qs;} 

    public String[] getHint() {return Hint;} 

    { 


    Qs[0] = "NNLSIGA"; 
    Qs[1] = "INNOAPASL"; 
    Qs[2] = "BLYNAA"; 
    Qs[3] = "OAIIDNLPAISN"; 
    Qs[4] = "HLLSAEAETSA"; 
    Qs[5] = "NDROOCC"; 
    Qs[6] = "NOUHLULO"; 
    Qs[7] = "PIMOYLA"; 
    Qs[8] = "ERERPI"; 
    Qs[9] = "AHEENL"; 
    Qs[10] = "IOEBS"; 
    Qs[11] = "LAKHMAOO ICTY"; 
    Qs[12] = "ATAATNL"; 
    Qs[13] = "POTEAK"; 
    Qs[14] = "BUCOMSUL"; 
    Qs[15] = "LNOCLIN"; 
    Qs[16] = "USINAT"; 
    Qs[17] = "TALS EKAL YTCI"; 
    Qs[18] = "NETONRT"; 
    Qs[19] = "NECTALOHSR"; 
    Qs[20] = "UAUSTAG"; 
    Qs[21] = "OSBNOT"; 
    Qs[22] = "KCANSOJ"; 
    Qs[23] = "OTASMRACNE"; 
    Qs[24] = "FFAONTRRK"; 
    Qs[25] = "ELAIGHR"; 
    Qs[26] = "NEUJAU"; 
    Qs[27] = "VREOD"; 
    Qs[28] = "SFRONJFEE IYTC"; 
    Qs[29] = "ILUNSTPAA"; 
    Qs[30] = "VLAHNLISE"; 
    Qs[31] = "ENRVED"; 
    Qs[32] = "XPNEIOH"; 
    Qs[33] = "KABSCMIR"; 
    Qs[34] = "GBSIRRRAHU"; 
    Qs[35] = "HRAFDORT"; 
    Qs[36] = "TNIELOMPER"; 
    Qs[37] = "ISADMNO"; 
    Qs[38] = "IENFPLRSDIG"; 
    Qs[39] = "OAUCIBLM"; 
    Qs[40] = "CYHNNEEE"; 
    Qs[41] = "SLMAE"; 
    Qs[42] = "EONVIRCDEP"; 
    Qs[43] = "OHRDNIMC"; 
    Qs[44] = "MGYNTROEOM"; 
    Qs[45] = "SED SMOEIN"; 
    Qs[46] = "OCNASR CTIY"; 
    Qs[47] = "NATOB GROEU"; 
    Qs[48] = "STAAN EF"; 
    Qs[49] = "TELLTICKOR"; 

    As[0] = "Lansing"; 
    As[1] = "Annapolis"; 
    As[2] = "Albany"; 
    As[3] = "Indianapolis"; 
    As[4] = "Tallahassee"; 
    As[5] = "Concord"; 
    As[6] = "Honolulu"; 
    As[7] = "Olympia"; 
    As[8] = "Pierre"; 
    As[9] = "Helena"; 
    As[10] = "Boise"; 
    As[11] = "Oklahoma City"; 
    As[12] = "Atlanta"; 
    As[13] = "Topeka"; 
    As[14] = "Columbus"; 
    As[15] = "Lincon"; 
    As[16] = "Austin"; 
    As[17] = "Salt Lake City"; 
    As[18] = "Trenton"; 
    As[19] = "Charleston"; 
    As[20] = "Augusta"; 
    As[21] = "Boston"; 
    As[22] = "Jackson"; 
    As[23] = "Sacramento"; 
    As[24] = "Frankfort"; 
    As[25] = "Raleigh"; 
    As[26] = "Juneau"; 
    As[27] = "Dover"; 
    As[28] = "Jefferson City"; 
    As[29] = "Saint Paul"; 
    As[30] = "Nashville"; 
    As[31] = "Denver"; 
    As[32] = "Phoenix"; 
    As[33] = "Bismark"; 
    As[34] = "Harrisburg"; 
    As[35] = "Hartford"; 
    As[36] = "Montpelier"; 
    As[37] = "Madison"; 
    As[38] = "Springfield"; 
    As[39] = "Columbia"; 
    As[40] = "Cheyenne"; 
    As[41] = "Salem"; 
    As[42] = "Providence"; 
    As[43] = "Richmond"; 
    As[44] = "Montgomery"; 
    As[45] = "Des Moines"; 
    As[46] = "Carson City"; 
    As[47] = "Baton Rouge"; 
    As[48] = "Santa Fe"; 
    As[49] = "Little Rock"; 

    Hint[0] = "Capital of The Great Lakes State"; 
    Hint[1] = "Capital of The Old Line State"; 
    Hint[2] = "Capital of The Empire State"; 
    Hint[3] = "Capital of The Hoosier State"; 
    Hint[4] = "Capital of The Sunshine State"; 
    Hint[5] = "Capital of The Granite State"; 
    Hint[6] = "Capital of The Aloha State"; 
    Hint[7] = "Capital of The Evergreen State"; 
    Hint[8] = "Capital of Mount Rushmore State"; 
    Hint[9] = "Capital of The Treasure State"; 
    Hint[10] = "Capital of The Gem State"; 
    Hint[11] = "Capital of The Sooner State"; 
    Hint[12] = "Capital of The Peach State";         
    Hint[13] = "Capital of The Sunflower State"; 
    Hint[14] = "Capital of The Buckeye State"; 
    Hint[15] = "Capital of The Cornhusker State"; 
    Hint[16] = "Capital of The Lone Star State"; 
    Hint[17] = "Capital of The Beehive State"; 
    Hint[18] = "Capital of The Garden State"; 
    Hint[19] = "Capital of The Mountain State"; 
    Hint[20] = "Capital of The Pine Tree State";   
    Hint[21] = "Capital of The Bay State"; 
    Hint[22] = "Capital of The Magnolia State"; 
    Hint[23] = "Capital of The Golden State"; 
    Hint[24] = "Capital of The Bluegrass State"; 
    Hint[25] = "Capital of The Tar Heel State"; 
    Hint[26] = "Capital of The Last Frontier"; 
    Hint[27] = "Capital of The First State"; 
    Hint[28] = "Capital of The Show Me State"; 
    Hint[29] = "Capital of The North Star State"; 
    Hint[30] = "Capital of The Volunteer State"; 
    Hint[31] = "Capital of The Centennial State"; 
    Hint[32] = "Capital of The Grand Canyon State"; 
    Hint[33] = "Capital of The Peace Garden State"; 
    Hint[34] = "Capital of The Keystone State"; 
    Hint[35] = "Capital of The Constitution State"; 
    Hint[36] = "Capital of The Green Mountain State"; 
    Hint[37] = "Capital of The Badger State"; 
    Hint[38] = "Capital of Prairie State"; 
    Hint[39] = "Capital of The Palmetto State"; 
    Hint[40] = "Capital of The Equality or Cowboy State"; 
    Hint[41] = "Capital of The Beaver State"; 
    Hint[22] = "Capital of The Ocean State"; 
    Hint[43] = "Capital of The Old Dominion State"; 
    Hint[44] = "Capital of Yellowhammer State"; 
    Hint[45] = "Capital of The Hawkeye State"; 
    Hint[46] = "Capital of The Silver State"; 
    Hint[47] = "Capital of The Pelican State"; 
    Hint[48] = "Capital of The Land of Enchantment"; 
    Hint[49] = "Capital of The Natural State"; 



} 
} 
+0

* Как увеличить размер массива 'String' каждый раз, когда мои кодовые контуры * Что вы подразумеваете под приращением массива ? –

+0

Ваш вопрос непонятен, добавьте больше объяснений к нему – silentprogrammer

ответ

0

Что я рекомендую вводит переменную,

private Integer hintCounter = 0; 

И изменения GETHINT этому

public String getHint() { 
    return Qs[hintCounter]; 
} 

Тогда приращением ваш hintCounter в коде сразу после который вызывает метод. Кроме того, вы не должны жестко кодировать значения массива, вы должны использовать текстовый файл и выполнять некоторые базовые функции ввода-вывода, чтобы добавить их в массив.

1

Вам необходимо создать ссылку на текущий элемент массива. Например. int current = 0;

Затем увеличивайте текущее значение с каждым циклом массива: current ++;

Вы можете ссылаться на элемент массива с помощью Qs [current];

Смежные вопросы