1。从键盘输入一个字符串
编写一个程序,计算输出一个字符串中大写英文字母数,和小写英文字母数,和其他非英文字母数
2.
编写一个方法,返回一个double类型的二维数组,数组中的元素通过解析字符串参数获得,
如字符串参数为“1,2;3,4,5;6,7,8;9”的参数
/*
*第一题
*/
package org.jsoft.zuoye;
import java.util.Scanner;
public class CharactersMum {
public static void main(String[] args) {
//输出一个字符串中大写英文字母数,和小写英文字母数,和其他非英文字母数
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
String str = sc.nextLine();
char []c = new char [str.length()];
c = str.toCharArray();//转化成字符数组
int n1=0,n2=0,n3=0;
for(int i = 0; i < c.length; i++){
if(c[i]>='A'&&c[i]<='Z'){
n1++;//大写字母数
}else if(c[i]>='a'&&c[i]<='z'){
n2++;//小写字母数
}else{
n3++;//非英文字母数
}
}
System.out.println("大写字母数:"+n1);
System.out.println("小写字母数:"+n2);
System.out.println("其他非字母数:"+n3);
}
sc.close();
}
}
/*
* 第二题
*/
package org.jsoft.zuoye;
public class ParseString {
//编写一个方法,返回一个double类型的二维数组,数组中的元素通过解析字符串参数获得
//如字符串参数为“1,2;3,4,5;6,7,8;9”的参数
public static double[][] parseString(String str){
//用split()方法分解
//1.先split(";")分解成一维(行)
//2.在用split(",")分解成二维(列)
//3.用一个二位数组装分解好的字符
double [][]result;
String []sFirst = str.split(";");//第一次分解
int l = sFirst.length;
result = new double [l][];//先设置多少行
for(int i = 0 ; i < l ; i++){
String sSecond[] = sFirst[i].split(",");//第二次分解
int l2 = sSecond.length;
result[i] = new double [l2];//再设置多少列
for(int j = 0 ; j < l2 ; j++){
result[i][j] = Double.valueOf(sSecond[j]);//将String转换成Double
}
}
return result;
}
public static void main(String[] args) {
String s = "1,2;3,4,5;6,7,8;9";
double arraySecond [][];
arraySecond = parseString(s);
//输出二维数组
for(int i = 0 ; i < arraySecond.length ; i++){
for(int j = 0 ; j <arraySecond[i].length ; j++){
System.out.print(arraySecond[i][j]+" ");
}
System.out.println();
}
}
}