It's UWAweek 17 (1st semester, week 8)

help2005

This forum is provided to promote discussion amongst students enrolled in CITS2005 Object Oriented Programming.

Please consider offering answers and suggestions to help other students! And if you fix a problem by following a suggestion here, it would be great if other interested students could see a short "Great, fixed it!"  followup message.

How do I ask a good question?
Displaying selected article
Showing 1 of 73 articles.
Currently no other people reading this forum.


 UWA week 12 (1st semester, week 4) ↓
SVG not supported

Login to reply

👍?
helpful
5:48pm Wed 20th Mar, Emily W.

import java.util.Scanner; import java.util.Random; import java.lang.Math; public class Guess_the_Number { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter your lower starting range number: "); int num1 = sc.nextInt(); System.out.println("Enter you higher end range number: "); int num2 = sc.nextInt(); System.out.println("Great! Now guess the secret number between " + num1 + " and " + num2 + "."); Random r = new Random(); int secretNumber = r.nextInt(num1, num2); int a = 2; float num = (float) (Math.log(num2 - num1 + 1) / Math.log(a)); int numberGuesses = (int) Math.ceil(num); System.out.println("You have " + numberGuesses + " tries."); int count = 0; while (count < numberGuesses) { count++; System.out.println("What is your guess? "); int guess = sc.nextInt(); if (guess == secretNumber) { System.out.println("Congrats! The number was " + secretNumber + " you did it in " + count + " tries!"); break; } if (guess > secretNumber) { System.out.println("You guessed too high! Guess again."); } if (guess < secretNumber) { System.out.println("You guessed too low! Try again."); } } if (count > numberGuesses) { System.out.println("Out of guesses! The number is " + secretNumber); System.out.println("Better luck next time!"); } } }

The University of Western Australia

Computer Science and Software Engineering

CRICOS Code: 00126G
Written by [email protected]
Powered by history
Feedback always welcome - it makes our software better!
Last modified  5:07AM Sep 06 2023
Privacy policy