Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何返回包含10个随机整数的数组中的最大整数?_Java_Arrays_Random - Fatal编程技术网

Java 如何返回包含10个随机整数的数组中的最大整数?

Java 如何返回包含10个随机整数的数组中的最大整数?,java,arrays,random,Java,Arrays,Random,这是我在学校提出的一个编码问题,我不想说“嘿,伙计们,帮我做作业吧!”,我其实想了解这里发生了什么。我们刚开始使用数组,它们让我有些困惑,所以我正在寻求帮助。 下面是完整的问题: 编写一个程序,其中main方法使用 10个int型插槽。为每个插槽分配一个随机生成的 整数。调用函数,将其传递给数组。电话 函数应将数组中的最大整数返回给 你的主要方法。主方法应该显示数字 返回。使用随机对象生成整数。创造它 与 Random r=新随机数(7) 生成一个随机整数 x=r.nextInt() 到目前为止

这是我在学校提出的一个编码问题,我不想说“嘿,伙计们,帮我做作业吧!”,我其实想了解这里发生了什么。我们刚开始使用数组,它们让我有些困惑,所以我正在寻求帮助。 下面是完整的问题:

编写一个程序,其中main方法使用 10个int型插槽。为每个插槽分配一个随机生成的 整数。调用函数,将其传递给数组。电话 函数应将数组中的最大整数返回给 你的主要方法。主方法应该显示数字 返回。使用随机对象生成整数。创造它 与

Random r=新随机数(7)

生成一个随机整数

x=r.nextInt()

到目前为止,我的情况如下:

import java.util.Random;
public class Q1 {

    public static void main(String[] args) { 
    Random r = new Random(7);
    int[] count = new int[11];
    int x = r.nextInt();
    for (int i = 0; i < count.length; i++)
    {
        count[i] = x;
    }
}
import java.util.Random;
公开课Q1{
公共静态void main(字符串[]args){
随机r=新随机(7);
int[]计数=新的int[11];
int x=r.nextInt();
for(int i=0;i
我用10个
int
s创建了这个数组,然后使用
for
循环分配随机生成整数的每个插槽

不过,我很难确定下一步要做什么。我不确定要创建什么样的方法/函数,然后如何从中获取最大的
int
,并返回它


非常感谢您的帮助,因为我非常想了解这里发生了什么。谢谢!

将最大值初始化为数组的第一个值。然后使用for循环迭代数组,并用最大值检查数组当前值。
或者您可以对数组进行排序并返回。祝您好运!

您可以通过对数组使用一个简单的for循环来完成。 首先,您必须创建一个单独的int变量(例如:int a)并赋值为零(0),并且在循环的每次迭代中,您必须将数组项与变量a进行比较

a < count[i]
a
如果这是真的,你必须将计数[i]值分配给变量a。这个循环将一直持续到数组的最后一个索引,你将在a变量中拥有最大的数。因此,只需SYSOUT变量

重要:我没有在这里发布代码,因为我想让你理解这个概念,因为如果你理解了它,那么你将来可以自己解决这些问题。
希望这有帮助

下面是如何生成随机整数

publicstaticvoidmain(字符串[]args){
int[]计数=新的int[10];
随机r=新随机(7);
int x=0;
for(int i=0;i
下面是如何制作方法并从列表中获取最大数量

static int maxNumber(int[]mArray){//将int数组作为参数传递
int max=mArray[0];

对于(inti=0;i您的代码应该是这样的。阅读注释以理解它

public class Assignment {



    public static int findMax(int[] arr) {       // Defiine a function to find the largest integer in the array
        int max = arr[0];          // Assume first element is the largest element in the array
        for (int counter = 1; counter < arr.length; counter++)  // Iterate through the array 
        {
             if (arr[counter] > max)  // if element is larger than my previous found max
             {
              max = arr[counter]; // then save the element as max
             }
        }
        return max;  // return the maximum value at the end of the array
    }




    public static void main(String[] args) { 

    int numberofslots =10;

    int[] myIntArray = new int[numberofslots];  // creates an array with 10 slots of type int

    Random r = new Random(7);

    for (int i = 0; i < myIntArray.length; i++)  // Iterate through the array 10 times
    {

     int x = r.nextInt();
     myIntArray[i] = x;  // Generate random number and add it as the i th element of the array.
    }

    int result =  findMax(myIntArray); // calling the function for finding the largest value 
    System.out.println(result); // display the largest value

    }
}
公共类赋值{
public static int findMax(int[]arr){//define函数以查找数组中的最大整数
int max=arr[0];//假设第一个元素是数组中最大的元素
for(int counter=1;countermax)//if元素大于我以前找到的最大值
{
max=arr[counter];//然后将元素另存为max
}
}
return max;//返回数组末尾的最大值
}
公共静态void main(字符串[]args){
int numberofslots=10;
int[]myIntArray=new int[numberofslots];//创建一个包含10个int类型插槽的数组
随机r=新随机(7);
for(int i=0;i

希望您能通过阅读注释来理解代码。

到目前为止,您得到的几乎是正确的,但您当前在for循环的每个迭代中使用相同的随机数。尽管您需要为for循环的每个迭代获得一个新的随机数。这取决于随机对象的定义方式。您可以实现通过以下方式更改您的代码:

import java.util.Random;
public class Q1 {

    public static void main(String[] args) { 
    Random r = new Random(7);
    int[] count = new int[11];
    for (int i = 0; i < count.length; i++)
    {
        int x = r.nextInt(); // You need to generate a new random variable each time
        count[i] = x;
    }
}
int largest = 0; // Assuming all values in the array are positive.
for (int i = 0; i < count.length; i++)
        {
            if(largest < count[i]) { // Compare whether the current value is larger than the largest value so far
               largest = count[i]; // The current value is larger than any value we have seen so far, 
                                  // we therefore set our largest variable to the largest value in the array (that we currently know of)
               }
        }
import java.util.Random;
公开课Q1{
公共静态void main(字符串[]args){
随机r=新随机(7);
int[]计数=新的int[11];
for(int i=0;i
请注意,此代码不是最优的,但它是对已有代码的最小更改

要从数组中获取最大值,您需要编写另一个for循环,然后将数组中的每个值与迄今为止的最大值进行比较。您可以通过以下方法执行此操作:

import java.util.Random;
public class Q1 {

    public static void main(String[] args) { 
    Random r = new Random(7);
    int[] count = new int[11];
    for (int i = 0; i < count.length; i++)
    {
        int x = r.nextInt(); // You need to generate a new random variable each time
        count[i] = x;
    }
}
int largest = 0; // Assuming all values in the array are positive.
for (int i = 0; i < count.length; i++)
        {
            if(largest < count[i]) { // Compare whether the current value is larger than the largest value so far
               largest = count[i]; // The current value is larger than any value we have seen so far, 
                                  // we therefore set our largest variable to the largest value in the array (that we currently know of)
               }
        }
int max=0;//假设数组中的所有值都是正数。
for(int i=0;ipublic static void main(String[] args) {
        Integer[] randomArray = new Integer[10];
        randomArray[0] = (int)(Math.random()*100);
        int largestNum = randomArray[0];
        for(int i=1; i<10 ;i++){
            randomArray[i] = (int)(Math.random()*100);
            if(randomArray[i]>largestNum){
                largestNum = randomArray[i];
            }
        }
        System.out.println(Arrays.asList(randomArray));
        System.out.println("Largest Number :: "+largestNum);
    }
public int largestValue(){
  int largestNum;
  int[] nums = new int[10];
  for (int n = 0; n < nums.length; n++){
    int x = (int) (Math.random() * 7);
    nums[n] = x;
    largestNum = nums[0];
    if (largestNum < nums[n]){
      largestNum = nums[n];
    }
  }
  return largestNum;
}