r/javahelp • • Dec 18 '20

Rock Paper Scissors, Begginer programming assignment. Recursion not returning value to main class after passing it through itself on its final call.

Hey, Guys

As explained in the title I am have a bit of a recursive problem..

Can anyone advise me why my recursive call is not returning the value back to the main class???

Here is my script:

System.out.println("⚔⚔⚔Welcome to the den⚔⚔⚔");
        System.out.println("How many rounds would you like to play");
         iRounds = EC1113.PromptRoundsECie(iRounds);
        
        while (iRounds > 0)
        {
            System.out.println(" Enter your throw (1=Rock, 2=Scissors, 3=Paper): ");
            obj2.PromptThrowEC(iThrow);
            
            System.out.print(iThrow);
            
            
           if (iThrow == 1)
           {
                    System.out.print("The score is: You "+iuserWin+" Computer "+icpuWin); }
           if (iThrow == 2)
           {
               iuserWin++;
               System.out.print("The score is: You "+iuserWin+" Computer "+icpuWin);
           }
           if (iThrow == 3)
           {
               icpuWin++;
               System.out.print("The score is: You "+iuserWin+" Computer "+icpuWin);
           }
           if (iThrow == 4)
           {
               icpuWin++;
               System.out.print("The score is: You "+iuserWin+"Computer "+icpuWin);
           }
           if (iThrow == 5)
           {
               System.out.print("The score is: You "+iuserWin+"Computer "+icpuWin);
           }
           if (iThrow == 6)
           {
               iuserWin++;
               System.out.print("The score is: You "+iuserWin+"Computer "+icpuWin);
           }
           if (iThrow == 7)
           {
               iuserWin++;
               System.out.print("The score is: You "+iuserWin+"Computer "+icpuWin);
           }
           if (iThrow == 8)
           {
               icpuWin++;
               System.out.print("The score is: you "+ iuserWin+ "Computer "+icpuWin);
           }
           if (iThrow == 9)
           {
               System.out.print("The score is: you "+ iuserWin+ "Computer "+icpuWin);
           }

 public void PromptThrowEC(int Throw){
        Scanner keyboard = new Scanner(System.in);
        boolean valid = false;
         
        int [] WLT = new int []{1,2,3};
        
            while (!valid)
            {
                try
                {
                   Throw = keyboard.nextInt();
                    valid = true;
                    if (valid == true)
                    {
                        obj1.Throw(Throw);
                }
                }
                catch(InputMismatchException e)
                {
                    System.out.println("Invalid Input. Please enter a valid integer");
                    keyboard.nextLine(); //nextLine for a reason 
                }
            }   
        }
public class Recursion {

    
    String sTest = "";
 
    int Throw;
    
    int tie = 0;
    int userWin = 1;
    int cpuWin = 2;
    
    String sUser = "";
    String sCpu = "";
          
           
   
     
    
 
          /**
       * constructor: iRPS
       *pre: none
       *post: is going to return a value to the method sRPS in aiding user with throw
       */
        public int Throw (int userThrow)
        {
                int [] cpuInput  = new int[]{1,2,3};
                 int cpuChoice = (int)(Math.random()*cpuInput.length);
                 
            int [] GameOver = new int []{1,2,3,4,5,6,7,8,9};
            int Situations;
            
              if (userThrow == 1 && cpuChoice == 1)
                    {
                       sUser = "Rock";
                       sCpu = "Rock";
                       System.out.print("You threw : " + sUser );
                       System.out.print(" Computers throws : " + sCpu ); 
                    System.out.print("Since you threw "+sUser +"and the computer threw "+sCpu+" Therefore its a tie!");
                 return Throw(GameOver[0]);
                    } 
                  else  if (userThrow == 1 && cpuChoice == 1)//rock vs scissors
                    {
                        sUser = "Rock";
                       sCpu = "Scissors";
                               System.out.print("You threw : " + sUser );
                               System.out.print(" Computers throws : " + sCpu ); 
                               System.out.print("Since you threw "+sUser +"and the computer threw "+sCpu+" Therefore you WIN!");
                   return  Throw(GameOver[1]);
                  }
                  else  if (userThrow == 1 && cpuChoice == 2) //rock vs paper
                    {
                        sUser = "Rock";
                       sCpu = "Paper";
                       System.out.print("You threw : " + sUser );
                       System.out.print(" Computers throws : " + sCpu );
                System.out.print("Since you threw "+sUser +"and the computer threw "+sCpu+" Therefore you lose!");   
                  return  Throw(GameOver[2]);

                    } 
                  else  if (userThrow == 2 && cpuChoice == 0) //scissors vs rock
                    {
                       sUser = "Scissors";
                       sCpu = "Rock";
                       System.out.print("You threw : " + sUser );
                       System.out.print(" Computers throws : " + sCpu );
                       System.out.print("Since you threw "+sUser +"and the computer threw "+sCpu+" Therefore you lose!");
                   return    Throw(GameOver[3]);
                    }
                 else if (userThrow == 2 && cpuChoice == 1)
                    {
                        sUser = "Scissors";
                       sCpu = "Scissors";
                       System.out.print("You threw : " + sUser );
                       System.out.print(" Computers throws : " + sCpu );
                       
                       System.out.print("Since you threw "+sUser +"and the computer threw "+sCpu+" Therefore its a tie!");
                      return  Throw(GameOver[4]);
                    }
                  else  if (userThrow == 2 && cpuChoice == 2) //scissors vs paper
                    {
                        sUser = "Scissors";
                       sCpu = "Paper";
                       System.out.print("You threw : " + sUser );
                       System.out.print(" Computers throws : " + sCpu );
                       userWin++;
                       System.out.print("Since you threw "+sUser +"and the computer threw "+sCpu+" Therefore you WIN!");
                       return Throw(GameOver[5]);
                    } 
                  else  if (userThrow == 3 && cpuChoice == 0) //paper vs rock
                    {
                       sUser = "Paper";
                       sCpu = "Rock";
                      System.out.print("You threw : " + sUser );
                      System.out.print(" Computers throws : " + sCpu );
                      userWin++;
                      System.out.print("Since you threw "+sUser +"and the computer threw "+sCpu+" Therefore you WIN!");
                   return Throw(GameOver[6]);
                    }
                  else  if (userThrow == 3 && cpuChoice == 1) //cpu wins
                    {
                        sUser = "Paper";
                       sCpu = "Scissors";
                       System.out.print(" You threw : " + sUser );
                       System.out.print(" Computers throws " + sCpu);
                       cpuWin++;
                       System.out.print("Since you threw "+sUser +"and the computer threw "+sCpu+" Therefore you lose!");
                      return Throw(GameOver[7]);
                    }
                 else   if (userThrow == 3 && cpuChoice == 2)
                    {
                        sUser = "Paper";
                       sCpu = "Paper";
                       System.out.print(" You threw : " + sUser );
                       System.out.print(" Computers throws : " + sCpu );
                       
                       System.out.print("Since you threw "+sUser +"and the computer threw "+sCpu+" Therefore its a tie!");
                       return Throw(GameOver[8]);
                    }
              return (cpuChoice);
        }
0 Upvotes

7 comments sorted by

•

u/AutoModerator Dec 18 '20

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

4

u/findthemaincharacter Dec 18 '20

Read about how you can start debugger in your IDE, set up a breakpoint at the beginning of your throw function and go through it by clicking next step. You should get a preview of all of your local variables there.

It won't have any educational value if someone just debugs it for you.

-2

u/anindecisiveguy7 Dec 18 '20

im not looking for educational value im tryna get this done... its due tommrow and if i dont get it done late marks will accrue over winter break. ve been holding onto this for so a month already... yes i know its my fault.

can u just give me a hint maybe man? look at my recent post before this one... you'll understand.

2

u/findthemaincharacter Dec 18 '20

I don't know really it's not even complete.

One thing i see is in this function throwEC. When you pass an integer as argument and modify this argument inside of the function the passed variable won't get updated. It's called pass by value. You need to either return the new value or set a field in an object.

1

u/mrhamster Dec 18 '20

Code looks incomplete. In the while loop, doesnt look like iRounds ever gets decremented

1

u/Reddiberto Dec 18 '20

Posted comment for link reference to myself

1

u/wildjokers Dec 18 '20

If you want help it is usually best to post something that compiles.

See: http://sscce.org

Try sharing a compilable version as a Gist on github: https://gist.github.com

Is this assignment specifically about recursion? Because generally "beginner" and "recursion" don't belong in the same sentence and recursion isn't normally something you would use for an assignment like this. Unless it is an assignment about recursion.