开发者

Java algorithm for random class that chooses from two loops

开发者 https://www.devze.com 2022-12-20 02:18 出处:网络
I have to make my program choose randomely between two sets of numbers (original & complement) but there are 5 sets of this loops. How can i assign for the program to choose randomely between the

I have to make my program choose randomely between two sets of numbers (original & complement) but there are 5 sets of this loops. How can i assign for the program to choose randomely between the two (original &complement) in each set? ok so this is what i have so far...

import java.util.Scanner;

import java.util.Random;

public class NumberFinder {

    int number;

    int t = 1;
    int v;
    Scanner scan = new Scanner(System.in);

    public void display()

    {
        if (t % 4 != 0)
            System.out.print(v + " ");
        else
            System.out.println(v);

        t++;

    }

    public int findNumber() {

        Random generator = new Random();
        {
            {
                for (v = 1; v < 32;) {
                    display();

                    if (v % 2 == 1)
                        v += 2;
                }

                for (v = 0; v < 32;)
                    ;
                {
                    display();

                    if (v % 2 == 1)
                        v += 2;
                }

            }
            System.out.println("Is your number in this set?");

            String response1 = scan.nextLine();

            for (v = 2; v < 32;) {
                display();

                if (v % 2 == 0)
                    v++;
                else
                    v += 3;
            }

            for (v = 0; v < 32;) {
                display();
                if (v % 2 == 0)
                    v++;
                else
                    v += 3;
            }

            System.out.println("Is your number in this set?");
            String response2 = scan.nextLine();

            for (v = 4; v < 32;) {
                display();

                if (v % 4 == 3)
                    v += 5;
                else
                    v++;
            }

            for (v = 0; v < 32;)
                ;
            {
                if (v % 4 == 3)
                    v += 5;
                else
                    v++;
            }

            System.out.println("Is your number in this set?");

            String response3 = scan.nextLine();

            for (v = 8; v < 32;) {
                display();
                if (v % 8 == 7)
                    v += 9;
                else
                    v++;
            }

            for (v = 0; v < 32;) {
                display();
                if (v % 8 == 7)
                    v += 9;
                else
                    v++;

            }

            System.out.println("Is your number in this set?");

            String response4 = scan.nextLine();

            for (v = 16; v < 32;) {
                display();
                if (v % 1 == 0)
                    v++;

            }

            for (v = 0; v < 32;) {
                display();
                if (v % 1 == 0)
                    v++;
            }

            System.out.println("Is your number in the set?");
            String response5 = scan.nextLine();

            if (response1.equalsIgnoreCase("y"))
                number += 1;
            if (response2.equalsIgnoreCase("y"))
                number += 2;
            if (response3.equalsIgnoreCase("y"))
                number += 4;
            if (response4.equalsIgnoreCase("y"))
               开发者_运维百科 number += 8;
            if (response5.equalsIgnoreCase("y"))
                number += 16;

            return number;
        }

    }
}

ok well the porgram i supposed to guess the number you are thinking about between 0 to 31. now i was asked to choose 5 sets with their complements (i used binaries to come up with the sets). Now even though i have 10 sets in total i am only suppose to show 5 (this is where the random comes in play) i'm supposed to make it choose between the complement and original randomely so let's say in one set is gonna show the first complement and then ask me if my number is in that said i said no then the next set of number shows (this time is the original of the second set). what i can't figure out is how to do that...for the first question to radomely choose between the original and the complement && then do the same but for the second questions and so forth till the fifth question.


For starters, since you're creating an instance of java.util.Random - how about actually using it? Then, since you wanto to represent sets of numbers, try usingjava.util.HashSet to represent those.

What are you trying to achieve with your code? I can't make heads nor tails of it. Try using meaningful variable names and breaking it up into smaller methods.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号