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 the 3 articles in this topic
Showing 3 of 100 articles.
Currently no other people reading this forum.


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

Login to reply

👍?
helpful
5:14pm Tue 19th Mar, Emily W.

Hello! So, for lab 3 exercise 1, I wanted to add user attempts based on their input range, as I have previously done this as a python exercise. With python it's a bit simpler, like so: min_num_guesses = round(math.log2(B - A + 1)). I have had a look and found that with java.lang.math, it only uses base 10 or e. I know that to change the base to 2 it would just be to divide log(x) by log(2) but I can't quite figure out how to implement this?


SVG not supported

Login to reply

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

For anyone interested, I have attached my results:)


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