gpt4 book ai didi

java - 声明数组格式

转载 作者:太空宇宙 更新时间:2023-11-04 11:24:52 26 4
gpt4 key购买 nike

我有制作 50-100 之间的 8 个随机数字数组的代码,但我不知道如何让我的偶数和赔率计数器工作,以及如何将 8 个数字的数组相加总计。有人可以帮我吗?

问题:

声明一个数组来保存八个整数。使用 for 循环向此数组添加 8 个随机整数(全部在 50 到 100 范围内(含))。重复的没问题。接下来,将数组传递给对数组进行排序的方法,并返回另一个仅包含原始数组中最大和最小元素的数组。在 main.c 中打印这两个值然后使用 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

最佳答案

为了解决问题的第二部分,您可以这样做:

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
}

要从 main 方法中使用上述内容:

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++;
}
}

这是完整的代码。我写的这是一个文本文件,所以我不 promise 任何事情。正如您所看到的,我接受了作业并将每个句子分解为评论,以便您可以了解需要如何看待这些事情。

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;
}
}

关于java - 声明数组格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44491149/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com