問題是當您使用掃描儀輸入輸入時,它會顯示在控制臺上。我希望它們按順序顯示。我希望他們像母艦一樣顯示。但是,使用nextInt方法時,彼此之間都顯示出底部。我想要這樣的控制臺輸出:但是使用nextInt()方法,新的int會在nextLine上顯示,如下所示:如何在掃描儀的同一行中顯示多個變量?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; } }}}
添加回答
舉報
0/150
提交
取消