6月22星期二之-java作業


1、

答案:ABC

2、從鍵盤輸入一個字符串
   編寫一個程序,判斷輸出一個字符串中大寫英文字母數,和小寫英文字母數,和其他非英文字母數

package org.jsoft.homework2;

//輸入一個字符串,判斷有多少大寫字母、小寫字母、非英文字母 import java.util.Scanner; public class CharacterString { public static void main(String[] args) { System.out.println("請輸入一個字符串"); Scanner sc = new Scanner(System.in); String string = sc.nextLine(); int notCaseCount = 0; int upperCaseCount = 0; int lowerCaseCount = 0; for (int i = 0; i < string.length(); i++) { char c = string.charAt(i);// 返回索引處i的char值 if (Character.isUpperCase(c)) { // 大寫字母 upperCaseCount += 1; } else if (Character.isLowerCase(c)) { // 小寫字母 lowerCaseCount += 1; } else { // 非英文字母 notCaseCount += 1; } } System.out.println("大寫字母數量" + upperCaseCount); System.out.println("小寫字母數量" + lowerCaseCount); System.out.println("非英文字母數量" + notCaseCount); } } 

3、編寫一個方法,返回一個double類型的二維數組,數組中的元素通過解析字符串參數獲得,如字符串參數為“1,2;3,4,5;6,7,8;9”的參數

package org.jsoft.homework2; import java.util.Arrays; public class DoubleArray { public static void main(String[] args) { String str="1,2;3,4,5;6,7,8;9"; double pda[][]=parseDoubleArray(str); for(int i=0;i<pda.length;i++) { for(int j=0;j<pda[i].length;j++) { System.out.print(pda[i][j]+" "); } System.out.println(); } } //解析二維數組的方法 public static double[][] parseDoubleArray(String arraystr){ double[][] ds = new double[arraystr.split(";").length][];//定義一個包含;個一維數組的二維數組 for (int i = 0; i < ds.length; i++) { String temp = arraystr.split(";")[i];//以;將字符串拆開 String[] temp1 = temp.split(",");//以,將字符串拆開 double[] tempArray = new double[temp1.length]; for (int j = 0; j < tempArray.length; j++) { tempArray[j] = Double.valueOf(temp1[j]);// valueof返回保存用參數字符串 表示的 double 值的 Double 對象。 } ds[i] = tempArray; } return ds; } } 

4、比較String和StringBuffer給定字符串后面加50000個字符串aa的時間差

package org.jsoft.seatwork;

public class CompareStringAndStringBuffer { public static void main(String[] args) { //String給定字符串后面加50000個字符串aa的時間差 String str1 = "abc"; long l1 = System.currentTimeMillis(); System.out.println(l1); for (int i = 0; i < 50000; i++) { str1+="aa"; } long l2 = System.currentTimeMillis(); System.out.println(l2); //StringBuffer給定字符串后面加50000個字符串aa的時間差 long l3 = System.currentTimeMillis(); System.out.println(l3); StringBuffer str2 = new StringBuffer("abc"); for (int i = 0; i < 50000; i++) { str2.append("aa"); } long l4 = System.currentTimeMillis(); System.out.println(l4); } } 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM