Showing Multiple Variables in Console with Scanner

时间:2018-07-09 00:56:52

标签: java java.util.scanner

The problem is when you entry an input with scanner ,it shows on console. I want them to shown in an order. I want them shown like a matris. But with nextInt method all shows bottom of each other.

I want a console output like this:

this

But with nextInt() method your new int shows on nextLine like this:

this

How can i show multiple variables in same line with scanner?

import java.util.Scanner;

public class ProbilityMatrixTest {

static int M;
static int N;
static float[][] matrixX;
static float[][] matrixY;
static boolean isProbilityMatrix;

public static void main(String[] args) {
    initiate();
    testMatrix(matrixX);
    System.out.println();
    multiplyMatrix();
    testMatrix(matrixY);
}

public static void initiate() {
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter the row and column size of matrix : ");
    M = sc.nextInt();
    N = sc.nextInt();
    System.out.println();


    matrixX = new float[M][N];
    System.out.println("Enter values of " + M + "x" + N + " matrix :");
    for (int j = 0; j < N; j++) {
        for (int i = 0; i < M; i++) {
            matrixX[i][j] = sc.nextFloat();
        }
    }
}

public static void testMatrix(float[][] givenMatrix) {
    isProbilityMatrix = true;
    if (M != N) {
        isProbilityMatrix = false;
    }
    for (int j = 0; j < N; j++) {
        float rowVariablesTotal = 0;
        for (int i = 0; i < M; i++) {
            rowVariablesTotal += givenMatrix[i][j];
            if (givenMatrix[i][j] < 0) {
                isProbilityMatrix = false;
            }
        }
        if (rowVariablesTotal != 1.0f) {
            isProbilityMatrix = false;
        }
    }
    System.out.print("TEST RESULT : ");
    if (isProbilityMatrix) {
        System.out.println("Probility matrix");
    } else {
        System.out.println("not Probility matrix");
    }
}

public static void multiplyMatrix() {

        matrixY = new float[M][N];
        for (int i = 0; i < M; i++) {
            for (int j = 0; j < N; j++) {
                float newMatrixVariable = 0;
                for (int a = 0; a < M; a++) {
                    newMatrixVariable += (matrixX[i][a] * matrixX[a][j]);
                }
                matrixY[i][j] = newMatrixVariable;
            }
        }
        System.out.println("The square of given matrix:");
        for (int j = 0; j < M; j++) {
            for (int i = 0; i < N; i++) {
                System.out.print(matrixY[i][j] + "  ");
            }
            System.out.println();
        }


}

}

Thats

1 个答案:

答案 0 :(得分:1)

您需要一次扫描整行。否则,您总是按Enter键,使它看起来就像您在前一行中输入另一个值之前

例如,键入3 3,然后输入,然后您可以键入三个以空格分隔的十进制值,输入,然后重复两次

System.out.print("Enter the row and column size of matrix : ");
String[] mn = sc.nextLine().split("\\s+");
int M = Integer.parseInt(mn[0]);
int N = Integer.parseInt(mn[1]);
System.out.println();

double[][] matrixX = new double[N][];
for (int i = 0; i < N; i++) {
    matrixX[i] = new double[M];
    String[] row = sc.nextLine().split("\\s+");
    for (int j = 0: j < M: j++) {
        matrix[i][j] = Double.parseDouble(row[j]);
        //... 
    } 
}