Scanner中的nextInt()陷阱

本文详细介绍了 Java 中 Scanner 类的使用方法,特别是如何处理 nextInt() 和 nextLine() 方法之间的输入问题。通过示例代码解释了如何正确地接收用户的整数输入及随后的字符串输入,避免因输入缓冲区未清空导致的问题。
int choice=sc.nextInt(); // 用户输入(1  回车符) =>  回车符是一行字符串的结束标志      nextInt()读第一个输入的整型数字

//接着如果不加sc.nextLine();
String s1=sc.nextLine(); //  s1=“回车符”   \n  程序直接跳过用户输入,执行下一条语句

//而加上sc.nextLine();
String s1=sc.nextLine(); //  s1=你从键盘输入的值  程序会停下来让用户输入  

转载于:https://www.cnblogs.com/xijieblog/p/4540033.html

import java.util.Scanner; public class demo01 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter the dimensions for tensor A(rows,columns,layers):"); int rA = scanner.nextInt(); int cA = scanner.nextInt(); int lA = scanner.nextInt(); System.out.println("Enter the dimensions for tensor B(rows,columns,layers):"); int rB = scanner.nextInt(); int cB = scanner.nextInt(); int lB = scanner.nextInt(); if (cA != rB || lA != lB) { System.out.println("Dimension mismatch: A's columns must equal B's rows and layers must be equal."); return; } System.out.println("Enter the elements for tensor A:"); int[][][] A = new int[lA][rA][cA]; for (int k = 0; k < lA; k++) { for (int i = 0; i < rA; i++) { for (int j = 0; j < cA; j++) { A[k][i][j] = scanner.nextInt(); } } } System.out.println("Enter the elements for tensor B:"); int[][][] B = new int[lB][rB][cB]; for (int k = 0; k < lB; k++) { for (int i = 0; i < rB; i++) { for (int j = 0; j < cB; j++) { B[k][i][j] = scanner.nextInt(); } } } int[][] unfoldA = unfold(A, rA, cA, lA); int[][] unfoldB = unfold(B, rB, cB, lB); int[][] bcircA = bcirc(A, rA, cA, lA); int[][] product = multiply(bcircA, unfoldB); int[][][] C = fold(product, rA, cB, lA); for (int k = 0; k < lA; k++) { System.out.println("The " + k + "th slice is:"); for (int i = 0; i < rA; i++) { for (int j = 0; j < cB; j++) { System.out.print(C[k][i][j] + " "); } System.out.println(); } } scanner.close(); } public static int[][] unfold(int[][][] tensor, int rows, int columns, int layers) { // Unfold tensor to matrix: each frontal slice becomes a column int[][] result = new int[rows * columns][layers]; for (int k = 0; k < layers; k++) { int idx = 0; for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { result[idx][k] = tensor[k][i][j]; idx++; } } } return result; } public static int[][] bcirc(int[][][] A, int rows, int columns, int layers) { int[][] bcirc = new int[rows * layers][columns * layers]; for (int i = 0; i < rows * layers; i++) { for (int j = 0; j < columns * layers; j++) { int blockRow = i / rows; int blockCol = j / columns; int slice = (blockRow + blockCol) % layers; int rowInSlice = i % rows; int colInSlice = j % columns; bcirc[i][j] = A[slice][rowInSlice][colInSlice]; } } return bcirc; } public static int[][] multiply(int[][] a, int[][] b) { int rowsA = a.length; int colsA = a[0].length; int colsB = b[0].length; if (colsA != b.length) { throw new IllegalArgumentException("Matrix dimension mismatch for multiplication"); } int[][] result = new int[rowsA][colsB]; for (int i = 0; i < rowsA; i++) { for (int j = 0; j < colsB; j++) { for (int k = 0; k < colsA; k++) { result[i][j] += a[i][k] * b[k][j]; } } } return result; } public static int[][][] fold(int[][] matrix, int rows, int columns, int layers) { int[][][] result = new int[layers][rows][columns]; for (int k = 0; k < layers; k++) { for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { result[k][i][j] = matrix[k * rows + i][j]; } } } return result; } }找错
10-29
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值