Click here to Skip to main content
15,037,388 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i put inside my code
return;
but it does not return to main method

What I have tried:

Java
import java.util.Random;     // Random number generator class
import java.util.Scanner;    // reads user inputs

public class HiLo
{
    private static Scanner input = new Scanner(System.in);
    public static void main (String[] args)
    {

        System.out.print("Rules");
        System.out.println("Number 1 through 6 are low");
        System.out.println("Number 7 are neither");
        System.out.println("Number 8 through 13 are high");
        System.out.println("You have 1000 points");
        number();
    }

    public static void number()
    {
        System.out.println("Predict(1=high, 0=low): ");
        int guess = input.nextInt();
        System.out.println("Enter the points to risk: ");
        int risk = input.nextInt();
        int points = 1000;
        switch(guess)
        {
            case 1: high(guess,risk,points); break;
            case 2: low(guess,risk,points); break;

        }
    }

    public static void high(int guess, int risk, int points)
    {
        final int max = 13;
        int answer;
        Random generator = new Random(); //Random  number generator. 0 to 10.
        answer = generator.nextInt(max) +1;

        if(guess!= answer)
        {
            System.out.println("Number is " + answer);
            System.out.println("you lose.");
            int total = points - risk;
            System.out.println("you have " + total + " points"); 
            System.out.println("play again?(1 for yes, 2 for no)");
            int playagain = input.nextInt();
            if(playagain == 1)
            {
                return;
            }
            if(playagain == 2)
            {
                System.out.println("GG");
            }
        }
        if(guess == answer)
        {
            System.out.println("Number is " + answer);
            System.out.println("you win.");
            int total = points + risk;
            System.out.println("you have " + total + "points"); 
            System.out.println("play again?(1 for yes, 2 for no)");
            int playagain = input.nextInt();
            if(playagain == 1)
            {
                return;
            }
            if(playagain == 2)
            {
                System.out.println("GG");
            }
        }
    }

    public static void low(int guess, int risk, int points)
    {
        final int max = 13;
        int answer;
        Random generator = new Random(); //Random  number generator. 0 to 10.
        answer = generator.nextInt(max) +1;
        if(guess!= answer)
        {
            System.out.println("Number is " + answer);
            System.out.println("you lose.");
            int total = points - risk;
            System.out.println("you have " + total + " points"); 
            System.out.println("play again?(1 for yes, 2 for no)");
            int playagain = input.nextInt();
            if(playagain == 1)
            {
                return;
            }
            if(playagain == 2)
            {
                System.out.println("GG");
            }
        }
        if(guess == answer)
        {
            System.out.println("Number is " + answer);
            System.out.println("you win.");
            int total = points + risk;
            System.out.println("you have " + total + "points"); 
            System.out.println("play again?(1 for yes, 2 for no)");
            int playagain = input.nextInt();
            if(playagain == 1)
            {
                return;
            }
            if(playagain == 2)
            {
                System.out.println("GG");
            }
        }
    }
}
Posted
Updated 28-Mar-18 21:19pm
v3
Comments
Richard MacCutchan 29-Mar-18 3:36am
   
Yes it does, and main immediately terminates.
Nerdy4dayz 29-Mar-18 5:14am
   
why? and how can i fix that?
Richard MacCutchan 29-Mar-18 5:33am
   
What do you mean "why", and what do you want to fix?

Quote:
but it does not return to main method

The return does not return to main because there is nothing to do in main, your code is called by last instruction of main.
What magic stuff do you expect with that return?
[Update]
Quote:
so how can i gt it to repeat the game again?

You need to use a
loop
to repeat some code.
   
v2
Comments
Nerdy4dayz 29-Mar-18 5:10am
   
so how can i gt it to repeat the game again?
Your number method DOES return to main but then the program terminates (it is the end of the main).
Usually you put your code in a loop inside the main method.
   
Comments
Nerdy4dayz 29-Mar-18 5:10am
   
so what do i do to repeat my game again?
CPallini 29-Mar-18 5:18am
   
A loop.
Nerdy4dayz 29-Mar-18 5:28am
   
so i found out how to repeat the game, but is there any way to return the value of my points deducted into my first loop so i can use that amount of points again?
CPallini 29-Mar-18 5:47am
   
If you need the number method to return a value then, well, make it so. declare it
public static int number()

and use accordingly the return statement.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900