id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

mystery


Translate into Java the following pseudo-code, expressed in plain English.

      
   Goal:
      We want to write a method that returns a "mystery value" (do not ask) that
      it computed based on an integer value given as a parameter named "n".

   Variables declarations & initialization:
      Boolean variable named "flag", initialized to false.
      Int variable named "result", initialized to 0.

   Steps for the code:
      While n is greater than 0, do the following:
         Increment the variable "result" by 1.
         If the "flag" variable is false, set it to true and decrement 
            the variable "n" by dividing it by 3.
         Else, set "flag" to false and increment "n" by multiplying it by 2.
      Return the value stored in the variable "result".      

mystery(0) → 0
mystery(2) → 1
mystery(3) → 3

...Save, Compile, Run (ctrl-enter)

public int mystery(int n){ }

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Java Help

Misc Code Practice

Difficulty: 1

Copyright Nick Parlante 2017 - privacy