我有一个代码,用于在50-100之间制作一个8个随机数字的数组,但我无法弄清楚如何让我的偶数和赔率计数器工作,以及如何添加8个数字的数组总计。有人能帮帮我吗?
问题:
声明一个数组以保存8个整数。使用for循环将8个随机整数添加到此数组,所有这些整数的范围均为50到100(包括50和100)。重复是可以的。接下来,将数组传递给对数组进行排序的方法,并返回另一个仅包含原始数组中最大和最小元素的数组。在main中打印这两个值。然后使用foreach循环在由一个空格分隔的一行上显示已排序数组的所有元素。后一个循环还应该计算数组中的奇数和偶数,并确定数组中所有元素的总和。
我的代码到目前为止:
import java.util.Arrays;
import java.util.Random;
public class Assignment1 {
private static Random rand = new Random();
public static void main(String[] args){
int[] randomEight = numbers();
int[] smallestLargest = sortArrayAndReturnSmallestLargest(randomEight);
System.out.println("Here is the Lowest number ");
System.out.println((smallestLargest[0]));
System.out.println("Here is the Largest number ");
System.out.println((smallestLargest[1]));
System.out.println("Here is the array");
System.out.println(Arrays.toString(randomEight));
System.out.println();
}
public static int[] numbers(){
int evenCount = 0;
int oddCount = 0;
int[] randomEight = new int[8];
for (int x = 0; x < 8; x++){
randomEight [x] = rand.nextInt((100 - 50) + 1) + 50;
for(int item: randomEight){
if(item % 2 == 0){
evenCount++;
} else {
oddCount++;
}
}
}
System.out.println("Here is the Evens: ");
System.out.println(evenCount);
System.out.println("Here is the Odds");
System.out.println(oddCount);
return randomEight;
}
public static int[] sortArrayAndReturnSmallestLargest(int[] randomEight){
int[] smallestLargest = new int[2];
Arrays.sort(randomEight);
smallestLargest[0] = randomEight[0];
smallestLargest[1] = randomEight[7];
return smallestLargest;
}
}
我的输出:
Here is the Evens:
53
Here is the Odds
11
Here is the Lowest number
53
Here is the Largest number
96
Here is the array
[53, 54, 57, 58, 62, 75, 80, 96]
示例输出:
The lowest element is 59
The highest element is 96
Here is the array
59 64 76 77 80 88 91 96
Evens: 5, odds: 3
Total: 631
答案 0 :(得分:0)
我无法弄清楚如何传递数组并为所有数据排序 问题的不同方式
你不需要对数组进行排序以获得这些问题的答案,只需使用自定义方法。
*最低元素:* 在数组中执行循环并使用变量设置最低值..
*最高元素:* 与之前的方法相同
*这是数组:* 我认为你完成了
* Evens和赔率:* 循环数组并检查x%2 == 0或x%2!= 0 (提示:你只需要做其中一种方法....记得 evens + odds = arrays.size )
*总计:* 循环数组并使用变量来累积结果
答案 1 :(得分:0)
为了解决问题的第二部分,您可以这样做:
private int[] sortArrayAndReturnSmallestLargest(int[] randomArray){
int[] smallestLargest = new int[2];
Arrays.sort(randomArray);
smallestLargest[0] = randomArray[0];
smallestLargets[1] = randomArray[randomArray.size()-1];
return smallestLargest
}
从主方法中使用上述内容:
int[] smallestLargest = sortArrayAndReturnSmallestLargest(randomEight)
我不想为你完成整个家庭作业。我的目的是告诉你如何做一些对你来说还不舒服的事情。根据说明,您应该能够使用下面的循环来计算,总结和打印您需要做的大部分内容。
为了获得你想要创建计数器的平均值和赔率的计数,循环randomArray并根据int值是偶数还是奇数增加每个计数。您可以执行以下操作:
int evenCount = 0;
int oddCount = 0;
for(int item: randomEight){
//Use modulo to determine even vs odd
if(item % 2 == 0){
evenCount++;
} else {
oddCount++;
}
}
这是完整的代码。我写这是一个文本文件,所以我不承诺任何事情。正如你所看到的那样,我完成了作业并将每个句子分解为评论,这样你就可以看到你需要如何看待这些内容。
import java.util.Arrays;
import java.util.Random;
public class Assignment1 {
public static void main(String[] args){
private static Random rand = new Random();
int evenCount = 0;
int oddCount = 0;
int sum = 0;
//Declare an array to hold eight integers.
int[] arrayWithEightInts = new int[8];
//Use a for loop to add eight random integers, all in the range from 50 to 100, inclusive, to this array. Duplicates are okay.
for (int x = 0; x < 8; x++){
arrayWithEightInts [x] = rand.nextInt((100 - 50) + 1) + 50;
}
//Next, pass the array to a method that sorts the array and returns another array containing only the largest and smallest elements in the original array.
int[] smallestLargest = sortArrayAndReturnSmallestLargest(arrayWithEightInts);
//Print these two values in main.
System.out.println(“The lowest element is ” + smallestLargest[0];
System.out.println(“The highest element is ” + smallestLargest[1];
//Then use a foreach loop to display all elements of the sorted array on one line separated by a single space. This latter loop should also count the odd and even numbers in the array and determine the sum of all elements in the array.
System.out.println(“Here is the array”);
for(int item: arrayWithEightInts){
sum = sum + item;
System.out.print(item + “ ”);
if(x % 2 == 0){
evenCount++;
} else {
oddCount++;
}
}
System.out.println();
System.out.println(“Evens: ” + evenCount + “, odds: “ + oddCount);
System.out.println(“Total: ” + sum);
}
public static int[] sortArrayAndReturnSmallestLargest(int[] randomEight){
int[] smallestLargest = new int[2];
Arrays.sort(randomEight);
smallestLargest[0] = randomEight[0];
smallestLargest[1] = randomEight[randomEight.size()-1];
return smallestLargest;
}
}
答案 2 :(得分:0)
Michel,每次更新同一个问题都很难跟踪变化。在退回之前,请先退后一步并运行代码。这里的问题出在你的for循环中:
Line1:for (int x = 0; x < 8; x++){
Line2: randomEight [x] = rand.nextInt((100 - 50) + 1) + 50;
Line3: for(int item: randomEight){
Line4: if(item % 2 == 0){
Line5: evenCount++;
Line6: } else {
Line7: oddCount++;
Line8: }
Line9: }
Line10: }
对于上面的代码,它是这样的。
在此理解你的循环运行n * n次,但它不应该这样做。您只需要检查现在插入数组的数字。
请更改您的代码,如下所示。希望这有帮助
for (int x = 0; x < 8; x++){
randomEight [x] = rand.nextInt((100 - 50) + 1) + 50;
if(isPrime(randomEight [x])){
evenCount++;
}else{
oddCount++;
}
}
private static boolean isPrime(int item){
boolean flag = false;
if(item % 2 == 0){
flag = true;
} else {
flag = false;
}
return flag;
}