+ 1

1)how can i write a code that choses a random number and get user input and if the input coincides three times he wins

java

6th Dec 2017, 3:21 PM
Salami Umar
Salami Umar - avatar
3 Respostas
+ 1
https://docs.oracle.com/javase/8/docs/api/java/util/Random.html ^Get random number https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html ^Get user input Then just use a variable to track how many times they guess correctly. IF it's equal to 3, THEN display the message of them winning.
6th Dec 2017, 3:25 PM
AgentSmith
+ 1
Here you go bro, I created the code so you can read it and learn: https://code.sololearn.com/cqpYNoqDVfPM/#java import java.util.Random; import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); Random rand = new Random(); int correct = 0; int maxGuesses = 5; int randNum = 0; int[] userInput = new int[maxGuesses]; boolean isPlaying = true; while(isPlaying) { for(int i = 0; i < maxGuesses; ++i) { System.out.println("Please input guess: "); userInput[i] = scnr.nextInt(); System.out.println(":::Your guess is " + userInput[i] + ":::"); randNum = (rand.nextInt(10) + 1); System.out.println(":::Random number is " + randNum + ":::"); if(userInput[i] == randNum) { ++correct; System.out.println("+++Match!+++"); } else { System.out.println("---Mismatch!---"); } System.out.println(""); } if(correct >= 3) { System.out.println("!!!!! YOU WIN !!!!!"); isPlaying = false; } else { System.out.println("!!!!! YOU LOSE !!!!!"); isPlaying = false; } } } }
6th Dec 2017, 3:58 PM
AgentSmith
+ 1
thanks ii really appreciate
6th Dec 2017, 5:56 PM
Salami Umar
Salami Umar - avatar