![](/img/trans.png)
[英]given an array A of N integers, return the largest integer K>0 such that both K and -K(the opposite number) exist in array A
[英]How to return the largest integer in an Array that has 10 random integers in it?
所以這是我學校的編碼問題,我不想說“嘿伙計們為我做功課!”,我其實想知道這里發生了什么。 我們剛開始使用數組,它們讓我很困惑,所以我正在尋求幫助。 這是完整的問題:
編寫一個程序,其中main方法創建一個包含10個int類型的插槽的數組。 為每個槽分配一個隨機生成的整數。 調用一個函數,將數組傳遞給它。 被調用的函數應該將數組中的最大整數返回到main方法。 您的主要方法應顯示返回的數字。 使用Random對象生成整數。 用它創建它
Random r = new Random(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;
}
}
我使用10個int
創建了該數組,然后使用for
循環分配隨機生成的整數的每個插槽。
不過,我現在很難做下一步該做什么。 我不確定要創建什么樣的方法/函數,然后如何從那里獲取最大的int
並返回它。
任何幫助都非常感謝,因為我真的想了解這里發生了什么。 謝謝!
以下是如何生成隨機整數
public static void main(String[] args) {
int []count = new int[10];
Random r = new Random(7);
int x=0;
for (int i = 0; i < count.length; i++)
{
x = r.nextInt();
count[i] = x;
}
System.out.println("Max Number :"+maxNumber(count));}//Getting Max Number
以下是如何制作方法並從列表中獲取最大數量。
static int maxNumber(int[] mArray){//Passing int array as parameter
int max=mArray[0];
for(int i=0;i<mArray.length;i++){
if(max<mArray[i]){//Calculating max Number
max=mArray[i];
}
}
return max;//Return Max Number.
}
詢問是否有任何不清楚的地方。 這就是我們如何制作返回int的方法。
將最大值初始化為數組的第一個值。 然后使用for循環迭代數組並檢查具有最大值的數組當前值。 或者您可以sort
數組進行sort
並返回。 祝好運!
您可以通過對Array使用簡單的for循環來完成此操作。 首先,您必須創建一個單獨的int變量(例如:int a)並賦值零(0),並且在循環的每個迭代中,您必須將數組項與變量a進行比較。 像這樣
a < count[i]
如果確實如此,則必須將count [i]值賦給變量a 。 而這個循環將繼續下去,直到陣列的最后一個索引,你將不得不在一個 variabe的數量最多。 所以簡單地說SYSOUT一個變量
重要提示 :我沒有在此發布代碼,因為我希望您理解這個概念,因為如果您了解它,那么您將來可以解決這些問題。
希望這可以幫助
你的代碼應該是這樣的。 閱讀評論以了解它
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
}
}
希望您能通過閱讀評論來理解代碼。
到目前為止你所獲得的幾乎是正確的,但是你目前在for循環的每次迭代中使用相同的隨機數。 即使您需要為for循環的每次迭代獲取一個新的隨機數。 這是由於如何定義Random對象。 您可以通過以下方式更改代碼來實現此目的:
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;
}
}
請注意,此代碼不是最佳代碼,但它是您已有代碼的最小變化。
要從數組中獲取最大數字,您需要編寫另一個for循環,然后將數組中的每個值與目前為止的最大值進行比較。 您可以通過以下方式執行此操作:
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)
}
}
當然,這也不是最佳的,兩者都可以在同一個for循環中完成。 但這應該更容易理解。
這可以在一個簡單的for循環中完成,不需要有2個循環
public 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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.