如何让我的代码有条件地循环回到它的开始?

时间:2016-01-30 17:26:22

标签: java loops if-statement while-loop

我是Java的初学者。我是大学第一堂课的第二周,已经喜欢编程了。我一直在尝试使用我在讲座和实验室学到的一些东西来创建一个小游戏,但我遇到了一些问题。

代码如下:

public class MathGame {
public static void main(String args[]){

    System.out.println("~~~~Welcome to ____!~~~~~");
    System.out.println("Press 'S' to continue");

    char startGame;
    char correctStart = 'S';
    startGame = TextIO.getChar();
    if (startGame == correctStart){

        System.out.println("Both you and the computer will start with 20 lives");   //Game explanation
        System.out.println("Use different move options to reduce your opponents life");
        System.out.println("First one down to zero loses!");
        System.out.println("OK here we go:");


        int lifeTotal1 = 10;
        int lifeTotal2 = 10;
        int turnNum = 0;
        turnNum ++;

        System.out.println("Start of turn: " + turnNum);
        System.out.println("Your life total is " + lifeTotal1 + " your opponents life total is " + lifeTotal2);     //Starts turn
        System.out.println("Select a move:");
        System.out.println("Press 1 to do 3 damage");
        System.out.println("Press 2 to do a random amount of damage");
        System.out.println("Press 3 to heal 2 damage");

        int userAttack1;
        userAttack1 = TextIO.getInt();

        if(userAttack1 == 1){           //User attack #1 selected
            lifeTotal2 -=3; 
        }
        if(userAttack1 == 2){           //User attack #2 selected
            double random1 = Math.random();
            if (random1 > 0 && random1 <= .2){
                lifeTotal2 -= 1;
            }
            if (random1 > .2 && random1 <= .4){
                lifeTotal2 -= 2;
            }
            if (random1 > .4 && random1 <= .6){
                    lifeTotal2 -= 3;
            }
            if (random1 > .6 && random1 <= .8){
                    lifeTotal2 -= 4;
            }
            if (random1 > .8 && random1 <= 1){
                        lifeTotal2 -=5;
            }
        }

        if (userAttack1 == 3){
            lifeTotal1 += 2;
        }

    System.out.println("End of turn");
    System.out.println("Your current health is " + lifeTotal1);
    System.out.println("Your opponents current health is " + lifeTotal2);

    if (lifeTotal1 <= 0){
        System.out.println("Game over, you lose");
    if (lifeTotal2 <= 0){
        System.out.println("Game over, you win");
    }
        }else{

在else语句之后我真的只是复制并粘贴代码以开始一个新的转向,直到其中一个生命总数降到0.这是一个问题,因为它创建了有限的转数。如何让我的代码保持循环直到生命总数达到零?

我知道游戏并非接近完成。我将不胜感激任何帮助! 非常感谢

2 个答案:

答案 0 :(得分:1)

while循环可用于迭代直到满足条件。这将循环,直到生命总数达到0。

//Game set up code before your loop

while (lifeTotal1 > 0 && lifeTotal2 > 0) {

    // Any code in here will repeat until one life total hits 0

}

//Code here to handle the game being over

答案 1 :(得分:0)

假设你从1迭代到10并且在某个迭代中你需要再次启动循环,那么你可以做这样的事情

for(int i=1 ;i<=10; i++){
   if(condition) {
      // This will make sure that the loop starts from the beginning.
      i=0;
   }
}