Java 中的键盘输入语句
最编程
2024-04-28 17:20:05
...
Scanner scanner = new Scanner(System.in);
key = scanner.next().charAt(0);//接收一个字符 String s1=scanner.nextLine( ); //接收字符串,可以接收空格,回车为结尾但是可以接收 String s2=scanner.next( ); //接收字符串,但next()函数不会接收回车符和tab,或者空格键等 int in = scanner.nextInt();//接收一个int数值 double le=scanner.nextDouble( ); //接收一个double数值 float fl = scanner.nextFloat(); //接收一个float数值 scanner.close;
例子:
import java.io.*;
import java.util.*;
public class Main
{
public static void main(String args[])
{
Scanner c = new Scanner(System.in);
int a, b;
while(c.hasNextInt())
{
a = c.nextInt();
b = c.nextInt();
System.out.println(a + b);
}
}
}
Scanner in = new Scanner(System.in);
String name = in.nextLine();
List<String> strings = new ArrayList<>();
while(in.hasNextLine()){
String string = in.nextLine();
strings.add(string);
}
Scanner scanner = new Scanner(System.in);
String s1 = scanner.nextLine();
String[] ss1 = s1.trim().split(" ");
Scanner in = new Scanner(System.in);
String line1 = in.nextLine();
String line2 = in.nextLine();
String[] line1s = line1.split(",");
String[] line2s = line2.split(" ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// n 个数
int n= Integer.parseInt(br.readLine());
// n 个节点
Node[] nodes= new Node[n];
String[] s = br.readLine().split(" ");
推荐阅读
-
格林尼治标准时间?UTC?CST?进来聊聊时区(Java 中的时区)
-
Java 帮助:快速准确地删除 Word 文档中的空行
-
Java Swing 中实现为窗体添加背景图像的两种方法说明
-
了解并使用 JAVA 中的 set 和 get 方法
-
Java 中的构造函数(构造方法
-
教姐妹们学Java(二十五):了解Java中的构造方法(1)
-
请求幂等性的 java 实现 java 中的幂等性
-
在 Java 中,新建对象的内存区域是如何变化的?顺序是什么?
-
Java Series | 特派团中的远程热部署
-
深入分析 Java 中的 MyBatis Plus 注释 @InterceptorIgnore:拦截器行为的优雅控制