使用Java練習演算法常用的基本操作

來源:互聯網
上載者:User

標籤:

一、使用Java練習演算法常常需要使用控制台的資料輸入和輸出,下面記錄一下基本的使用方法:

基本用法

1 import java.util.*;2 public class Main {3     public static void main(String[] args){4         Scanner cin = new Scanner(System.in);     //常用Scanner類來擷取資料5         int a = cin.nextInt();                    //將輸入的資料轉換成int型賦給a,輸入資料以空格或Tab分隔,Enter執行6         System.out.println(a);                    //控制台輸出7     }

常用:

 1 import java.util.*; 2 public class Main { 3     public static void main(String[] args){ 4         Scanner cin = new Scanner(System.in);     //常用Scanner類來擷取資料 5         while (cin.hasNext()) {                   //迴圈擷取 6             int i = cin.nextInt();                //擷取int型資料 7             float f = cin.nextFloat();            //擷取float型資料 8             String s = cin.next();                //擷取字串 9             char[] charArrary = s.toCharArray();  //將String轉換成字元數組10             String s2 = cin.nextLine();           //擷取一行,不被空格或Tab打斷11             System.out.println(s2);                12         }13     }14 }
 二、常見的字串操作
  1. 字串長度:length()
  2. 子字串位置:indexOf(), lastIndexOf()
  3. 字串比較:"==" 和 equals()方法
  4. 大小寫字母轉換:toLowerCase();toUpperCase()
  5. 指定位置的字元:charAt()
  6. 子字串:subString()
  7. 替換:replace()
  8. 合并:concat()
  9. 追加:append()
  10. 插入:insert()
  11. 分割:split()
  12. 轉換成字元數組toCharArray()
  13. 任意類型轉換成字串:String.valueOf()
  14. 判斷:startsWith(), endsWith()

使用Java練習演算法常用的基本操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.