zoukankan      html  css  js  c++  java
  • Java 算法笔记

    数字与字符串之间的相互转换

    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
  • 相关阅读:
    RecyclerView+CardView简单使用
    Android六大进程间通信方式总结之一:基本知识
    前台服务
    Notification
    SQLite的基本用法
    SharePreferences基本用法
    自定义控件和使用的两种基本方法
    AsyncTask的简单使用
    Java Servlet调用数据库复习
    JDBC数据源(DataSource)的简单实现
  • 原文地址:https://www.cnblogs.com/superslow/p/9016426.html
Copyright © 2011-2022 走看看