【问题标题】:How to use an array in for loop to calculate minimum?如何在for循环中使用数组来计算最小值?
【发布时间】:2012-11-01 15:00:48
【问题描述】:

我正在尝试使用数组,但我不确定这是否是正确的方法。

我希望第一个和第二个整数输入相互比较,然后如果有更多它们相互比较。

下面是这段代码。

for(int ii = 1, j = 0; j <= copySel ; ii++, j++) {     
             int x = 0;
             int[x] check;
                // Prompt as follows
                System.out.print("Enter value " + ii + ": ");
                try {
                    c = Get();
                }
                catch (InputMismatchException e) {
                    // Display the following text in the event of an invalid input
                    System.out.println("Invalid input!");
                    ii--; x--;
                } check = c; x++;    

                System.out.print(check + " "+ x);
        }

我的实际格式并没有这么糟糕。我需要尝试根据用户输入的整数数找到最小值

static int Get()
    {
        Scanner intFind = new Scanner(System.in);
        int select;
        select = intFind.nextInt();
        return select;
    }

这是Get() ^

我不能连续使用 min(x, y) 吗?

【问题讨论】:

  • 这不是作业吗?还有什么问题?
  • 如果是作业,请添加它的标签。我也加了
  • 有人告诉我不要这样做,因为他们很快就会删除。
  • @FahimParkar 几个月前,作业标签已被弃用。来自标签说明 **此标签已过时,正在被删除。但是不要在不查看问题以查看它是否需要清理的情况下将其删除。请参阅this question on Meta 了解更多信息。 **

标签: java arrays for-loop


【解决方案1】:

我没有完全理解您想要做什么,但这里有一些您可能会考虑修复的错误。如果 check 是一个数组,你必须这样初始化它:

int[] check;

get() 是否会返回一个整数数组,如果无法使 check = c,则必须将 c 的内容复制或克隆到 check 中:

check = (int[])c.clone();

【讨论】:

  • 我必须得到最小值
  • 我需要尝试根据用户输入的整数个数找到最小值
  • 好吧,那么最直接的方式就是使用Mandar的方式,
  • 我不能连续使用 min(x, y) 吗?
【解决方案2】:
 System.out.print("Enter value " + ii + ": ");
 int min = Get();
 int c = 0;
 for(int ii = 1, j = 0; j < copySel ; ii++, j++) { 

            // Prompt as follows
            System.out.print("Enter value " + ii + ": ");
            try {
                c = Get();
            }
            catch (InputMismatchException e) {
                // Display the following text in the event of an invalid input
                System.out.println("Invalid input!");
                ii--;
            } min = Math.min(min, c);   

            System.out.print("minimum is:"+ min);
    }

【讨论】:

  • 如果你想使用你的代码,你可以使用以下代码,你没有使用克隆,因为 Get() 不返回整数数组,但它只返回 1 个整数
  • 非常好。感谢您的帮助。
【解决方案3】:
 Scanner in = new Scanner(System.in);
 System.out.println("Enter the integers: ");
 String s = in.nextLine();
 string[] str = s.plit(" ");
 int[] a = new a[str.length];
 for(int i =0; i< str.length; i++)
 {
     a[i] = Integer.parseInt(str[i]);
 }
 //Madar's code
 int min=a[0];

for(int i=0;i<a.length;i++)
{
    min = Math.min(a[i], min);
}

System.out.println("The min is "+min);

【讨论】:

  • 我不能连续使用 min(x, y) 吗?
  • 是的,你确实可以代替 if 语句,你可以写 min = Math.min(a[i], min);
【解决方案4】:

如果你有一个整数数组,你可以这样做

  Integer [] arr = {5,2,3,4,5,6,7,8};
  List<Integer> list = new ArrayList<Integer>(Arrays.asList(arr));
  Collections.sort(list);
  System.out.println("Minimum "+list.get(0)); ;

【讨论】:

    【解决方案5】:
    int min=a[0];
    
    for(int i=1;i<n;i++)
    {
        if(a[i] < min) 
             min = a[i];
    }
    
    System.out.println("The min is "+min);
    

    【讨论】:

    • 这是什么意思,是不是在原来的for循环中?
    • 这里的 n 是什么我认为 n 是 a.length
    • 我不能连续使用 min(x, y) 吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-18
    • 1970-01-01
    • 1970-01-01
    • 2012-11-01
    • 2013-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多