我真的非常困惑。 我已经进行过几次网络搜索,但找不到任何可以帮助我解决这一精确作业问题的信息。
涉及:Java,while循环,随机生成数字和扫描仪控制台输入。
我们必须完成main方法中的代码,以便它从控制台获取两个单独的输入并生成多个“ rolls”,然后在结果为一对数字(偶数,奇数)时显示结果。
编辑:有人向我指出我的措辞令人困惑。 约瑟夫·拉森(Joseph Larson)说得更好:
“您要询问随机数的上限,然后多次运行,对吗?” 对,就是那样。
我有两个主要问题。 如果这些问题得到解决,我很确定我可以解决其余问题。
1)我知道我应该做些事情来完成while循环,但是我尝试过的任何事情都无法获得所需的结果。
2)我认为我错误地声明了randUpBound和奇偶项目,但我无法弄清楚如果我做错了什么。
最奇怪的部分是我的大多数尝试都创建了一个空白的无限循环-不显示任何内容,但是IntelliJ发誓该程序正在运行,并且直到我将其停止后它才会停止。 甚至引号中的字符串也不会出现。
预期的显示和代码如下。 我已经将//添加到了我的代码所在的行中,并留在了老师的说明中。
谢谢你提供的所有帮助!
预期展示
输入随机上限? 12
输入奇偶对数以计数? 2
滚动数字:11、2
发现奇数+偶数对! 11,2
滚动数字:1、8
发现奇数+偶数对! 1、8
滚动数字:1、1
总卷数:6
码
导入java.util。*; //添加
公共类OddEvenPairs {公共静态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;
}
}