0

how to do this?

how can i limit the time that the user can give input to the Scanner in this code? i mean that when 25 second (25000 millis) are over the user can't give input anymore and the game ends (program ends by writting: return;). you cant run the code on SoloLearn... (won't work). https://code.sololearn.com/ce0vyM1ZGkR8 writen code: import java.util.*; public class Main { public static void main(String[] args) throws InterruptedException { String[] randomWords = {"run" , "block" , "java" , "code" , "time"}; Random r = new Random(); String myRandomWord = randomWords[r.nextInt(5)]; System.out.println("In 10 seconds There will be a countDown, the system will give you a word and you will need to type it as fast as you can!"); Thread.sleep(10000); System.out.println("3"); Thread.sleep(1000); System.out.println("2"); Thread.sleep(1000); System.out.println("1"); Thread.sleep(1000); System.out.println("GO!!!"); System.out.println("Your word is : " + myRandomWord); long countTimeStart = System.currentTimeMillis(); Scanner s = new Scanner (System.in); String myWord = s.next(); long countTimeEnd = System.currentTimeMillis(); float timeToWriteWordInSeconds = (countTimeEnd - countTimeStart) / 1000; //1 sec = 1000 millis. float timeToWriteWordInMillis = countTimeEnd - countTimeStart; if (!myWord.equals (myRandomWord)){ System.out.println("You Lost!"); }else if (myWord.equals (myRandomWord) && timeToWriteWordInMillis <= 10000){ System.out.println("You Won!"); }else if (myWord.equals (myRandomWord) && timeToWriteWordInMillis > 10000){ System.out.println("You got the word right, but it took you to long! - you lost!"); } System.out.println(""); System.out.println(timeToWriteWordInMillis + " millis"); System.out.println("about " + timeToWriteWordInSeconds + " seconds");

27th Jun 2020, 1:49 PM
Yahel
Yahel - avatar
7 Respuestas
+ 5
I myself have tried many a times in making a loader in java using sleep thread but it doesn't works while on BlueJ it perfectly works Seniru :)
27th Jun 2020, 2:18 PM
Nilesh
Nilesh - avatar
+ 4
Hello friend ✌️ This is to inform you that you can use thread and sleep in sololearn code playground and not even applets . Basically sl code playground doesn't supports thread classes
27th Jun 2020, 1:51 PM
Nilesh
Nilesh - avatar
+ 2
Hmm Nilesh✰ sololearn supports Thread and it's sleep method too
27th Jun 2020, 2:16 PM
Seniru
Seniru - avatar
+ 2
So how you explain that the code runs well on any other platform? Can you please just leave this subject and answer my original question?
27th Jun 2020, 3:52 PM
Yahel
Yahel - avatar
0
Nilesh✰ if they aren't supported you probably should get an Error, which doesn't happen here or in this code Maybe you got a time out for your code. But again it's not unsupported - but the sleep's behavior is limited
27th Jun 2020, 2:26 PM
Seniru
Seniru - avatar
0
This code is working on other platforms... just run it on another platform and please answer my question...
27th Jun 2020, 2:40 PM
Yahel
Yahel - avatar