In: Computer Science
The assignment is to write an algo that will predict your guess number, you will give computer clue as higher or lower. User binary algorithm.
package loop; so basically reverse guess game
here is a template of what the professor did in class we need to add stuff and make it reverse
import java.util.Scanner;
import java.util.Random;
public class GuessGame {
public static void main(String[]args){
try {
Scanner scan =
new Scanner(System.in);
Random random =
new Random();
int
computerGuess = random.nextInt( 100 );
//System.out.println( "Secrect Number :" +computerGuess );
int count =
0;
boolean
keepGuess = true;
while( keepGuess
) {
System.out.print("Enter your guessed Number:
");
String input = scan.next();
if( isNumaric( input ) ){
int userGuessNum =
Integer.parseInt( input );
count = count + 1;
if ( userGuessNum ==
computerGuess ){
System.out.println("Congrats! you win it toom you "+count);
keepGuess
= false;
}else if( userGuessNum >
computerGuess ) {
System.out.println("Guess lower");
}else if( userGuessNum <
computerGuess ){
System.out.println("Guess Higher");
}
}else {
System.out.println("Your input: "+ input + " is
wrong it has to be number");
}
}
}catch( Exception e ) {
System.out.println( "Wrong format number: " + e );
}
}
public static boolean isNumaric( String input )
{
try {
Integer.parseInt( input );
return
true;
}catch(Exception e){
return
false;
}
}
}
PROGRAM:
<script> // Your JavaScript goes here </script>
let randomNumber = Math.floor(Math.random() * 100) + 1; const guesses = document.querySelector('.guesses'); const lastResult = document.querySelector('.lastResult'); const lowOrHi = document.querySelector('.lowOrHi'); const guessSubmit = document.querySelector('.guessSubmit'); const guessField = document.querySelector('.guessField'); let guessCount = 1; let resetButton;
<div class="resultParas"> <p class="guesses"></p> <p class="lastResult"></p> <p class="lowOrHi"></p> </div>
<label for="guessField">Enter a guess: </label><input type="text" id="guessField" class="guessField"> <input type="submit" value="Submit guess" class="guessSubmit">
function checkGuess() { alert('I am a placeholder'); }
checkGuess();
let name = 'Bingo'; name; let hello = ' says hello!'; hello; let greeting = name + hello; greeting;
name += ' says hello!';
name = name + ' says hello!';
function checkGuess() { let userGuess = Number(guessField.value); if (guessCount === 1) { guesses.textContent = 'Previous guesses: '; } guesses.textContent += userGuess + ' '; if (userGuess === randomNumber) { lastResult.textContent = 'Congratulations! You got it right!'; lastResult.style.backgroundColor = 'green'; lowOrHi.textContent = ''; setGameOver(); } else if (guessCount === 10) { lastResult.textContent = '!!!GAME OVER!!!'; setGameOver(); } else { lastResult.textContent = 'Wrong!'; lastResult.style.backgroundColor = 'red'; if(userGuess < randomNumber) { lowOrHi.textContent = 'Last guess was too low!'; } else if(userGuess > randomNumber) { lowOrHi.textContent = 'Last guess was too high!'; } } guessCount++; guessField.value = ''; guessField.focus(); }
guessCount === 1
guessSubmit.addEventListener('click', checkGuess);
function setGameOver() { guessField.disabled = true; guessSubmit.disabled = true; resetButton = document.createElement('button'); resetButton.textContent = 'Start new game'; document.body.append(resetButton); resetButton.addEventListener('click', resetGame); }
function resetGame() { guessCount = 1; const resetParas = document.querySelectorAll('.resultParas p'); for (let i = 0 ; i < resetParas.length ; i++) { resetParas[i].textContent = ''; } resetButton.parentNode.removeChild(resetButton); guessField.disabled = false; guessSubmit.disabled = false; guessField.value = ''; guessField.focus(); lastResult.style.backgroundColor = 'white'; randomNumber = Math.floor(Math.random() * 100) + 1; }
for (let i = 1 ; i < 21 ; i++) { console.log(i) }
const resetParas = document.querySelectorAll('.resultParas p'); for (let i = 0 ; i < resetParas.length ; i++) { resetParas[i].textContent = ''; }
guessField.focus();
const guessField = document.querySelector('.guessField');
guessField.focus();
guessField.value = 'Hello';
guesses.value
guesses.textContent = 'Where is my paragraph?';
guesses.style.backgroundColor = 'yellow'; guesses.style.fontSize = '200%'; guesses.style.padding = '10px'; guesses.style.boxShadow = '3px 3px 6px black';
OPERATIONS USED:
Addition +
Subraction -
Multiplication ×
Division ÷
We have selected a random number between 1 and 100. See if you can guess it in 10 turns or fewer. We'll tell you if your guess was too high or too low.