Java笔试输入

通过Scanner类获取用户输入。

Scanner scan = new Scanner();

单行输入

next()方法

1、读取到有效字符后结束输入
2、将有效字符后的空格当作结束符

import java.util.*;
public class ScannerTest{
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		if (scan.hasNext()) {
            System.out.println(scan.next());//2021
        }
	}
}		

测试结果:
在这里插入图片描述

import java.util.*;
public class ScannerTest{
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		//hasNext()判断是否还有输入
		while(scan.hasNext()) {
            System.out.println(scan.next());
        }
	}
}		

测试结果:
在这里插入图片描述

nextLine()方法

1、以Enter为结束符,也就是说 nextLine()方法返回的是输入回车之前的所有字符。
2、可以获得空白。

public class Test1 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);//2021 03 23
        if(scan.hasNext()) {
            System.out.println(scan.nextLine());
        }
     }
}

测试结果:
在这里插入图片描述

public class Test1 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);//2021 03 23
        while(scan.hasNext()){
            String s = scan.nextLine();
            //spilt() 根据参数如","、" "等, 分割String字符串, 返回一个String的数组(String[])
            //trim()函数是去掉String字符串的首尾空格
            String[] strings = s.split(" ");
            for(int i = 0; i < strings.length; i++){
            	//Integer.parseInt()方法将字符转为int
                System.out.print(Integer.parseInt(strings[i]));
            }
         }
    }
}

测试结果:
在这里插入图片描述

多行输入

已知每行几个元素
import java.util.*;
public class Test1 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int len1 = scan.nextInt();
        int len2 = scan.nextInt();
        int[] nums1 = new int[len1];
        int[] nums2 = new int[len2];
        for(int i = 0; i < len1; i++){
            nums1[i] = scan.nextInt();
        }
        for(int i = 0; i < len2; i++){
            nums2[i] = scan.nextInt();
        }
        System.out.println("输出:");
        System.out.println(Arrays.toString(nums1));
        System.out.println(Arrays.toString(nums2));
    }
}

测试结果:
在这里插入图片描述

未知每行几个元素

while(sc.hasNextLine()) 判断是否还有下一行

public class Main2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        List<String> list = new ArrayList<>();
        while (sc.hasNextLine()) {
            list.add(sc.nextLine());
        }
        for (String s : list) {
            System.out.print(s + " ");
        }
    }
}

测试结果:
在这里插入图片描述
idea 默认ctr+D 结束输入

牛客练习

还会涉及到几种类型间的转换。

String转char数组

String s = "abx";
char[] ch = s.toCharArray();

char、char[]转String

char[] c = {'a','b','c'};
String s = String,valueOf(c[0]);//"a"
String s = String.valueOf(c);//"abc"

String转int

String s = "9";
int i = Integer.parseInt(s);//9

int 转String

int a = 9;
String s = String.valueOf(a);//"9"

char转int(不能强转)

char c = '9';
int res = c -'0';

如果直接(int)c 得到的是c 的ascii码值。

int转char

int a = 1;
cahr c = (char)(a + 48);	//'1'

如果进行强转,int 值当作ascii码值来转换成对应的字符。

int i = 97;
cahr c = (char) i;	//'a'

ascii码中 48-57 对应数字 0-9,65-90对应大写字母‘A’-‘Z’,97-122对应小写字母’a’-‘z’

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值