samedi 30 janvier 2016

How can I get my code to conditionally loop back to its start?

I'm very much a beginner at Java. I am in my second week of my first course at college and already love programming. I've been trying to use some of the stuff I've learned in lecture and lab to create a little game but I'm having some issues.

The code is as follows:

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{

After the else statement I've literally just copy and pasted the code to start a new turn until one of the life totals gets down to 0. This is an issue though because it creates a finite number of turns. How can I get my code to keep looping until a life total reaches zero?

I understand the game is no where near complete. I would appreciate any help! Thanks so much

Aucun commentaire:

Enregistrer un commentaire