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