code here
import java.util.Random;import java.util.Scanner;public class GuessTheNumber { public static int RenameMe(Scanner A, String B, intC, int D) {if (A == null) {return -1;} else if (C > D) {return -2;} else if(C < 0) {return -3;}int E = -6;System.out.println(B);if (A.hasNextInt()) {E = A.nextInt();A.nextLine(); //read and ignore the rest of the lineif (E < C) {return -4;} else if (E > D) {return -5;}} else {A.nextLine(); //read and ignore the token that isn't aninteger}return E; }/** * This is a guess the number game that usesthe method above for user input. * @param argsunused */ public static void main(String []args){ Random rand = new Random(); Scanner input = newScanner(System.in); final int LOW_BOUND = 1; final int HIGH_BOUND = 100; int hiddenNum = rand.nextInt(HIGH_BOUND- LOW_BOUND +1) + LOW_BOUND; int guessCount = 0; int guessedNum; do { guessedNum =RenameMe(input, "Enter a number between " + LOW_BOUND + " and " + HIGH_BOUND+ ": ", LOW_BOUND, HIGH_BOUND); guessCount++; if ( guessedNum >=LOW_BOUND && guessedNum <= HIGH_BOUND) { if(guessedNum < hiddenNum) { System.out.println("Your guess is too low."); } else{ System.out.println("Your guess is too high."); } } else { System.out.println("Your guess is invalid."); } } while (guessedNum !=hiddenNum); System.out.print("You won in " +guessCount + " attempts!"); input.close(); }}
test code
import java.util.Scanner;/** * This contains testing methods for the InputMethodsclass. * * @author JimWilliams * @author //TODO add your namehere when you contribute. */public class TestGuessTheNumber {/** * This main method runs the selected tests. Comment out a test if you don't want it to run. * * @param argsunused */ public static void main(String[] args){ testRenameMe(); }/** * Tests that the RenameMe method handlesall the cases described in its method header * comment. */ private static void testRenameMe(){ boolean error = false; //test 1, a happy path case (validinput) "25\n" with min 2 and max 50 should result in // 25 being returned. { //creates a new scope so that thevariables declared don't affect others in // this samemethod. //simulates a user typing25 then Enter. Scanner input = newScanner("25\n"); //the value expected tobe returned by the method for this test case. int expected = 25; int actual =GuessTheNumber.RenameMe(input, "Enter a number between 2 and50:", 2, 50); //if the actual valuereturned doesn't equal the expected then this is an error. if (actual != expected){ error =true; System.out.println("testRenameMe 1) Expected " + expected + ", avalid value to " + "be returned instead of " + actual); } } { //test 2, -1 is returned in theappropriate circumstance which should be described int expected = -1; } { //test 3, -2 is returned in theappropriate circumstance which should be described int expected = -2; } { //test 4, -3 is returned in theappropriate circumstance which should be described int expected = -3; } { //test 5, -4 is returned in theappropriate circumstance which should be described int expected = -4; } { //test 6, -5 is returned in theappropriate circumstance which should be described int expected = -5; } { //test 6, -6 is returned in theappropriate circumstance which should be described int expected = -6; } if (error) { System.out.println("\ntestRenameMe failed"); } else { System.out.println("\ntestRenameMe passed; it is expected that theprompt is output"); } }}
code here import java.util.Random; import java.util.Scanner; public class GuessTheNumber { public static int Renam
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am