HI
please can you run the code as attachement..i m trying to test the guess number ..if it is greater than the random number or smaller but when it is equal the function will return -1
i have error that i cannot understand as i m new in java
the function is in number 108 or the name of the function is "public static int checkGuess(int guess)"
any help will be appreciated thanks

package exercices;

import java.util.Scanner;

public class RandomGuess {
    // main is a method that contains all of the code to be executed when a program runs

        // This is a class variable that is available to every method
        // If you declare a variable in a method it is only accessible in that method (local variable)

        static double myPI = 3.14159265; 

        // Any changes made to randomNumber in any functions will effect its global value

        static int randomNumber;

        // Creates a Scanner object that monitors keyboard input

        static Scanner userInput = new Scanner(System.in);
    public static void main(String[] args) {
        /* Basic Method
         * accessModifier static returnDataType methodName (parameters)
         * { Statements }
         * Access Modifier: Determines who can execute a method
         * static: Used when you want to be able to execute a method that isn't part of a class definition
         * Return Data Type: The data type of value returned after a method executes (void if no values are returned)
         * Method Name: Must start with a letter, but can include letters, numbers, $, or _
         * Parameters / Arguments: Values passed to a method
         */

        //System.out.println(addThem(1,2)); // addThem(1,2) will be replaced with the value that method returns

        // Demonstrating passing by value
        int d = 5;

        // Changes to the variable d in tryToChange don't effect its value globally
        // We are passing the value of d to tryToChange and not the variable

        //tryToChange(d);
        //System.out.println("Static Variable d = "+ d);

        // Guessing a random number
        //System.out.println("\n");

        System.out.println(getRandomNum()); // Both prints and generates the value for randomNumber

        int guessResult = 1;
        int randomGuess = 0;

        while(guessResult != -1)
        {
        System.out.print("Guess a number between 0 and 50: ");

        // Accepts an integer input from the user
        // You can't declare this variable inside the while loop if you want to access it outside the while loop

        randomGuess = userInput.nextInt(); 

        guessResult = checkGuess(randomGuess);
        }

        System.out.println("Yes the random number is " + randomGuess);

        System.out.println(randomNumber); // Random value was changed globally by getRandomNum

    }

    // Adds the two numbers sent and returns the solution
    // public is the access modifier and means anyone can execute this method
    // Java Methods can return any primitive data type, or reference to an object (More on that later)

    public static int addThem(int a, int b)
    {
        double smallPI = 3.140; // This variable is local to the addThem function

        // compare returns 0 if equal | -1 if smallPI is less than myPI | 1 if smallPI is greater than myPI

        System.out.println(Double.compare(smallPI, myPI));

        int c = a + b;

        // return returns a value that replaces the call to this method
        // It must be an int since you defined this method returns ints above

        return c; 
    }

    // When you define an attribute / parameter you must define its type
    // That's why you can't type tryToChange(d)
    // Because this function doesn't return a value return type is void

    public static void tryToChange(int d)
    {
        d = d + 1;
        System.out.println("tryToChange d = " + d);
    }

    public static int getRandomNum()
    {
        // Creates a random number between 0 and 50
        // Since randomNumber is a class variable you don't have to declare, or define its type
        // If int randomNumber was declared in this method it wouldn't effect the global variable named randomNumber

        randomNumber = (int) (Math.random() * 51);
        return randomNumber;
    }

    public static int checkGuess(int guess)
    {
        String var1,var2;
        if(guess == randomNumber)
        {
            return -1;
        } else if (guess > randomNumber){

            var1="Greater than the guess";

            int num = Integer.parseInt(var1);
            return num;
        }

        else if (guess < randomNumber){
            var2="smaller than the guess";
            int num1 = Integer.parseInt(var2);
            return num1;

        }
        else{
            return guess; // Must return a value of type int
        }
    }

}

What value should checkGuess return if the numbers are not equal? (Not -1, but what?). You left that bit out when posting the requirements.

Anyway, in your code you create Strings like "Greater than the guess" then try to parse that as an int value. That won't work because the string doesn't contain anything that looks like an int. parseInt only works for strings like "100" or "-99" or "0". If the string doesn't represent an integer value parseInt throws an error at runtime.

i can return -2 when it is greater and -3 when it is smaller
my problem is that i want to println to show for the user that the guess is greater or smaller

The "cleanest" way is to return -2 or -3 from checkGuess, then use a if test (or switch) to print the right message,eg

guessResult = checkGuess(randomGuess);
if (guessResult == -1) {
   System.out.println("Well done");
} else if (guessResult == -2) {
   System.out.println("Sorry, too big");
(etc)

(note: for such a simple case it may look like a lot of overhead to separate the checking of the guess from the printing of the message. Maybe in this case it is. But in real life things get more complicated, and breaking up the logic like this is the right way to go. Doing it now will help to create good habits.)

Edited 2 Years Ago by JamesCherrill

This question has already been answered. Start a new discussion instead.