简体   繁体   English

创建随机生成器以生成和计算奇偶数对

[英]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.涉及:Java,while循环,随机生成数字,扫描仪控制台输入。

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.我们必须在 main 方法中完成代码,以便它从控制台获取两个单独的输入并生成一些“滚动”,然后当它们是一对数字时显示结果,一个偶数,一个奇数。

Edit: It was pointed out to me my phrasing was confusing.编辑:有人指出我的措辞令人困惑。 Joseph Larson phrased it better: Joseph Larson 措辞更好:

"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.如果这些问题得到解决,我很确定我可以找出 rest。

1) I know I'm supposed to do something to complete the while loop, but nothing I've tried gets the required results. 1)我知道我应该做一些事情来完成while循环,但我尝试过的任何事情都没有得到所需的结果。

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. 2)我认为我错误地声明了 randUpBound 和 oddeven 项目,但我不知道如果我有我可能做错了什么。

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.最奇怪的部分是我的大多数尝试都创建了一个空白的无限循环 - 没有显示任何内容,但 IntelliJ 发誓程序正在运行,并且在我让它停止之前它不会停止。 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 12

Enter number of odd even pairs to count?输入要计数的奇偶对数? 2 2

Numbers rolled: 11, 2掷出的数字:11、2

  1. Odd+even pair found, 11,2找到奇数+偶数对,11,2

Numbers rolled: 1, 8掷出的数字:1、8

  1. Odd+even pair found, 1, 8找到奇数+偶数对,1, 8

Numbers rolled: 1, 1掷出的数字:1, 1

Total Roll count: 6总卷数:6

Code代码

import java.util.*;导入 java.util.*; //added //添加

public class OddEvenPairs { public static void main(String[] args) {公共 class OddEvenPairs { 公共 static 无效主(字符串 [] 参数) {

    //.....[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 的变量名称更改为“numberOfTries”之类的名称。 Oddeven doesn't really explain what it's really for. Oddeven 并没有真正解释它的真正用途。

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.在该循环内,您将生成两个随机数,然后调用您的 oddEvenPair 方法。 Then just print out the results.然后打印出结果。

Your function is really weird and probably doesn't do what you want.你的 function 真的很奇怪,可能不会做你想做的事。

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:你真的有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.用这些术语来思考问题,然后每个步骤的代码量真的很小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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