程序读取十个数字

程序读取十个数字

本文介绍了程序读取十个数字,并显示不同数字的数量和由一个空格分隔的不同数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题以前已经被问过了,而不是我正在编写我的代码的格式。只是开始使用java类,所以我不熟悉任何复杂的java ..下面的代码基本上是所有的java我知道。请帮忙!提前致谢。

I know that this question has been asked before, but not in the the format that I'm writing my code.. Just started taking java classes so I am not familiar with any complex java.. the code below consists of basically all the java I know. Please help! Thanks in advance.

import java.util.Scanner;
public class problem2try {

    public static void main(String[] args) {
        //declarations
        Scanner keyboard = new Scanner (System.in);
        int [] inputList = new int [10];
        int [] distinctArray = new int [10];
        int num;
        int counter = 0;

        //input
        System.out.print("Please enter in 10 integers: ");

        for (int i = 0; i < inputList.length; i++)
        {
            num = keyboard.nextInt();
            inputList[i] = num;
        }

        //processing
        distinctArray[0] = inputList[0];
        for (int i = 1; i < inputList.length; i++)
        {
            for (int j = 0; j < inputList.length; j++)
            {
                if (inputList[i] == inputList[j])
                {
                    counter++;
                    continue;
                }
                else
                {
                    distinctArray[i] = inputList[i];
                }
            }
        }

        //output
        System.out.println("The number of distinct numbers is " + counter);
        System.out.print("The distict numbers are: ");
        for (int x=0; x<distinctArray.length; x++)
        {
            if (distinctArray[x] != 0)

                System.out.print(distinctArray[x] + " ");
        }
    }
}


推荐答案

您在处理块中的逻辑似乎已经关闭了。我修改它来检查当前数字(外部循环)到所有已知数字(内部循环)。如果没有找到匹配项,它将附加到已知号码列表中,并且计数增加。

Your logic in the "processing" block seemed off. I modified it to check the current number (outer loop) to all of the known numbers (inner loop). If no match was found, it is appended to the list of known numbers and the count is incremented.

我还修改了输出代码来打印第一个 counter 已知号码列表中的数字。

I also modified the "output" code to print the first counter numbers from the list of known numbers. Values past that index are uninitialized.

import java.util.Scanner;
public class problem2try {

    public static void main(String[] args) {
        //declarations
        Scanner keyboard = new Scanner (System.in);
        int [] inputList = new int [10];
        int [] distinctArray = new int [10];
        int num;
        int counter = 0;

        //input
        System.out.print("Please enter in 10 integers: ");

        for (int i = 0; i < inputList.length; i++)
        {
            num = keyboard.nextInt();
            inputList[i] = num;
        }

        //processing
        for (int i = 0; i < inputList.length; i++)
        {
            boolean found = false;
            for (int j = 0; j < counter; j++)
            {
                if (inputList[i] == distinctArray[j])
                {
                    found = true;
                    break;
                }
            }
            if (!found)
            {
                distinctArray[counter++] = inputList[i];
            }
        }

        //output
        System.out.println("The number of distinct numbers is " + counter);
        System.out.print("The distict numbers are: ");
        for (int x=0; x<counter; x++)
        {
            System.out.print(distinctArray[x] + " ");
        }
    }
}

这篇关于程序读取十个数字,并显示不同数字的数量和由一个空格分隔的不同数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 02:03