要使用Scanner类输入字符串,可以使用next()或者nextLine()方法。1. 使用next()方法输入字符串:该方法会读
要使用Scanner类输入字符串,可以使用next()或者nextLine()方法。
1. 使用next()方法输入字符串:该方法会读取输入的字符串直到遇到空格或者换行符为止。java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入字符串:");
String str = scanner.next();
System.out.println("输入的字符串是:" + str);
}
}
2. 使用nextLine()方法输入字符串:该方法会读取输入的一行字符串,包括空格和换行符。java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入字符串:");
String str = scanner.nextLine();
System.out.println("输入的字符串是:" + str);
}
}
无论使用next()还是nextLine()方法,都需要在输入字符串之前输出提示信息,以便用户知道需要输入什么类型的数据。
--结束END--
本文标题: scanner如何输入字符串
本文链接: https://www.lsjlt.com/news/430820.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-15
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0