Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7507307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:27:45+00:00 2026-05-29T22:27:45+00:00

I have written some code in java that will guess a number based on

  • 0

I have written some code in java that will guess a number based on the user’s selections. It’s pretty simple code. I am trying to integrate this code into an android app, by displaying the different number set each on different pages. The user will then click “Yes” if their number is in the set, or “No” if it isn’t, and they’ll then be redirected to another page.

The problem is I don’t know how to keep track of their selections across the pages so that I can run the “If” statements based on their values as shown in the original code. Is there a simple way to keep track of whether they click “Yes” or “No” across the pages?

This is my java code

public class BinaryGame 
{
    public static void main (String []args)
    {
        String set1, set2, set3, set4, set5, another = "y";
        int num, num1, num2, num3, num4, rand, number = 0, count = 0;
        final int PER_LINE = 4;

        Scanner scan = new Scanner (System.in);
        Random generator = new Random();

        while (another.equalsIgnoreCase("y"))
        {
            System.out.println ("Think of a number between 0 and 31. And I will be able to figure it out.");
            System.out.println ("");

            rand = generator.nextInt(2);
            if (rand == 1)
            {
                for (num = 16; num < 32; num++)
                {
                    System.out.print(num + " ");

                    count++;
                    if (count % PER_LINE ==0)
                    System.out.println ();
                }

                System.out.print ("Is your number in this group? y/n: ");
                set1 = scan.nextLine();
                if (set1.equalsIgnoreCase("y"))
                number += 16;
                else
                number += 0;
            }
            else 
            {
                for (num = 0; num < 16; num++)
                {
                    System.out.print(num + " ");

                    count++;
                    if (count % PER_LINE ==0)
                    System.out.println ();
                }

                System.out.print ("Is your number in this group? y/n: ");
                set1 = scan.nextLine();
                if (set1.equalsIgnoreCase("y"))
                    number += 0;
                else
                    number += 16;
            }

            System.out.println ();

            rand = generator.nextInt(2);
            if (rand == 1)
            {
                for (num1 = 8; (num1 < 16) || (23 < num1 && num1 < 32) ; num1++)
                {
                    System.out.print (num1 + " ");

                    count++;
                    if (count % PER_LINE ==0)
                    System.out.println ();
                    if (num1 == 15)
                    num1 = 23;
                }
                System.out.print ("Is your number in this group? y/n: ");
                set2 = scan.nextLine();
                if (set2.equalsIgnoreCase("y"))
                number += 8;
                else
                number += 0;
            }
            else
            {
                for (num1 = 0; (num1 < 8) || (15 < num1 && num1 < 24) ; num1++)
                {
                    System.out.print (num1 + " ");

                    count++;
                    if (count % PER_LINE ==0)
                    System.out.println ();
                    if (num1 == 7)
                    num1 = 15;
                }
                System.out.print ("Is your number in this group? y/n: ");
                set2 = scan.nextLine();
                if (set2.equalsIgnoreCase("y"))
                number += 0;
                else
                number += 8;
            }
            System.out.println ();
            rand = generator.nextInt(2);
            if (rand == 1)
            {
                for (num2 = 4; (num2 < 8) || (11 < num2 && num2 < 16) || (19 < num2 && num2 < 24) || (27 < num2 && num2 < 32); num2++)
                {
                    System.out.print (num2 + " ");

                    count++;
                    if (count % PER_LINE ==0)
                        System.out.println ();
                    if (num2 == 7)
                        num2 = 11;
                    if (num2 == 15)
                        num2 = 19;
                    if (num2 == 23)
                        num2 = 27;
                }

                System.out.print ("Is your number in this group? y/n: ");
                set3 = scan.nextLine();
                if (set3.equalsIgnoreCase("y"))
                    number += 4;
                else
                    number += 0;
            }
            else
            {
                for (num2 = 0; (num2 < 4) || (7 < num2 && num2 < 12) || (15 < num2 && num2 < 20) || (23 < num2 && num2 < 28); num2++)
                {
                    System.out.print (num2 + " ");

                    count++;
                    if (count % PER_LINE ==0)
                        System.out.println ();
                    if (num2 == 3)
                        num2 = 7;
                    if (num2 == 11)
                        num2 = 15;
                    if (num2 == 19)
                        num2 = 23;
                }

                System.out.print ("Is your number in this group? y/n: ");
                set3 = scan.nextLine();
                if (set3.equalsIgnoreCase("y"))
                    number += 0;
                else
                    number += 4;
            }
            System.out.println ();
            rand = generator.nextInt(2);
            if (rand == 1)
            {
                for (num3 = 2; (num3 < 4) || (5 < num3 && num3 < 8) || (9 < num2 && num3 < 12) || (13 < num3 && num3 < 16) || 
                    (17 < num3 && num3 < 20) || (21 < num3 && num3 < 24) || (25 < num3 && num3 < 28) || (29 < num3 && num3 < 32); num3++)
                {
                    System.out.print (num3 + " ");

                    count++;
                    if (count % PER_LINE ==0)
                        System.out.println ();
                    if (num3 == 3)
                        num3 = 5;
                    if (num3 == 7)
                        num3 = 9;
                    if (num3 == 11)
                        num3 = 13;
                    if (num3 == 15)
                        num3 = 17;
                    if (num3 == 19)
                        num3 = 21;
                    if (num3 == 23)
                        num3 = 25;
                    if (num3 == 27)
                        num3 = 29;
                }
                System.out.print ("Is your number in this group? y/n: ");
                set4 = scan.nextLine();
                if (set4.equalsIgnoreCase("y"))
                    number += 2;
                else
                    number += 0;
            }
            else
            {
                for (num3 = 0; (num3 < 2) || (3 < num3 && num3 < 6) || (7 < num2 && num3 < 10) || (11 < num3 && num3 < 14) || 
                (15 < num3 && num3 < 18) || (19 < num3 && num3 < 22) || (23 < num3 && num3 < 26) || (27 < num3 && num3 < 30); num3++)
            {
                System.out.print (num3 + " ");

                count++;
                if (count % PER_LINE ==0)
                    System.out.println ();
                if (num3 == 1)
                    num3 = 3;
                if (num3 == 5)
                    num3 = 7;
                if (num3 == 9)
                    num3 = 11;
                if (num3 == 13)
                    num3 = 15;
                if (num3 == 17)
                    num3 = 19;
                if (num3 == 21)
                    num3 = 23;
                if (num3 == 25)
                    num3 = 27;
            }
            System.out.print ("Is your number in this group? y/n: ");
            set4 = scan.nextLine();
            if (set4.equalsIgnoreCase("y"))
                number += 0;
            else
                number += 2;
            }
            System.out.println ();
            rand = generator.nextInt(2);
            if (rand == 1)
            {
                for (num4 = 1; num4 < 32; num4 += 2)
                {
                    System.out.print (num4 + " ");
                    count++;
                    if (count % PER_LINE ==0)
                        System.out.println ();
                }
                System.out.print ("Is your number in this group? y/n: ");
                set5 = scan.nextLine();
                if (set5.equalsIgnoreCase("y"))
                    number += 1;
                else
                    number += 0;
            }
            else
            {
                for (num4 = 0; num4 < 32; num4 += 2)
                {
                    System.out.print (num4 + " ");
                    count++;
                    if (count % PER_LINE ==0)
                        System.out.println ();
                }
                System.out.print ("Is your number in this group? y/n: ");
                set5 = scan.nextLine();
                if (set5.equalsIgnoreCase("y"))
                    number += 0;
                else
                    number += 1;
            }
            System.out.println ();
            System.out.println ("You chose: " + number);
            number = 0;
            System.out.print ("Would you like to play again? y/n: ");
            another = scan.nextLine();
        }

    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T22:27:47+00:00Added an answer on May 29, 2026 at 10:27 pm

    If by pages you mean Acvities with in the same application, then you would want to look at SharedPreferences (class reference and dev guide).

    In the Activity where the button is pressed, you might want to do somethinge like:

    SharedPreferences prefs = getSHaredPreferecne("buttonPrefs");
    SharedPreferences.Editor editor = prefs.editor();
    
    editor.PutBoolean("btnYes", true);
    editor.commit();
    

    Then, when you need access this (again with in the same application):

    SharedPreferecnes myPrefs = getShharedPreferences("buttonPrefs")
    boolean yesIsPressed = myPrefs.getBoolean("btnYes", false)    // the second param is a default return value if preference is not found
    

    SharedPreferences can used anywhere in tha same application, as long you have the correct name.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written some code in Java that will decode a SHOUTcast stream and
I have some java code that will be running as an importer for data
I have written some code to ensure that items on an order are all
I have some code I've written in PHP for consuming our simple webservice, which
I have an application written in C# that invokes some C code as well.
I have some old code written in C for 16-bit using Borland C++ that
I have a c++ source code that was written in linux/unix environment by some
Below, I have written simple code that re-creates problems that have emerged as my
I have written some code in my VB.NET application to send an HTML e-mail
I have written some code for playing a .wav through my application. Now I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.