简体   繁体   中英

The first user input(number) in the 2d array should be less than or equal to other element in the 2d array

The program will ask the user for the size of rows and columns of the 2d array. Afterwards, the program will let the user enter values inside the 2d array.

I have my sample code, but my problem is: the first number(double) in the array that the user inputted should be less than or equal to other elements that he will be inputting. (for ex: [[2.0, 3.1], [6.0, 4.0], [8.2, 9.9]] ) - this is okay because the first element is the lowest .

But if the user inputs 1.9 or lower, the program will tell the user that he should input another row again. Can u pls help me improve this.

Sample code:

Scanner scan = new Scanner(System.in);

    System.out.print("How many rows? ");
    Integer rows = Integer.valueOf(scan.nextLine());

    System.out.print("How many columns?");
    Integer columns = Integer.valueOf(scan.nextLine());

    double two_d[][] = new double[rows][columns];

    int i, j;

    for (i = 0; i < rows; i++) {
        System.out.print("Enter " + columns + " numbers seperated by comma (" + (i + 1) + " row): ");
        String[] line = scan.nextLine().split(",");
        for (j = 0; j < columns; j++) {
            two_d[i][j] = Double.parseDouble(line[j]);
        }

    }

    for (i = 0; i < rows; i++) {
        for (j = 0; j < columns; j++) {
            System.out.print(two_d[i][j] + " ");
        }
        System.out.println();
    }

    scan.close();
    }

Try the following:

class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        System.out.print("How many rows? ");
        int rows = Integer.valueOf(scan.nextLine());

        System.out.print("How many columns?");
        int columns = Integer.valueOf(scan.nextLine());

        double two_d[][] = new double[rows][columns];

        int i, j;

        for (i = 0; i < rows; i++) {
            System.out.print("Enter " + columns + " numbers seperated by comma (" + (i + 1) + " row): ");
            String[] line = scan.nextLine().split(",");
            double low = Double.MIN_VALUE;
            for (j = 0; j < columns; j++) {
                two_d[i][j] = Double.parseDouble(line[j]);
                if (two_d[i][j] < low) {
                    System.out.println("Error you number is to small"); //Set an error message
                    i--; //Restart for this row
                }
                else
                  low = two_d[i][j];
            }

        }

        for (i = 0; i < rows; i++) {
            for (j = 0; j < columns; j++) {
                System.out.print(two_d[i][j] + " ");
            }
            System.out.println();
        }

        scan.close();
    }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM