2015-05-30 47 views
0

我想在我的遊戲中做一個for循環,使得當猜數的數量加起來後,它進入for循環,並給出正確的猜測數。這是爲了我的計算機科學課,我們必須將for循環合併到程序中。For循環在我的策劃遊戲

if (guess1 == Secret_code[0]) { 
    System.out.println(" You have guessed one correctly in the right spot!"); 
} 

if (guess2 == Secret_code[1]) { 
    System.out.println(" You have guessed one correctly in the right spot!"); 
} 

if (guess3 == Secret_code[2]) { 
    System.out.println(" You have guessed one correctly in the right spot!"); 
} 

if (guess4 == Secret_code[3]) { 
    System.out.println(" You have guessed one correctly in the right spot!"); 
} 

do { 
    System.out.println("What is the number one peg in my code???"); 
    guess1 = Guess.nextInt(); 
    System.out.println("What is the number two peg in my code???"); 
    guess2 = Guess.nextInt(); 
    System.out.println("What is the number three peg in my code???"); 
    guess3 = Guess.nextInt(); 
    System.out.println("What is the number four peg in my code???"); 
    guess4 = Guess.nextInt(); 
    if (guess1 == Secret_code[0]) { 
     System.out.println(" You have guessed one correctly in the right spot!"); 
    } 
    if (guess2 == Secret_code[1]) { 
     System.out.println(" You have guessed one correctly in the right spot!"); 
    } 
    if (guess3 == Secret_code[2]) { 
     System.out.println(" You have guessed one correctly in the right spot!"); 
    } 
    if (guess4 == Secret_code[3]) { 
     System.out.println(" You have guessed one correctly in the right spot!"); 
    } 
} 
while ((guess1 != Secret_code[0]) & (guess2 != Secret_code[1]) & (guess3 != Secret_code[2]) & (guess4 != Secret_code[3]));  

public static void CorrectGuess(int[] Secret_code) { 
    //Variables 
    int Correctspot; 

    for (Secret_code[0] = 0; Secret_code[0] < 3; Secret_code[0]++) 
     Correctspot = +1; 
} 
+3

有什麼問題? –

+0

沒有問題....只需要添加for循環,可以計算正確的猜測數 – Sot

+1

本網站不適用於代碼請求。當你遇到問題時,你會來到這裏,通常會展示你對這個主題的嘗試或任何研究。如果你不瞭解情況,請告訴你的老師,因爲他們是那些得到報酬以確保你知道這些東西的人,如果他們不知道你是否有什麼東西,就不能正確教導你不明白 –

回答

1

我會告訴你只是一個樣本,因爲像他們說的不是我們的工作,但我覺得不好,因爲他們的一些不好的老師救出來。我也將建立一個數組,因爲這將解決所有的問題,你可能會遇到

//make a Scanner obj 
Int[] guessArray = {10, 20, 30, 5};  
int useranswer = 0; 
int loopCount = 0; 

while(loopCount < 4) { 
    System.out.println("blalalala for Guess #" + (loopCount+1)); 
    useranswer = input.nextInt(); 

    if(useranswer == guessArray[loopCount]) { 
     loopCount++; 
    }//end of if 

}//end of while loop 

具有數組列表將有助於保持一切爲了和你能這個循環之後辭職的值在後面的程序。你總是需要小心while循環,因爲如果代碼錯誤,最終可能會導致計算機崩潰。

我也會查找for循環,因爲根據您未來的問題,forwhile可能工作得最好。

編輯過的編碼風格[1]:注意:我通過間隔排列所有內容,因爲它幫助我查看所有內容,如果這是一個問題,請讓我知道。所以我可以在後面更改它