mercredi 1 août 2018

Loop for three possibilities in Java. Ideally they'd be strings ie. "new," "again," and "quit."

It's a short homework I received in school, but I wanted to elaborate on it a bit by making it an interactive app, versus reading from a file. I'd like to make a loop asking for input, and run the program accordingly. I'd also like to add 2 to the first die, and 3 to the second, subtracting 6 if it is over the die's amount, and roll again, then print the outcome. All and any help would be appreciated. Thanks!

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int dieOne = 0, dieTwo = 0, wins = 0, losses = 0, ties = 0, testLoop = 2;
boolean oneValid = false, twoValid = false; 

    while (testLoop != 3){

    if (testLoop == 1) {

    dieOne = 0;
    dieTwo = 0;
    wins = 0;
    losses = 0;
    ties = 0;
    testLoop = 2;
    oneValid = false;
    twoValid = false; 
    testLoop = 2;    

  }

  else if (testLoop == 2){

 while (testLoop == 2) {

while (oneValid != true && twoValid != true) {

System.out.println("\nDie 1 please!");
dieOne = sc.nextInt();
oneValid = isValid(dieOne);

while (oneValid != true) {
        System.out.println("\nNot a valid entry. Enter a number from 1 through 6 please.");
        dieOne = sc.nextInt();
        oneValid = isValid(dieOne);
    }

    System.out.println("\nYour first die is: " + dieOne);

System.out.println("\nDie 2 please!");
dieTwo = sc.nextInt();
twoValid = isValid(dieTwo);

while (twoValid != true) {

        System.out.println("\nNot a valid entry. Enter a number from 1 through 6 please.");
        dieTwo = sc.nextInt();
        twoValid = isValid(dieTwo);
    }

    System.out.println("\nYour second die is: " + dieTwo);

}

int[] keepScore = rollDice(dieOne,dieTwo);

wins = wins + keepScore[0];
losses = losses + keepScore[1];
ties = ties + keepScore[2];

System.out.println("\nWins: " + wins + " Losses: " + losses + " Ties: " + ties);
System.out.println("\nWould you like to play again? \n1 = New Game, 2 = Another Roll, 3 = Quit.");
testLoop = sc.nextInt();
    }

}
    else {

        while (testLoop != 1 || testLoop != 2){

        System.out.println("Not valid entry, 1 = New, 2 = Another round, 3 = Quit");
        testLoop = sc.nextInt();
}

    }

}

}

//starts method that does calculations accroding to rules

public static int[] rollDice(int dieOne, int dieTwo) {

 int win = 0, loss = 0, draw = 0;

 if (dieOne + dieTwo == 5 || dieOne + dieTwo == 7 || dieOne + dieTwo == 12) {
     win++;
 }
else if (dieOne + dieTwo == 2 || dieOne + dieTwo == 4 || dieOne + dieTwo == 11) {
     loss++;
}
else {
    draw++;
}

int[] winLossDraw = {win,loss,draw};

return winLossDraw;

}    

//starts method that checks if input is valid

public static boolean isValid(int die) {

boolean isValid;

if (die < 1 || die > 6) {
    isValid = false;
}
else {
    isValid = true;
}

return isValid;

}    

    }

Aucun commentaire:

Enregistrer un commentaire