(JAVA) hi, im working on a guessing game, and im trying to make it so it reads t
ID: 3674341 • Letter: #
Question
(JAVA) hi, im working on a guessing game, and im trying to make it so it reads the first letter of the word they type, to determine if they want to play again. I need to make a line of code where if they answer with any word starting with Y or y, it will run again. Ive been trying different variatons of charAt and ignorecase but none are working for me. heres the code for the part im talking about, The line specifically is the bold one:
public static void main(String[] args) {
boolean wantsToPlay = true;
while(wantsToPlay) {
playgame();
System.out.print("Do you want to play again? ");
Scanner scanner = new Scanner(System.in);
if ((scanner.nextLine().equalsIgnoreCase("y") //this part here.
playing = true;
else
playing = false;
}
Thanks.
Explanation / Answer
package com.test.chegg;
import java.util.Scanner;
/**
* Class to check the user want to continue to play the game or not
*
* @author yourName
*
*/
public class PlayGame {
public static void main(String[] args) {
boolean playing = false;
Scanner scanner = new Scanner(System.in);
// we need to use do..while conditional structure for repeating a step
// until condition doesn't satisfied
do {
playgame();
System.out.print("Do you want to play again? ");
if (scanner.nextLine().equalsIgnoreCase("y"))// this part here.
{
playing = true;
} else {
playing = false;
}
} while (playing);
scanner.close();// Closing the scanner
System.out.println("Thank you !");
}
/**
* Method to implement your game logic
*/
private static void playgame() {
}
}
Output:
Do you want to play again? y
Do you want to play again? Y
Do you want to play again? y
Do you want to play again? Y
Do you want to play again? Y
Do you want to play again? y
Do you want to play again? n
Thank you !
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.