简体   繁体   中英

Create random generator to make and count odd even pairs of numbers

I'm really, really stuck and confused. I've net searched several times, and can't find anything that helps me with this precise homework problem.

Involved: Java, while loops, randomly generating numbers, and scanner console input.

We have to finish the code in main method so it takes two separate inputs from the console and generates a number of "rolls", then displays the results when they are a pair of numbers, one even, one odd.

Edit: It was pointed out to me my phrasing was confusing. Joseph Larson phrased it better:

"You are to ask for the upper bound of the random numbers, and then a number of times to run, correct?" Yes, that's it.

I have two primary problems. If these get fixed, I'm fairly sure I can figure out the rest.

1) I know I'm supposed to do something to complete the while loop, but nothing I've tried gets the required results.

2) I think I've declared the randUpBound and oddeven items incorrectly, but I can't figure out what I might have done wrong if I have.

The weirdest part is most of my attempts have created a blank infinite loop -nothing is displayed, but IntelliJ swears the program is running, and it doesn't stop until I make it stop. Not even the strings in quotes appear.

Expected display and code below. I've stuck //added to the lines where it's my code, and left in the teacher's instructions.

Thanks for any help you can give!

Expected Display

Enter random upper bound? 12

Enter number of odd even pairs to count? 2

Numbers rolled: 11, 2

  1. Odd+even pair found, 11,2

Numbers rolled: 1, 8

  1. Odd+even pair found, 1, 8

Numbers rolled: 1, 1

Total Roll count: 6

Code

import java.util.*; //added

public class OddEvenPairs { public static void main(String[] args) {

    //.....[add in missing code here - make declarations and add console input for the random number upper bound,
    // and the number of odd-even pairs to be counted]

    //read two consecutive numbers - fencepost
    Scanner console = new Scanner(System.in); //added
    Random rand = new Random(); //added

    int randUpBound = console.nextInt(); //added
    int oddeven = console.nextInt(); // added
    System.out.println("Enter random upper bound? " + randUpBound); //added
    System.out.println("Enter number of odd even pairs to count? " + oddeven); //added

    int roll1 = rand.nextInt(randUpBound);
    int roll2 = rand.nextInt(randUpBound);
    System.out.println("Numbers  " + roll1 + ", " + roll2);
    int rollcount = 2;

    int oddEvenNum = roll1 + roll2;

    //process the numbers
    while (oddeven < oddEvenNum) {
        oddeven = oddEvenPair(roll1, roll2, oddeven);
        roll1 = rand.nextInt(randUpBound);
        roll2 = rand.nextInt(randUpBound);
        System.out.println("Numbers  " + roll1 + ", " + roll2);
        rollcount += 2;
        //.....[complete missing code here]

    }

}

//method to figure out odd-even pair
public static int oddEvenPair(int roll1, int roll2, int oddeven) {
    //boolean oddEvenFound = false;
    if (roll1 % 2 == 1) {

        if (roll2 % 2 == 0) {
            //oddEvenFound = true;
            oddeven++;
            System.out.println("Odd even " + oddeven);
            System.out.println("Odd+even pair found!" + roll1 + "," + roll2);
        }
    }
    return oddeven;

}

}

Okay, there are a few problems with your code. First, this:

int randUpBound = console.nextInt(); //added
int oddeven = console.nextInt(); // added
System.out.println("Enter random upper bound? " + randUpBound); //added
System.out.println("Enter number of odd even pairs to count? " + oddeven); //added

These will happen in order, which means you'll accept input from the console and THEN prompt the user. This will be confusing.

Next, I would change the variable name for oddeven to something like "numberOfTries". Oddeven doesn't really explain what it's really for.

Once you have those two numbers, you need a loop.

for (int thisTry = 1; thisTry <= numberOfTries; ++thisTry) {
    ...
}

This will then run your code the proper number of times.

Inside that loop is where you will generate your two random numbers and then call your oddEvenPair method. Then just print out the results.

Your function is really weird and probably doesn't do what you want.

Here's the thing. You let yourself get confused by thinking about too much at once. Break it down. How would you do it on paper? Break it into little pieces. You really have 5:

-Get data from the user (with prompts)

-Loop the proper number of times

-Generate two random numbers in the proper range

-Determine both odd/both even/one of each

-Print the results

Think about things in those terms, and then the amount of code for each step is REALLY small.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM