今天在练习时,发现Java没有输入单个字符的函数,查询课本后发现,Java输入单个字符需要先输入字符串后通过charAt(s)方法返回字符串中特定位置的字符,具体代码如下:
import java.util.Scanner;
public class str {
public static void main(String[] args) {
String st;
String st1;
Scanner input=new Scanner(System.in);
st1=input.nextLine();
st=input.nextLine();
char ch=st1.charAt(0);//charAt(index)会返回字符串中index位置的字符
System.out.println(ch+st);
input.close();
}
}
输出结果如下:
因笔者水平有限,如有错误,还望指正。