1。从键盘输入一个字符串
编写一个程序,判断输出一个字符串中大写英文字母数,和小写英文字母数,和其他非英文字母数
import java.util.Scanner;
public class Work1 {
public static void main(String[] args){
System.out.println("请输入:");
Scanner scan = new Scanner(System.in);//键盘输入
String ss = scan.next();
int a1 = 0;
int a2 = 0;
int b = 0;
for(int i=0; i<ss.length(); i++){
char c = ss.charAt(i); //获取索引处字符
if('a'<=c && c<='z'){ //判断小写字母
a1++;
}else if('A'<=c && c<='Z'){ //判断大写字母
a2++;
}else{ //其他字符
b++;
}
}
System.out.println("大写字母:"+a1); //输出
System.out.println("小写字母: "+a2);
System.out.println("非英文字母:"+b);
}
}
2.
编写一个方法,返回一个double类型的二维数组,数组中的元素通过解析字符串参数获得,如字符串参数为“1,2;3,4,5;6,7,8;9”的参数
public class TestString
{
public static void main(String[] args)
{
double[][] d; //定义double类型二位数组
String s = "1,2;3,4,5;6,7,8;9";
String[] s1 = s.split(";"); //以;进行分割
d = new double[s1.length][];
for(int i=0; i<s1.length; i++)
{
String[] s2 = s1[i].split(","); //以,进行分割
d[i] = new double[s2.length];
for(int j=0; j<s2.length; j++)
{
d[i][j] = Double.parseDouble(s2[j]); //转为double类型
}
}
for(int i=0; i<d.length; i++) //循环输出
{
for(int j=0; j<d[i].length; j++)
{
System.out.print(d[i][j] + " ");
}
System.out.println();
}
}
}