使用for循环在一个随机、未排序的数组中查找出现多次的元素。

huangapple go评论45阅读模式
英文:

Using for loop to find an element that appears multiple times in a random, unsorted array

问题

我的目标是在一个随机生成的包含50个整数的数组中,如果某个用户输入的值出现1次或更多次,就打印出该值及其相应的索引。如果我搜索数组中的一个值,而它恰好出现多次,那么只会打印出该元素的一个位置,然后执行if-else语句。如果我删除第三个for循环末尾的break语句,整个程序就会崩溃。这是导致我问题的部分。如果代码不够清晰,我表示抱歉,我是新手。

public static void main(String[] args) {
    System.out.println("IDS201 HW3:\n");
    System.out.println("1. Generate 50 random integer unsorted list.\n");

    Scanner stdin = new Scanner(System.in);

    int[] randomNumbers = new int[50];
    for (int index = 0; index < randomNumbers.length; index++) {
        randomNumbers[index] = (int) (Math.random() * 100);
    }//end for

    int count = 0;
    for (int i = 0; i < randomNumbers.length; i++) {
        System.out.print(randomNumbers[i] + ",");
        count++;
        if (count == 10) {
            System.out.println();
            count = 0;
        }
    }//end for

    System.out.println("\nSearch value?");
    int x = stdin.nextInt();
    int i;
    for (i = 0; i < randomNumbers.length; i++) {
        if (randomNumbers[i] == x)
            break;
    }

    if (i != randomNumbers.length) {
        System.out.println("\nFound " + x + " in array [" + i + "]");
    } else {
        System.out.println(x + " is not in the list");
    }

    {
        int temp;
        int size = randomNumbers.length;

        for (i = 0; i < size; i++) {
            for (int j = i + 1; j < size; j++) {
                if (randomNumbers[i] > randomNumbers[j]) {
                    temp = randomNumbers[i];
                    randomNumbers[i] = randomNumbers[j];
                    randomNumbers[j] = temp;
                }
            }
        }
        System.out.println("\nSmallest element of the array is: " + randomNumbers[0]);
    }
    System.out.println("\n3. Sort the list:");
    int size = randomNumbers.length;
    for (i = 0; i < size; i++) {
        for (int j = i + 1; j < size; j++) {
            if (randomNumbers[i] > randomNumbers[j]) {
                int temp = randomNumbers[i];
                randomNumbers[i] = randomNumbers[j];
                randomNumbers[j] = temp;
            }
        }
    }

    System.out.print("Now the Array after Sorting is :\n\n");
    int count1 = 0;
    for (i = 0; i < size; i++) {
        System.out.print(randomNumbers[i] + ",");
        count++;
        if (count == 10) {
            System.out.println();
            count = 0;
        }
    }
}

在for循环中的if语句

英文:

My goal is to print out a user input value (and its corresponding index) if it appears 1 or more times in a random generated array of 50 integers. If I search the array for a value, and it happens to appear more than once, however, only one location of the element is printed, and then the if-else statement is executed. If I remove the break at the end of the third for loop, the whole thing falls apart. I've attached an image but here is the part of it that is giving me an issue. Apologies if the code is not clean, I'm very new.

public static void main(String[] args) {
System.out.println(&quot;IDS201 HW3:\n&quot;);
System.out.println(&quot;1. Generate 50 random integer unsorted list.\n&quot;);
Scanner stdin = new Scanner(System.in);
int[] randomNumbers = new int[50];
for(int index = 0; index &lt; randomNumbers.length; index++) {
randomNumbers[index] = (int) (Math.random()*100);
}//end for
int count = 0;
for(int i = 0; i &lt; randomNumbers.length; i++) {
System.out.print(randomNumbers[i] + &quot;,&quot;);
count++;
if(count == 10) {
System.out.println();
count = 0;
}
}//end for
System.out.println(&quot;\nSearch value?&quot;);
int x = stdin.nextInt();
int i;
for(i = 0; i &lt; randomNumbers.length; i++) {
if(randomNumbers[i] == x) 
break;}
if (i != randomNumbers.length) {
System.out.println(&quot;\nFound &quot; + x + &quot; in array [&quot; + i + &quot;]&quot;);}
else {
System.out.println(x + &quot; is not in the list&quot;);}
{int temp;
int size = randomNumbers.length;
for(i = 0; i&lt;size; i++ ){
for(int j = i+1; j&lt;size; j++){
if(randomNumbers[i]&gt;randomNumbers[j]){
temp = randomNumbers[i];
randomNumbers[i] = randomNumbers[j];
randomNumbers[j] = temp;
}
}
}
System.out.println(&quot;\nSmallest element of the array is: &quot; + randomNumbers[0]);}
System.out.println(&quot;\n3. Sort the list:&quot;);
int size = randomNumbers.length;
for(i=0; i&lt;size; i++)  
{  
for(int j=i+1; j&lt;size; j++)  
{  
if(randomNumbers[i] &gt; randomNumbers[j])  
{  
int temp = randomNumbers[i];  
randomNumbers[i] = randomNumbers[j];  
randomNumbers[j] = temp;  
}  
}  
}  
System.out.print(&quot;Now the Array after Sorting is :\n\n&quot;); 
int count1 = 0;
for(i=0; i&lt;size; i++)  
{  
System.out.print(randomNumbers[i]+ &quot;,&quot;);
count++;
if(count == 10) {
System.out.println();
count = 0;
}
}  
}		

}

if statement in for loop

答案1

得分: 0

I've translated the code as requested:

import java.util.Scanner;

public class NumCount {
    private static final int RANDOM_NUMBER_COUNT = 50;

    private static void display(int[] randomNumbers) {
        int count = 0;
        for (int i = 0; i < RANDOM_NUMBER_COUNT; i++) {
            System.out.print(randomNumbers[i] + ",");
            count++;
            if (count == 10) {
                System.out.println();
                count = 0;
            }
        }
    }

    private static int[] generateRandomNumbers() {
        int[] randomNumbers = new int[RANDOM_NUMBER_COUNT];
        for (int index = 0; index < RANDOM_NUMBER_COUNT; index++) {
            randomNumbers[index] = (int) (Math.random() * 100);
        }
        display(randomNumbers);
        return randomNumbers;
    }

    private static int search(int[] randomNumbers, int x) {
        int i;
        int count = 0;
        for (i = 0; i < randomNumbers.length; i++) {
            if (randomNumbers[i] == x) {
                System.out.println("\nFound " + x + " in array [" + i + "]");
                count++;
            }
        }
        return count;
    }

    private static int[] sort(int[] randomNumbers) {
        int size = randomNumbers.length;
        for (int i = 0; i < size; i++) {
            for (int j = i + 1; j < size; j++) {
                if (randomNumbers[i] > randomNumbers[j]) {
                    int temp = randomNumbers[i];
                    randomNumbers[i] = randomNumbers[j];
                    randomNumbers[j] = temp;
                }
            }
        }
        return randomNumbers;
    }

    /**
     * Start here.
     */
    public static void main(String[] args) {
        System.out.println("IDS201 HW3:\n");
        System.out.println("1. Generate " + RANDOM_NUMBER_COUNT + " random integer unsorted list.\n");
        int[] randomNumbers = generateRandomNumbers();
        System.out.print("\n2. Search value? ");
        Scanner stdin = new Scanner(System.in);
        int x = stdin.nextInt();
        int count = search(randomNumbers, x);
        if (count == 0) {
            System.out.println(x + " is not in the list");
        }
        System.out.println("\n3. Sort the list:");
        sort(randomNumbers);
        System.out.print("Now the Array after Sorting is :\n\n");
        display(randomNumbers);
    }
}

I've made the necessary code adjustments and removed the smallest number search part as you mentioned.

英文:

Implementing the comments to your question:

import java.util.Scanner;

public class NumCount {
    private static final int  RANDOM_NUMBER_COUNT = 50;

    private static void display(int[] randomNumbers) {
        int count = 0;
        for (int i = 0; i &lt; RANDOM_NUMBER_COUNT; i++) {
            System.out.print(randomNumbers[i] + &quot;,&quot;);
            count++;
            if (count == 10) {
                System.out.println();
                count = 0;
            }
        }
    }

    private static int[] generateRandomNUmbers() {
        int[] randomNumbers = new int[RANDOM_NUMBER_COUNT];
        for (int index = 0; index &lt; RANDOM_NUMBER_COUNT; index++) {
            randomNumbers[index] = (int) (Math.random() * 100);
        }
        display(randomNumbers);
        return randomNumbers;
    }

    private static int search(int[] randomNumbers, int x) {
        int i;
        int count = 0;
        for (i = 0; i &lt; randomNumbers.length; i++) {
            if (randomNumbers[i] == x) {
                System.out.println(&quot;\nFound &quot; + x + &quot; in array [&quot; + i + &quot;]&quot;);
                count++;
            }
        }
        return count;
    }

    private static int[] sort(int[] randomNumbers) {
        int size = randomNumbers.length;
        for (int i = 0; i &lt; size; i++) {
            for (int j = i + 1; j &lt; size; j++) {
                if (randomNumbers[i] &gt; randomNumbers[j]) {
                    int temp = randomNumbers[i];
                    randomNumbers[i] = randomNumbers[j];
                    randomNumbers[j] = temp;
                }
            }
        }
        return randomNumbers;
    }

    /**
     * Start here.
     */
    public static void main(String[] args) {
        System.out.println(&quot;IDS201 HW3:\n&quot;);
        System.out.println(&quot;1. Generate &quot; + RANDOM_NUMBER_COUNT + &quot; random integer unsorted list.\n&quot;);
        int[] randomNumbers = generateRandomNUmbers();
        System.out.print(&quot;\n2. Search value? &quot;);
        Scanner stdin = new Scanner(System.in);
        int x = stdin.nextInt();
        int count = search(randomNumbers, x);
        if (count == 0) {
            System.out.println(x + &quot; is not in the list&quot;);
        }
        System.out.println(&quot;\n3. Sort the list:&quot;);
        sort(randomNumbers);
        System.out.print(&quot;Now the Array after Sorting is :\n\n&quot;);
        display(randomNumbers);
    }
}

Of-course there is no need to search for the smallest number because it will be the first element in the sorted array. Hence I removed that part of your code.

huangapple
  • 本文由 发表于 2020年7月28日 14:19:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/63128127.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定