1.有一篇三行文字的文章,每行最多80个字符,编写一个类,由实参传递字符串,分别统计文章中大写字母、小写字母、数字、空格及其他字符的个数。
   分析:自定义方法需返回多个统计结果,可采用一个专门的数组存放统计结果,并将其作为方法参数,在被调方法中计数。

2. 在5×5魔方矩阵中,每一行、每一列、每一对角线上的元素之和都是相等的。试定义一个包含下列元素的一个二维整型数组,然后检验其是否是魔方矩阵,并将其按如下格式显示到屏幕上。
   17 24 1  8  15
   23 5  7  14 16
   4  6  13 20 22
   10 12 19 21 3
   11 18 25 2  9
   提示:通过这个实际例子熟悉数组类型的应用场合以及数组的编程与使用方法,进一步熟悉含有选择、循环结构的程序的设计方法。3. 分析并编写一个People类,再在新类中生成10个People类对象,并放在一个一维数组中,编写方法按身高进行排序。 

解决方案 »

  1.   

    第一题用char比较做
    第二题用多维数组做
    第三题有点面向对象的意思吧  无非就是一个实现Comparable接口  看看我的这个blog吧:Java中的排序(一)
      

  2.   

    [code]
    package com.tarena.wangweiwei.day07;
    import java.io.* ;public class Count
    {
    public static void main(String[] args)throws IOException
    {
    String str ;
    System.out.println("请输入(以回车键结束):") ;
    BufferedReader buf = new BufferedReader(new InputStreamReader(System.in)) ;
    str = buf.readLine() ;
    int lowCount , upCount , numCount , spaceCount , otherCount ;
    lowCount = lowCaseCount(str) ;
    upCount = upCaseCount(str) ;
    numCount = numCaseCount(str) ;
    spaceCount = spaceCaseCount(str) ;

    System.out.println("小写字母" + lowCount + "个!") ;
    System.out.println("大写字母" + upCount + "个!") ;
    System.out.println("空格字符" + spaceCount + "个!") ;
    System.out.println("数字字符" + numCount + "个!") ;
    }

    public static int lowCaseCount(String str)
    {
    int count = 0 ;
    for(int i = 0 ; i < str.length() ; i ++)
    if(str.charAt(i) >= 'a' && str.charAt(i) <= 'z')
    count ++ ;
    return count ;
    }
    public static int upCaseCount(String str)
    {
    int count = 0 ;
    for(int i = 0 ; i < str.length() ; i ++)
    if(str.charAt(i) >= 65 && str.charAt(i) < 97)
    count ++ ;
    return count ;
    }
    public static int numCaseCount(String str)
    {
    int count = 0 ;
    for(int i = 0 ; i < str.length() ; i ++)
    if(str.charAt(i) >= '0' && str.charAt(i) <= '9')
    count ++ ;
    return count ;
    }
    public static int spaceCaseCount(String str)
    {
    int count = 0 ;
    for(int i = 0 ; i < str.length() ; i ++)
    if(str.charAt(i) == ' ')
    count ++ ;
    return count ;
    }
    }
    [/code]
      

  3.   

    [code]
    package com.tarena.wangweiwei.day07;
    import java.io.* ;public class Count
    {
    public static void main(String[] args)throws IOException
    {
    String str ;
    System.out.println("请输入(以回车键结束):") ;
    BufferedReader buf = new BufferedReader(new InputStreamReader(System.in)) ;
    str = buf.readLine() ;
    int lowCount , upCount , numCount , spaceCount , otherCount ;
    lowCount = lowCaseCount(str) ;
    upCount = upCaseCount(str) ;
    numCount = numCaseCount(str) ;
    spaceCount = spaceCaseCount(str) ;

    System.out.println("小写字母" + lowCount + "个!") ;
    System.out.println("大写字母" + upCount + "个!") ;
    System.out.println("空格字符" + spaceCount + "个!") ;
    System.out.println("数字字符" + numCount + "个!") ;
    }

    public static int lowCaseCount(String str)
    {
    int count = 0 ;
    for(int i = 0 ; i < str.length() ; i ++)
    if(str.charAt(i) >= 'a' && str.charAt(i) <= 'z')
    count ++ ;
    return count ;
    }
    public static int upCaseCount(String str)
    {
    int count = 0 ;
    for(int i = 0 ; i < str.length() ; i ++)
    if(str.charAt(i) >= 65 && str.charAt(i) < 97)
    count ++ ;
    return count ;
    }
    public static int numCaseCount(String str)
    {
    int count = 0 ;
    for(int i = 0 ; i < str.length() ; i ++)
    if(str.charAt(i) >= '0' && str.charAt(i) <= '9')
    count ++ ;
    return count ;
    }
    public static int spaceCaseCount(String str)
    {
    int count = 0 ;
    for(int i = 0 ; i < str.length() ; i ++)
    if(str.charAt(i) == ' ')
    count ++ ;
    return count ;
    }
    }
    [/code]