Im attempting to program a 'Name Guesser'. This game relies heavily on if statements and randomisation. I have a problem which i don't know how to fix, my if statements can only run in a sequence, and not separately! I want to be able to guess the selected name in any given order, but currently i can only guess the name in one order. Eg. C H R I S is the only way to guess the name correctly, any other sequence of characters will not work. What can i do to fix this?
Note - Chris is the only name implemented so far.
My NameGuesser class:
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class NameGuesser {
public static class Mark{
public static boolean M = false;
public static boolean A = false;
public static boolean R = false;
public static boolean K = false;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner reader1 = new Scanner(System.in);
String Guessspace = "_ _ _ _";
boolean Mark = true;
int Lives = 3;
int Guessingmode = 0;
String[] Names = {"Mark", "Adam", "Joel"};
String Random = (Names [new Random().nextInt(Names.length)]);
List<String> Markletters = new ArrayList<String>();
Markletters.add("M");
Markletters.add("A");
Markletters.add("R");
Markletters.add("K");
System.out.println(Random);
if (Random == "Mark"){
Guessingmode = 1;
}
if (Random == "Adam"){
Guessingmode = 2;
}
if (Random == "Joel"){
Guessingmode = 3;
}
{
if (Guessingmode == 1){
String Nameguess = reader1.next();
if(Markletters.contains(Nameguess)){
if (Nameguess.equals("M")){
NameGuesser.Mark.M = true;
GuessSpace GuessSpace;
GuessSpace = new GuessSpace();
GuessSpace.GuessSystem();
}
}
Nameguess = reader1.next();
if(Markletters.contains(Nameguess)){
if (Nameguess.equals("A")){
NameGuesser.Mark.A = true;
GuessSpace GuessSpace;
GuessSpace = new GuessSpace();
GuessSpace.GuessSystem();
}
}
Nameguess = reader1.next();
if(Markletters.contains(Nameguess)){
if (Nameguess.equals("R")){
NameGuesser.Mark.R = true;
GuessSpace GuessSpace;
GuessSpace = new GuessSpace();
GuessSpace.GuessSystem();
}
}
Nameguess = reader1.next();
if(Markletters.contains(Nameguess)){
if (Nameguess.equals("K")){
NameGuesser.Mark.K = true;
GuessSpace GuessSpace;
GuessSpace = new GuessSpace();
GuessSpace.GuessSystem();
}
}
}
}
}
}
My GuessSpace class:
public class GuessSpace extends NameGuesser{
public void GuessSystem() {
if( NameGuesser.Mark.M == true){
System.out.println("M _ _ _");
}
if( NameGuesser.Mark.M == true){
if( NameGuesser.Mark.A == true){
System.out.println("M A _ _");
}
}
if( NameGuesser.Mark.M == true){
if( NameGuesser.Mark.A == true){
if( NameGuesser.Mark.R == true){
System.out.println("M A R _");
}
}
}
if( NameGuesser.Mark.M == true){
if( NameGuesser.Mark.A == true){
if( NameGuesser.Mark.R == true){
if( NameGuesser.Mark.K == true){
System.out.println("M A R K");
System.out.println("Well done! You guessed my name!");
System.exit(0);
}
}
}
}
}
}
Aucun commentaire:
Enregistrer un commentaire