Loops and logic statements - java

I have this program needed for class and the way they ask me to complete it is confusing me as i am unable to output the proper answer. what is needed is a series of inputs that contain a series of 'x''s 'X''s and 'r''s which in turn outputs a sound. if the input contains a character that is not an 'x' 'X' or an 'r' it must output something along the lines of "please enter a valid input." For the most part i had everything down but i am unable to figure out a way to properly display the invalid input string.
import java.util.Scanner;
public class String2Beat { //main class
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("To play a drum song please enter a series of x's and r's.");
System.out.println("Use an Uppercase X for the base drum, ");
System.out.println("Use a Lowercase x for the snare drum, ");
System.out.println("Or use a Uppercase R for a rest:");
String drums = scan.nextLine();
for (int j = 0; j < 3; j++){
for (int i = 0; i < drums.length(); i++){
if (drums.charAt(i) != 'x' && drums.charAt(i) != 'X' && drums.charAt(i) != 'r'){
System.out.println("not a valid string input");
}
else {
if (drums.charAt(i) == 'X'){
System.out.println("Now playing a Bass Drum. " + drums.charAt(i));
playBass();
}
else if(drums.charAt(i) == 'x'){
System.out.println("Now playing a Snare Drum. " + drums.charAt(i));
playSnare();
}
else if(drums.charAt(i) == 'r'){
System.out.println("Now playing a Rest. " + drums.charAt(i));
playRest();
}
}
}
}
scan.close();
}

What you need to do is figure out if the String contains an invalid input before you play the sounds. Your program should probably look something like this:
import java.util.Scanner;
public class String2Beat { //main class
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("To play a drum song please enter a series of x's and r's.");
System.out.println("Use an Uppercase X for the base drum, ");
System.out.println("Use a Lowercase x for the snare drum, ");
System.out.println("Or use a Uppercase R for a rest:");
String drums = scan.nextLine();
boolean test = false;
for (int i = 0; i < drums.length(); i++)
{
if (drums.charAt(i) != 'X' || drums.charAt(i) != 'x' || drums.charAt(i) != 'R')
test = true;
}
if (!test)
{
for (int j = 0; j < 3; j++)
{
for (int i = 0; i < drums.length(); i++)
{
if (drums.charAt(i) == 'X')
{
System.out.println("Now playing a Bass Drum. " + drums.charAt(i));
playBass();
}
else if(drums.charAt(i) == 'x')
{
System.out.println("Now playing a Snare Drum. " + drums.charAt(i));
playSnare();
}
else
{
System.out.println("Now playing a Rest. " + drums.charAt(i));
playRest();
}
}
}
}
}
else
{
System.out.println("not a valid string input");
}
scan.close();
}

Related

Best way to code my unintelligent Nim game?

This is my nim game (goal is dont be the last person to pick up marbles), can someone please guide me. In playing the game, I have just one question
How can I keep track of whose turn it is, I guess if I can keep track of that I can monitor my remainingMarbles. This is the crux of my code. Please help
public class Nim{
public static void main(String[] args)
{
System.out.println("************** Welcome to the game of Nim *******************");
System.out.println("The object of this game is to make me take the last marble off the table");
System.out.println("You must take at least 1 and you can take up to 3 marbles on your turn");
System.out.println("You can go first");
Scanner scan = new Scanner(System.in);
final int MARBLES = 13;
int remainingMarbles;
String input;
do {
//boolean whoseTurn = true;
remainingMarbles = MARBLES;
System.out.println("There are " + MARBLES + " marbles on the table");
while (remainingMarbles > 0){
remainingMarbles -= getUserSelection();
System.out.println("There are " + (remainingMarbles -= getComputerSelection()) + " marble(s) on the table.");
}
if (1 <= remainingMarbles && remainingMarbles <= 2 && remainingMarbles < 0) {
System.out.println("Congratulations! you won!");
System.out.println("Want to play again? y/n");
input = scan.nextLine();
input = input.toLowerCase();
} else
System.out.println("Hard luck you lose!");
System.out.println("Want to play again? y/n");
input = scan.nextLine();
input = input.toLowerCase();
}while (input.charAt(0) == 'y');
}
private static int getUserSelection()
{
Scanner scan = new Scanner(System.in);
do {
System.out.println("Enter 1, 2 or 3");
int userSelection = scan.nextInt();
if (isValidMove(userSelection)){
return userSelection;
}
else {
System.out.println("Not a valid entry");
}
}while (true);
}
private static boolean isValidMove(int input)
{
return 1 <= input && input <= 3;
}
private static int getComputerSelection ()
{
Random generator = new Random();
int computerSelection = 1 + generator.nextInt(3);
System.out.println("The computer chooses " + computerSelection);
return computerSelection;
}
}
First, make a boolean before the loop.
boolean whoseTurn = true;
When the variable is true it's the first player's turn, otherwise the second player's turn.
Now inside the while loop we change the variable at the end of every repetition:
whoseTurn = !whoseTurn;
(Which is a faster way of this)
if(whoseTurn) whoseTurn = false;
else whoseTurn = true;
To conclude, you should do something like that:
...
boolean whoseTurn = true;
while(remainingMarbles > 0){
remainingMarbles -= getUserSelection();
System.out.println("There are " + (remainingMarbles -= getComputerSelection()) + " marble(s) on the table.");
whoseTurn = !whoseTurn;
}
...

How to break from a loop after finding a word

I am trying to create a Hangman and I have 2 problems.
1) The first problem is when the user finds the word, the loop does not stop.
2) I have a variable attempts which allows to know the number of attempts. Even if the user finds the letter, the number of attempts decrease.
The word to find is no
Here is a demonstration:
1) I enter the letter n
You have 5 attempts.
--
Enter your letter : n
2) I enter the letter o
The letter is good.
You have 4 attempts.
n-
Enter your letter : o
3) Normally the loop should stop.
The letter is good.
You have 3 attempts.
no
Enter your letter :
If you have an idea thank you in advance.
Scanner input = new Scanner(System.in);
char letter = 0;
String[] words = {/*"yes",*/ "no"};
String word_random = words[(int) (Math.random() * words.length)];
boolean[] word_found = new boolean[word_random.length()];
int attempts = 5;
while(attempts > 0){
System.out.println("You have " + attempts + " attempts.");
for(int i=0; i<word_random.length(); i++) {
if ( word_found[i] ) {
System.out.print(word_random.charAt(i));
}
else {
System.out.print('-');
}
}
System.out.println("");
System.out.print("Enter your letter : ");
letter = input.next().charAt(0);
for(int i=0; i<word_random.length();i++){
if(word_random.charAt(i) == letter){
System.out.println("The letter is good. ");
word_found[i] = true;
}
}
attempts--;
}
}
}
You are just missing a checking loop or method. Check the solution below.
Scanner input = new Scanner(System.in);
char letter = 0;
String[] words = {/*"yes",*/ "no"};
String word_random = words[(int) (Math.random() * words.length)];
boolean[] word_found = new boolean[word_random.length()];
int attempts = 5;
while(attempts > 0){
System.out.println("You have " + attempts + " attempts.");
for(int i=0; i<word_random.length(); i++) {
if ( word_found[i] ) {
System.out.print(word_random.charAt(i));
}
else {
System.out.print('-');
}
}
System.out.println("");
System.out.print("Enter your letter : ");
letter = input.next().charAt(0);
for(int i=0; i<word_random.length();i++){
if(word_random.charAt(i) == letter){
System.out.println("The letter is good. ");
word_found[i] = true;
}
}
boolean done = true;
for(boolean b : word_found)
done = done && b;
if(done) break;
else attempts--;
}
I will follow to your solution, not suggest a better one.
Ad 1. Add a check if the array word found contains only true after your first for cycle and if there are only true values in the array, print "you won" and set attempts to 0
Ad 2. Move attempts-- to the else case of your first for cycle OR add attempts++ in the true case of your first for cycle
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
char letter = 0;
String[] words = { /* "yes", */ "no" };
String word_random = words[(int) (Math.random() * words.length)];
boolean[] word_found = new boolean[word_random.length()];
int attempts = 5;
while (attempts > 0) {
System.out.println("You have " + attempts + " attempts.");
for (int i = 0; i < word_random.length(); i++) {
if (word_found[i]) {
System.out.print(word_random.charAt(i));
} else {
System.out.print('-');
}
}
System.out.println("");
System.out.print("Enter your letter : ");
letter = input.next().charAt(0);
boolean match = false;
for (int i = 0; i < word_random.length(); i++) {
if (word_random.charAt(i) == letter) {
System.out.println("The letter is good. ");
word_found[i] = true;
match = true;
if (i == word_found.length - 1) {
System.out.println("THE END: attempts: " + attempts);
return;
}
}
}
if (!match) {
attempts--;
}
}
System.out.println("THE END");
}
I suggest you to modify the last part of your code like I did, and it should work.

my 2nd if statement is being ignored dont know why

Hello guys i am basically creating a program that takes about 5 inputs from users and stores them in an array called course....every time user enter 1 course i want to ask the user if he wants to continue.. if he type yes the program continues or else it shows the values in array(course)......
i am facing issues :-
1.i dont wanna ask user to continue if he is already entering the last value which is 5th one.
2.my 2nd loop is basically being ignored i dont know why if it works every thing would've been ok.
3.if the user enters last value and press yes according to my program it is still not printing the values in my course(array).
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package ArrayExample;
import java.util.Scanner;
/**
*
* #author jodh_
*/
public class ArrayExample {
public static void main(String args[]) {
String[] course = new String[5];
for (int i = 0; i < 5; i++) {
System.out.println("Please enter course name " + (i + 1) + ": ");
Scanner gettingname = new Scanner(System.in);
String coursenames = gettingname.nextLine();
course[i] = coursenames;
if (i < 4) {
for(int j =0; j<4; j++){
System.out.println("");
System.out.println("Would you like to continue? ((y)Yes / (n)No)");
Scanner gettingYesOrNo = new Scanner(System.in);
String input = gettingYesOrNo.nextLine();
if (input.equals("yes") || input.equals("y") || input.equals("Y") || input.equals("YES") || input.equals("Yes")) {
System.out.println("");
break;
} else if (input.isEmpty()) {
System.out.println("Input cannot be empty!! Please try again");
} else if (input.equals("no") || input.equals("No") || input.equals("NO") || input.equals("n") || input.equals("N")) {
System.out.println("Your select courses are as follows");
}
for (int x = 0; x < i + 1; x++) {
System.out.println("");
System.out.println((x + 1) + ": " + course[x]);
}
System.exit(0);
}
}
}
}
}
This will work fine if you change the break to continue.Try my code.
String[] course = new String[5];
for (int i = 0 ; i < 5 ; i++){
System.out.println("Please enter course name " + (i + 1) + ": ");
Scanner gettingname = new Scanner(System.in);
String coursenames = gettingname.nextLine();
course[i] = coursenames;
if (i < 4){
System.out.println("");
System.out.println("Would you like to continue? ((y)Yes / (n)No)");
Scanner gettingYesOrNo = new Scanner(System.in);
input = gettingYesOrNo.nextLine();
if (input.equals("yes") || input.equals("y") || input.equals("Y") || input.equals("YES") || input.equals("Yes")) {
System.out.println("");
continue;
} else if (input.isEmpty()) {
System.out.println("Input cannot be empty!! Please try again");
} else if (input.equals("no") || input.equals("No") || input.equals("NO") || input.equals("n") || input.equals("N")) {
System.out.println("Your select courses are as follows");
}
}
for (int x = 0; x < i + 1; x++) {
System.out.println("");
System.out.println((x + 1) + ": " + course[x]);
}
}
}
Here is the output
Please enter course name 1:
androiid
Would you like to continue? ((y)Yes / (n)No)
y
Please enter course name 2:
dsfnfv
Would you like to continue? ((y)Yes / (n)No)
y
Please enter course name 3:
sfgngff
Would you like to continue? ((y)Yes / (n)No)
y
Please enter course name 4:
grgngr
Would you like to continue? ((y)Yes / (n)No)
y
Please enter course name 5:
fgmt
1: androiid
2: dsfnfv
3: sfgngff
4: grgngr
5: fgmt
Process finished with exit code 0
I've made quite a few changes in your code like removing some unnecessary loops but its working now
public static void main(String args[]) {
String[] course = new String[5];
Scanner gettingname = new Scanner(System.in);
int i=0;
for (; i < 5; i++) {
System.out.println("Please enter course name " + (i + 1) + ": ");
course[i] =gettingname.nextLine();
System.out.println("");
if(i==4) break;
System.out.println("Would you like to continue? ((y)Yes / (n)No)");
Scanner gettingYesOrNo = new Scanner(System.in);
String input = gettingYesOrNo.nextLine();
if (input.equals("yes") || input.equals("y") || input.equals("Y") || input.equals("YES") || input.equals("Yes")) {
System.out.println("");
continue;
}
else if (input.isEmpty()) {
System.out.println("Input cannot be empty!! Please try again");
}
else if (input.equals("no") || input.equals("No") || input.equals("NO") || input.equals("n") || input.equals("N")) {
break;
}
}
System.out.println("Your select courses are as follows");
for (int x = 0; x < i+1 ; x++) {
System.out.println("");
System.out.println((x + 1) + ": " + course[x]);
}
}
}
This is why code formatting is so very important! Spacing the nested if/else if/for statements allows you to follow the logic of your program much quicker and easier.
Here is a formatted version of your code:
import java.util.Scanner;
/** * * #author jodh_ */
public class ArrayExample
{
public static void main(String args[])
{
String[] course = new String[5];
for (int i = 0; i < 5; i++)
{
System.out.println("Please enter course name " + (i + 1) + ": ");
Scanner gettingname = new Scanner(System.in);
String coursenames = gettingname.nextLine();
course[i] = coursenames;
if (i < 4)
{
for(int j =0; j<4; j++)
{
System.out.println("");
System.out.println("Would you like to continue? ((y)Yes / (n)No)");
Scanner gettingYesOrNo = new Scanner(System.in);
String input = gettingYesOrNo.nextLine();
if (input.equals("yes") || input.equals("y") || input.equals("Y") || input.equals("YES") || input.equals("Yes"))
{
System.out.println("");
break;
}
else if (input.isEmpty())
{
System.out.println("Input cannot be empty!! Please try again");
}
else if (input.equals("no") || input.equals("No") || input.equals("NO") || input.equals("n") || input.equals("N"))
{
System.out.println("Your select courses are as follows");
}
for (int x = 0; x < i + 1; x++)
{
System.out.println("");
System.out.println((x + 1) + ": " + course[x]);
}
System.exit(0);
}
}
}
}
}
In this formatted code we can easily see where each piece of code is being executed.
If you take a look at the part that checks for "yes" you will see that if the conditions are met it breaks out of the inner for loop. This means that all code underneath this break within the inner loop is not executed if the user gives an answer of "yes".
if (input.equals("yes") || input.equals("y") || input.equals("Y") || input.equals("YES") || input.equals("Yes"))
{
System.out.println("");
break;
}
Your print statement is inside this inner for loop, which in fact means that it will only execute if the conditions of the "yes" if statement are not met. If you were to input the text "cat" in the "would you like to continue?" prompt it would skip all the if/else if statements since none of them are being met, execute the print loop, output the courses, and System.exit(0); terminate the program.
It appears that the intended purpose is to output the courses if the user inputs "no". So lets see if we can move the print loop inside the "no" if.
else if (input.equals("no") || input.equals("No") || input.equals("NO") || input.equals("n") || input.equals("N"))
{
System.out.println("Your select courses are as follows");
for (int x = 0; x < i + 1; x++)
{
System.out.println("");
System.out.println((x + 1) + ": " + course[x]);
}
System.exit(0);
}
This will output the courses only if the user inputs "no". And then it will termanate the program. Perfect!
So lets talk about this empty input condition:
else if (input.isEmpty())
{
System.out.println("Input cannot be empty!! Please try again");
}
Unless you need to specifically check that the input is empty you probably want to replace this with just an else statement. Lets see what that would look like.
if (input.equals("yes") || input.equals("y") || input.equals("Y") || input.equals("YES") || input.equals("Yes"))
{
System.out.println("");
break;
}
else if (input.equals("no") || input.equals("No") || input.equals("NO") || input.equals("n") || input.equals("N"))
{
System.out.println("Your select courses are as follows");
for (int x = 0; x < i + 1; x++)
{
System.out.println("");
System.out.println((x + 1) + ": " + course[x]);
}
System.exit(0);
}
else
{
System.out.println("Invalid input!! Please try again.");
j--;
}
You need to decrement j because at the end of the for loop it will increment forward to the next course. Since the user input was invalid we want to keep the course iterator right where it is and continue to ask the user if they woud like to continue until a valid selection has been made.
We're almost there!
Now that we have sorted the input conditions, what happens when the user reaches the 5th course?
Right now we only have the courses print if the user answers "no". So at the end of the course loop we need to print the full course string array. The final product looks like so:
import java.util.Scanner;
/** * * #author jodh_ */
public class ArrayExample
{
public static void main(String args[])
{
String[] course = new String[5];
for (int i = 0; i < 5; i++)
{
System.out.println("Please enter course name " + (i + 1) + ": ");
Scanner gettingname = new Scanner(System.in);
String coursenames = gettingname.nextLine();
course[i] = coursenames;
if (i < 4)
{
for(int j =0; j<4; j++)
{
System.out.println("");
System.out.println("Would you like to continue? ((y)Yes / (n)No)");
Scanner gettingYesOrNo = new Scanner(System.in);
String input = gettingYesOrNo.nextLine();
if (input.equals("yes") || input.equals("y") || input.equals("Y") || input.equals("YES") || input.equals("Yes"))
{
System.out.println("");
break;
}
else if (input.equals("no") || input.equals("No") || input.equals("NO") || input.equals("n") || input.equals("N"))
{
System.out.println("Your select courses are as follows");
for (int x = 0; x < i + 1; x++)
{
System.out.println("");
System.out.println((x + 1) + ": " + course[x]);
}
System.exit(0);
}
else
{
System.out.println("Invalid input!! Please try again");
j--;
}
}
}
}
System.out.println("Your select courses are as follows");
for (int x = 0; x < 5; x++)
{
System.out.println("");
System.out.println((x + 1) + ": " + course[x]);
}
}
}
Note: There are more optimal ways to get to the same result but I tried to keep the code as close to the original logic as possible.
Here's some code I wrote that might fix your program. You'll have to implement the rest but it should be easy enough.
import java.util.Scanner;
public class ArrayExample {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String[] course = new String[5];
String yesOrNo;
int index = 0;
do{
System.out.println("Please enter course name " + (index + 1) + ": ");
course[index] = input.nextLine();
if(index < 4) {
System.out.println("Would you like to continue? ((y)Yes / (n)No)");
yesOrNo = input.nextLine();
}
index++;
}while(yesOrNo.toLowerCase().startsWith("y) && index < 5)
//if the loop is exited then we know either the user chose not to continue or all the course names have been filled
//handle that here
}
}
The logic of what I did:
The while loop condition makes it so that you won't continue looping when all the courses are filled. And inside of the loop it checks if the index is not 4.
there's no more need for your second loop
you can print your array after the loop now.
Making two scanners is completely useless and takes up memory because you didn't even close them.
I hope this helps.

Creating simple hangman program in java, trouble with for loop

public class Game {
public static void main(String[] args) {
String secretWord = "frog";
System.out.println("Word has " + secretWord.length() + " letters.");
System.out.println("Guess a letter: ");
int correctGuesses = 0;
int strikes = 5;
Scanner input = new Scanner(System.in);
while (strikes > 0) {
here the loop cycles through the characters in a String, and checks if the user input guessedLetter matches a char in the string. right now the loop cycles through each char starting with the first char in the string through the last, the user must guess the letters in the exact order of they are arranged in the string, how can I fix this so that any character input matching a character in the String will be correct rather than the characters having to be in order?
for (int i = 0; i < secretWord.length(); i++) {
char guessedLetter = input.next().charAt(0);
char currentLetter = secretWord.charAt(i);
if (guessedLetter == currentLetter) {
correctGuesses++;
System.out.printf("Correct Guess! %d Letters Left!\n", secretWord.length() - correctGuesses);
}
else if (guessedLetter != currentLetter) {
strikes--;
System.out.printf("Incorrect: You Have %d Chances Left..\n", strikes);
}
if (strikes == 0) {
System.out.println("You Are Out of Chances! Game over!");
}
else if (correctGuesses == secretWord.length()) {
System.out.println("You Got It! The Word is: " + secretWord);
}
}
}
}
You could try it as follows:
move all the characters of secretWord to a map (key would be the character and the value would be incidences of that character in the string).
read the character from the keyboard and interate (basically, your logic).
Here is the code.
public static void main(String[] args) {
String secretWord = "frog";
Map<Character, Integer> mapOfLetters = new HashMap<>();
for (char c : secretWord.toCharArray()) {
int count = 1;
if (mapOfLetters.containsKey(c)) {
count = mapOfLetters.get(c) + 1;
}
mapOfLetters.put(c, count);
}
System.out.println("Word has " + secretWord.length() + " letters.");
System.out.println("Guess a letter: ");
int correctGuesses = 0;
int strikes = 5;
Scanner input = new Scanner(System.in);
while (strikes > 0 && !mapOfLetters.isEmpty()) {
char guessedLetter = input.next().charAt(0);
if (mapOfLetters.containsKey(guessedLetter)) {
correctGuesses++;
System.out.printf("Correct Guess! %d Letters Left!\n", secretWord.length() - correctGuesses);
int count = mapOfLetters.get(guessedLetter) - 1;
if (count == 0) {
mapOfLetters.remove(guessedLetter);
} else {
mapOfLetters.put(guessedLetter, count);
}
} else {
strikes--;
System.out.printf("Incorrect: You Have %d Chances Left..\n", strikes);
}
}
if (strikes == 0) {
System.out.println("You Are Out of Chances! Game over!");
} else if(mapOfLetters.isEmpty()){
System.out.println("You Got It! The Word is: " + secretWord);
}
}
guessedLetter = input.next().charAt(0);
If you want to match the whole UserInput there should not be charAt(0).
On the other hand, if you just want to match any character, at first time you can test guessedLetter == currentLetter is true, then just exit program.
By the way, if you like, just google "KMP algorithm" to learn how to match String.

How do I make a String unusable the second time in a loop?

I'm new to this site. I've decided to create a console base hangmaan game and I've been doing ok up till now. My current problem has me stuck.
I'm trying to make it so that if the user has input a letter and it has been marked as correct or incorrect. The program should then not let the user input that same letter again in later iterations of the while loop.
The comments should give you a better idea of what I'm talking about.
Can anyone please help me?
package hangMan2;
import java.io.IOException;
import java.util.Scanner;
public class mainClss {
public static void main(String[] args) {
int point = 0;
int numberOfLetterInWord;
// prompt user for input and store input into String word
System.out.println("Enter a word in lower case from the dictionary: ");
Scanner inputWord = new Scanner(System.in);
String word = inputWord.next();
// letters remaining in word equals the length of the word at the start
int lettersRemainingInWord = word.length();
for (int i = 0; i < 30; i++) {
System.out.println("\b");
}
// while points are above 7 (7 is when man is hung) and there's more
// than one letter remaining do all the following:
while (point > -7 && lettersRemainingInWord >= 1) {
//prompts user to input letter guess and stores in letter
System.out.print("\nEnter a letter for this " + word.length()
+ " letter word: ");
Scanner inputLetter = new Scanner(System.in);
String letter = inputLetter.next();
if (word.contains(letter)) {
System.out.println("Correct!");
point += 1;
System.out.println("Score: " + point);
lettersRemainingInWord -= 1;
//I need code here to remove the current letter from being able to be used again
if (lettersRemainingInWord > 0) {
continue;
}
else {
System.out.println("\nYou win!!!");
System.out.println("The word was: " + word);
break;
}
}
else {
System.out.println("Incorrect\n");
point -= 1;
System.out.println("Score: " + point);
//I need code here to remove the current letter from being able to be used again
if (lettersRemainingInWord > 0) {
continue;
}
else {
System.out.println("Game over! You lose!");
System.out.println("Score: " + point);
System.out.println("The word was: " + word);
break;
}
}
}
if (point <= -7) {
System.out.println("Game over! You lose!");
System.out.println("Score: " + point);
System.out.println("The word was: " + word);
}
}
}
You could test whether the letter is in a Set. If not, accept it and add it to the set. If so, then reject it.
Set<String> usedLetters = new HashSet<String>();
boolean isUsedLetter(String letter) {
if (usedLetters.contains(letter)) {
return true;
} else {
usedLetters.add(letter);
return false;
}
}
You can use an ArrayList to hold the characters that have already been typed and then check the list to see if the character is in there.
List<Character> used = new ArrayList<Character>();
char let = //letter that they inputted
if(used.contains(let)) {
//do something
}
else {
used.add(let);
}

Categories

Resources