vendredi 1 novembre 2019

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.

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;

}

}




Aucun commentaire:

Enregistrer un commentaire