hasNext VS hasNextLine
hasNext()(检查是否有更多的输入内容可供读取)(用于逐个读取输入中的标记(例如单词、数字)时,通常与 next() 搭配使用)(读取完标记后,光标会停留在该标记的结尾位置)
hasNext() 会查看入流中是否还有下一个可用的标记(token)。一个标记通常被空格、换行符、制表符等分隔。
如果输入流中有未读的标记,hasNext() 返回 true;否则返回 false。
Hello World
123 456使用 hasNext()
- 第一次调用
hasNext()返回true,next()会返回Hello。 - 第二次调用
hasNext()返回true,next()会返回World。 - 第三次调用
hasNext()返回true,next()会返回123。 - 第四次调用
hasNext()返回true,next()会返回456。 - 再调用
hasNext()将返回false。
hasNextLine()(检查是否有更多的输入行可供读取)(用于逐行读取输入时,与 nextLine() 搭配使用) (读取完行内容后,光标会移到下一行的开头位置)
hasNextLine() 会查看输入流中是否还有下一行。它检测是否还有一个完整的行(由换行符分隔的内容)可供读取。
如果有下一行,hasNextLine() 返回 true;否则返回 false。
Hello World
123 456使用hasNextLine()
- 第一次调用
hasNextLine()返回true,nextLine()会返回Hello World。 - 第二次调用
hasNextLine()返回true,nextLine()会返回123 456。 - 再调用
hasNextLine()将返回false。
next()和nextline()混合使用
在使用 Scanner 类时,next() 和 nextLine() 的混合使用可能会导致一些意外行为,特别是在读取输入时,因为这两个方法的工作方式不同。
可能的问题
当你混合使用 next() 和 nextLine() 时,可能会出现一些问题。以下是常见的场景:
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = scanner.next(); // 使用 next() 读取名字
System.out.print("Enter your address: ");
String address = scanner.nextLine(); // 使用 nextLine() 读取地址
System.out.println("Name: " + name);
System.out.println("Address: " + address);预期行为
- 用户首先输入名字,然后按回车,程序读取并显示名字。
- 然后用户输入地址,按回车,程序读取并显示地址。
实际行为
- 用户输入名字,比如
John,并按回车。 -
next()方法读取John,光标停在John后面。 - 此时,换行符仍在输入缓冲区中。
- 当程序执行
nextLine()时,它会读取剩余的内容(即空的换行符)并直接返回一个空字符串。
解决方法
为了避免这种情况,可以在调用 nextLine() 之前,添加一个额外的 nextLine() 来消耗掉缓冲区中的换行符:
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = scanner.next(); // 使用 next() 读取名字
scanner.nextLine(); // 消耗掉上一次输入后的换行符
System.out.print("Enter your address: ");
String address = scanner.nextLine(); // 现在可以正常读取地址
System.out.println("Name: " + name);
System.out.println("Address: " + address);总结
-
next() -
nextLine() - 混合使用时,要注意
next()不会读取换行符,因此在使用nextLine()之前需要手动处理换行符。
数组
一维数组
单组_一维数组

import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
//方法一
// // 注意 hasNext 和 hasNextLine 的区别
// in.nextInt();
// Long sum=0L;
// while (in.hasNextInt()) { // 注意 while 处理多个 case
// sum+= in.nextInt();
// // int b = in.nextInt();
// // System.out.println(a + b);
// }
// System.out.println(sum);
//方法二
int count=in.nextInt();
Long sum=0L;
for(int i=0;i<count;i++){
sum+=in.nextInt();
}
System.out.println(sum);
}
}多组_一维数组_T组形式

import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int count1 = in.nextInt();
Long sum;
int count2;
for (int i = 0; i < count1; i++) {
sum=0L;
count2 = in.nextInt();
for (int j = 0; j < count2; j++) {
sum += in.nextInt();
}
System.out.println(sum);
}
}
}二维数组
单组_二维数组

import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int count1 = in.nextInt();
Long sum=0L;
int count2=in.nextInt();
for (int i = 0; i < count1; i++) {
for (int j = 0; j < count2; j++) {
sum += in.nextInt();
}
}
System.out.println(sum);
}
}多组_二维数组_T组形式

import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int count3 = in.nextInt();
for (int k = 0; k < count3; k++) {
int count1 =in.nextInt();
Long sum = 0L;
int count2 = in.nextInt();
for (int i = 0; i < count1; i++) {
for (int j = 0; j < count2; j++) {
sum += in.nextInt();
}
}
System.out.println(sum);
}
}
}字符串
单组_字符串

import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
scanner.nextLine(); // 消耗掉上一行输入后的换行符
String s = scanner.nextLine();
StringBuilder strBuilder=new StringBuilder(s);
strBuilder.reverse();
System.out.println(strBuilder);
}
}多组_字符串_T组形式

import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
scanner.nextLine(); // 消耗掉上一行输入后的换行符
for(int i=0;i<n;i++){
scanner.nextLine(); // 消耗掉上一行输入后的换行符
String s = scanner.nextLine();
StringBuilder strBuilder=new StringBuilder(s);
strBuilder.reverse();
System.out.println(strBuilder);
}
}
}单组_二维字符数组

import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
scanner.nextLine(); // 消耗掉上一行输入后的换行符
StringBuilder[] strBuilderArr=new StringBuilder[n];
for(int i=0;i<n;i++){
String s = scanner.nextLine();
StringBuilder strBuilder=new StringBuilder(s);
strBuilder.reverse();
strBuilderArr[i]=strBuilder;
}
for(int i=n-1;i>=0;i--){
System.out.println( strBuilderArr[i]);
}
}
}多组_带空格的字符串_T组形式

import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
scanner.nextLine(); // 消耗掉上一行输入后的换行符
for(int i=0;i<n;i++){
scanner.nextLine();
String s = scanner.nextLine();
StringBuilder strBuilder=new StringBuilder();
for(int j=0;j<s.length();j++){
if(s.charAt(j)!=' '){
strBuilder.append(s.charAt(j));
}
}
strBuilder.reverse();
System.out.println(strBuilder);
}
}
}格式化输出
//1. %s , %d , %.2f %c 称为占位符
//2. 这些占位符由后面变量来替换
//3. %s 表示后面由 字符串来替换
//4. %d 是整数来替换
//5. %.2f 表示使用小数来替换,替换后,只会保留小数点两位, 并且进行四舍五入的处理
//6. %c 使用 char 类型来替换单组_保留小数位数(System.out.println 不能直接使用格式化字符串来输出带有格式的数字。应该使用 System.out.printf 或者 String.format 来实现格式化输出)



import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
double dd=in.nextDouble();
System.out.printf("%.3f",dd);
}
}单组_补充前导零(%09d)

import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
int num=in.nextInt();
System.out.printf("%09d",num);
}
}










