Java常用类之String类、Stringbuffer和Random类练习
生活随笔
收集整理的這篇文章主要介紹了
Java常用类之String类、Stringbuffer和Random类练习
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
- 定義一個StringBuffer類對象,
- 1)使用append方法向對象中添加26個字母,并倒序遍歷輸入
- 2)刪除前五個字符
運行結果:
2.使用Random類產生5個1~30的隨機整數
package 第十一章常用類;import java.util.Arrays; import java.util.Random;//使用Random類產生5個1~30的隨機整數 public class Test2 {public static void main(String[] args){Random r=new Random();int[] arr=new int[5];//賦值for(int i=0;i<arr.length;i++){//1~30arr[i]=r.nextInt(30)+1;}System.out.println(Arrays.toString(arr));} }3.使用隨機數0和1來模擬拋硬幣實驗,統計拋出1000次后正反面出現的次數并輸出,正面為1,反面為0
package 第十一章常用類;import java.util.Random;//使用隨機數0和1來模擬拋硬幣實驗,統計拋出1000次后正反面出現的次數并輸出 //正面為1,反面為0 public class Test4 {public static void main(String[] args){Random r=new Random();int countPostive=0;int countNegtive=0;for(int i=0;i<1000;i++){//產0或1int a=r.nextInt(2);if(a==0){countNegtive++;}if(a==1){countPostive++;}}System.out.println("1出現的次數:"+countPostive+",0出現的次數:"+countNegtive);} }運行結果:
4.* 給定如下HTML代碼:
* 要求對內容進行拆分,拆分后的結果是(特別注釋:只能使用spilt方法,不允許使用subString方法):
face Aerial,Serial
size +2
color red
運行結果:
總結
以上是生活随笔為你收集整理的Java常用类之String类、Stringbuffer和Random类练习的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java常用类之String类练习
- 下一篇: Java异常类(Throwable)