欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

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(" ");