2013-03-30 74 views
0

我需要幫助我是Java編程的新手,我不知道如何修復我的代碼。Java while循環和其他

我想做一個007遊戲。我創建了if語句,並且不會循環。如果我在do - while循環中的每個語句前添加一個!,它將導致無限循環。

我該如何解決我的編程問題。

import java.util.Scanner; 
import java.util.Random; 

public class DoubleOSeven { 

public static void main(String[] args) { 

    System.out.println("Let's Play a game of 007 "); 
    System.out.println("You can SHOOT, BLOCK, AND RELOAD"); 
    System.out.println("Both you and I start with one bullet"); 

    Scanner input = new Scanner(System.in); 

    System.out.println("Let's Start, Enter (shoot, reload , or block) "); 
    String INput = input.nextLine(); 

    //User and Computer ammo 
    int Userammo = 1; 
    int ammo = 1; 

    //Creates a random number between 1 and 3 
    Random rand = new Random(); 
    int output = rand.nextInt(3) + 1; 


    do{ 
    //User chooses shoot 
    if(INput.equals("shoot")){ 
     Userammo --; 
     System.out.println("You took a shot, Your ammo count is at: " + Userammo); 

    //User chooses reload 
    }else if (INput.equals("reload")){ 
     Userammo ++; 
     System.out.println("You reloaded, Your ammo count is at: " + Userammo); 

    //User chooses block  
    }else if(INput.equals("block")){ 
     System.out.println("You blocked, Your ammo count is at: " + Userammo); 

    //If random is 1 shoot 
    }if(output == 1){ 
     ammo ++; 
     System.out.println("I took a shot at you, My ammo count is at: " + ammo); 

    //If random is 2 block 
    }else if(output == 2){ 
     System.out.println("I blocked, My ammo count is at: " + ammo); 

    //If random is 3 reload 
    }else if(output == 3){ 
     ammo ++; 
     System.out.println("I reloaded, My ammo count is at: " + ammo); 

    //If both User and Computer shoot 
    }if(output == 1 && INput == "shoot"){ 
     System.out.println("It's a tie you we both die"); 

    } 
    }while((output == 3 && INput == "shoot") && (output == 1 && INput == "reload") && (output == 1 && INput == "shoot")); 

    } 
} 

回答

0

首先:爲了使循環運行良好,對用戶和計算機隨機決定的問題必須在循環內部。通過這種方式,每個循環都有一組新的變量值。

// Declare variables 
String INput = ""; 
Random rand = new Random(); 
int output = 0; 

do{ 
    // Ask user 
    System.out.println("Enter (shoot, reload , or block) "); 
    INput = input.nextLine(); 

    // Computer decision 
    output = rand.nextInt(3) + 1; 

    ... 

第二:你需要有一個明確的決定何時終止循環(即遊戲)。這可以通過以下

  • 用戶想要結束比賽(需要在用戶選擇多一個選擇quit
  • 計算機要結束遊戲(需要第四隨機之一來完成這意味着quit)。
  • 從用戶/電腦彈藥和用戶/電腦拍攝的組合中的一些條件。如果某位玩家被射門並且沒有彈藥,那麼你可以結束比賽。

在所有這些情況下,您需要

  • 申報的比賽結束一個變量的循環
  • 決定如何在循環
  • 做之前繼續循環如果不能結束遊戲

的這是該片段:

boolean endOfGame = false; 
... 
do { 
    ... 
    if ( INput.equals("quit") 
     || (INput.equals("shoot") && ammo == 0) 
     || (Userammo == 0 && output == 1)) { 
     endOfGame = true; 
    } 
    ... 
} while (!endOfGame); 

如果您在while, 中做出決定,您可能會有相同的效果,但這樣遊戲結束的決定就更清晰了。 也可以在循環中的許多不同位置將endOfGame設置爲true

編輯:於編碼風格等

  • 它是一種常見的很好的做法變量的名字開始與低字母的一些注意事項(例如,而不是使用UserammouserAmmo)。以大寫字母開頭的名稱表示類別。
  • 如果名稱具有某些含義,它使代碼更容易閱讀。比如我會建議爲你的一些變量
    • 輸入→ userAction
    • Userammo → userAmmo
    • 輸出→壓實
    • 彈藥的以下變化→ compAmmo
  • 使用最終常量賦予意義重複出現在代碼中的數字或字符串。
  • 以新行開始if聲明。 (否則,如果在同一行是確定)

使用這些你可能會寫(而不是需要一些評論)

public class DoubleOSeven { 
    public static final String S_SHOOT = "shoot"; 
    public static final String S_BLOCK = "block"; 
    public static final String S_RELOAD = "reload"; 
    public static final int I_SHOOT = 1; 
    public static final int I_BLOCK = 2; 
    public static final int I_RELOAD = 3; 

    ... 
    System.out.println("Enter ("+ S_SHOOT + ", " + S_RELOAD + " or " + S_BLOCK + ") "); 

    ... 
    } else if(userAction.equals(S_BLOCK)){ 
     System.out.println("You blocked, Your ammo count is at: " + userAmmo); 
    } 

    if(compAction == I_SHOOT){ 
     compAmmo--; // I changed ++ to -- in order to be a fair game 
     System.out.println("I took a shot at you, My ammo count is at: " + compAmmo); 
    } else if(compAction == I_BLOCK){ 
     System.out.println("I blocked, My ammo count is at: " + compAmmo); 
    ... 
2
while((output == 3 && INput == "shoot") && (output == 1 && INput == "reload") && (output == 1 && INput == "shoot")); 

應該

while((output == 3 && INput.equals("shoot")) || (output == 1 && INput.equals("reload")) || (output == 1 && INput.equals("shoot"))); 
+0

我改變了我的代碼,但是,我怎麼讓它去繼續,直到有人失去 –

0

根據你的條件如下

while((output == 3 && INput == "shoot") && (output == 1 && INput == "reload") && (output == 1 && INput == "shoot")); 

僅在3個條件爲真循環將被執行。

你需要在你的病情

0

你所有的& &情況下,它們對錯誤的這個循環的結果進行評估的一些變化。

比方說,T爲所有真正的案件和F對所有的F案件

If T&&T&&T then yes we = T (the case for adding !) 
If T&&F&&F then F 
IF F&&T&&F then F 
... 
IF F&&F&&F then F 

所以,你需要重新評估你要什麼條件導致循環。

我們尋找

(output==3 && Input=="shoot") || (output==1 && input=="reload") || (output==1 && input=="shoot") 

引起下一次迭代?

這將使情況下,如果其中任何一個都是T,我們得到T.如果所有的人都爲F,那麼我們得到F.