数字与字符串之间的相互转换
1 String str = "123";
2 int num = 12;
3 //字符串转换为数字
4 int tranToNum = Integer.parseInt(str, 16);//这里的16表示十六进制,也可以是十进制或是其他进制(如果不写,这里默认是10进制)
5 System.out.println(tranToNum);
6 //数字串转化为字符串
7 String tranToStr = String.valueOf(num);
8 System.out.println(tranToStr);
从键盘接受输入的字符串
1 //第一种
2 @SuppressWarnings("resource")
3 Scanner sc =new Scanner(System.in);
4 String str=sc.next();
5 System.out.println(str);
6 //第二种
7 @SuppressWarnings("resource")
8 Scanner sc1=new Scanner(System.in);
9 String str1= sc1.nextLine();
10 System.out.println(str1);
11 //第三种
12 Reader reader =new InputStreamReader(System.in);
13 BufferedReader bReader =new BufferedReader(reader);
14 //上二同 BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in));
15 try {
16 String str2=bReader.readLine();
17 System.out.println(str2);
18 } catch (IOException e) {
19 // TODO Auto-generated catch block
20 e.printStackTrace();
21 //第四种
22 Scanner s = new Scanner(System.in);
23 int i = 0;
24 if(s.hasNextInt()) //算法题中,整数和字符串同
25 {
26 i = s.nextInt();
27 }
28 s.close();//关闭scanner
除与取余
1 int i=123;
2 int a=i/100; //a=1
3 int b=(i%100)/10; //b=2
4 int c=i%10; //c=3