Hello, world!(蓝桥杯)

题目:

输入ASCII值输出字符串java

输入:

一串ASCII码值,用空格隔开数组

72 101 108 108 111 44
32 119 111 114 108 100 33

输出:

Hello, world!

程序:

package com.cwstd.test;

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext())
        {
            String s=sc.nextLine();
            String ss[]=s.split(" ");
            for(String x:ss)
            {
                int x1=Integer.parseInt(x);
                System.out.print((char)x1);//将int型的ASCII值转化为char型字符
            }
        }
    }
    }

知识总结:

String类中的split()方法

split()方法,有两个参数split(String regex,int limit),按字符串regex来切割,返回一个字符串数组,数组储存的是切割后的子串。参数limit为切割后数组长度,当limit为1时不进行切割,当limit为负值或者不赋值就是执行切割到无限次。code

Scanner sc = new Scanner(System.in);
        while(sc.hasNext())
        {
            String s=sc.nextLine();
            String ss[]=s.split(" ",-1);//String ss[]=s.split(" ");不传参也是同样的效果
            for(String x:ss)
            {
                System.out.print(x);
            }
        }

1 2 3 4 5 6 7 8 9 10字符串

输出:
12345678910string

Scanner sc = new Scanner(System.in);
        while(sc.hasNext())
        {
            String s=sc.nextLine();
            String ss[]=s.split(" ",2);
                System.out.println(ss[0]);
                System.out.print(ss[1]);

        }

1 2 3 4 5 6it

输出:
1
2 3 4 5 6class

相关文章
相关标签/搜索