I made a code to produce a random 4 digit number. I then set up a scanner consol
ID: 3643959 • Letter: I
Question
I made a code to produce a random 4 digit number. I then set up a scanner console for the user to input a number. Then I use arrays to store the guessed numbers as digits and then compare...? or something?You can ONLY USE
printlns
booleans
if/then
While loop
for loop
arrays
strings
parameters
scanners
The program will print the following words in the given circumstance...
"bagels" -- no digits match between the guess and the answer
"fermi"-- printed for each corret digit in a correct position
"pica" for each correct digit in the WRONG position.
Example Run
--------------------------------------
Your guess? 1234
bagels
Your guess? 5678
fermi fermi pica
Your guess? 5566
fermi
Your guess? 5587
fermi pica pica
Your guess? 7578
You got it right in 5 guesses.
Do you want to play again? y
Your guess? 1234
fermi pica
Your guess? 5678
pica
Your guess? 2936
You got it right in 3 guesses.
Do you want to play again? NOPE
Explanation / Answer
import java.util.Random; import java.util.Scanner; public class Guess { public static void main(String args[]) { Scanner sc=new Scanner(System.in); Random randomGenerator = new Random(); int n1,n2,x=1; String s1,s2; char ch='y'; int a[]=new int[4]; StringBuffer dis=new StringBuffer(); n1=randomGenerator.nextInt(9999); int i; do { System.out.printf("Your guess?"); n2=sc.nextInt(); s1=Integer.toString(n1); s2=Integer.toString(n2); i=0; while(iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.