如何使用数组在for循环来计算最低? [英] How to use an array in for loop to calculate minimum?

查看:140
本文介绍了如何使用数组在for循环来计算最低?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用一个数组,但我不知道这是做的正确方法。

我想在第一和第二个整数输入到彼此比较,然后,如果有更多的它们相互比较。

因此​​,这里是一块code的。

 的for(int II = 1,J = 0; J< = copySel;二++,J ++){
             INT X = 0;
             INT [X]检查;
                //提示如下
                System.out.print(输入值+ II +:);
                尝试{
                    C = get()方法;
                }
                赶上(InputMismatchException时五){
                    //显示在输入无效的情况下,下面的文字
                    的System.out.println(无效的输入!);
                    ii--; X - ;
                }检查= C; X ++;                System.out.print(检查++ x)的;
        }

我的实际格式不是这个坏。我需要设法找到取决于整数的数目的最小值用户已输入

 静态INT的get()
    {
        扫描仪intFind =新的扫描仪(System.in);
        INT选择;
        选择= intFind.nextInt();
        返回选择;
    }

这是获得()^

我不能使用分钟(X,Y)持续?


解决方案

  System.out.print(输入值+ II +:);
 INT分钟= get()方法;
 INT C = 0;
 为(中间体II = 1,J = 0; J&下; copySel;ⅱ++ J ++){            //提示如下
            System.out.print(输入值+ II +:);
            尝试{
                C = get()方法;
            }
            赶上(InputMismatchException时五){
                //显示在输入无效的情况下,下面的文字
                的System.out.println(无效的输入!);
                ii--;
            }分钟= Math.min(分钟,C);            System.out.print(最低的是:+分);
    }

I am trying to use an array, but I am not sure if that is the right way to do this.

I want the first and second integer input to compare with one another, then if there are more they compare with each other.

So here is the piece of the code.

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);
        }

My actual format is not this bad. I need to try to find the minimum value depending on the number of integer the user has input

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

This is Get() ^

cant I use min(x, y) continuously?

解决方案

 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);
    }

这篇关于如何使用数组在for循环来计算最低?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆