multiaplication:
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
char playAgain = 'y';
while (playAgain == 'y' || playAgain == 'Y')
{
int right = 0, wrong = 0;
for (int i = 1; i <= 5; i++)
{
int num1 = (int) (Math.random() * 10);
int num2 = (int) (Math.random() * 10);
boolean answeredInTime = false;
System.out.print("What is " + num1 + " - " + num2 + "? You have 10
seconds: \n");
Thread timerThread = new Thread(() ->
{
for (int j = 10; j > 0; j--)
{
System.out.print(j+" ");
try
{
Thread.sleep(1000);
} catch (InterruptedException e)
{
return;
}
}
});
timerThread.start();
int answer = -1;
if (input.hasNextInt())
{
answer = input.nextInt();
answeredInTime = true;
}
timerThread.interrupt();
System.out.println();
if (answeredInTime)
{
if (num1 - num2 == answer)
{
System.out.println("You are right");
right++;
}
else
{
System.out.println("Wrong answer");
wrong++;
}
}
else
{
System.out.println("Time's up!");
wrong++;
}
}
System.out.println("Right: " + right);
System.out.println("Wrong: " + wrong);
System.out.println("Your point is " + right + "/5");
System.out.print("Do you want to play the game again? (y/n): ");
playAgain = input.next().charAt(0);
}
System.out.println("Goodbye!!!");
input.close();
}
}