zoukankan      html  css  js  c++  java
  • javaString使用练习

    1. 编写程序,求一个三阶方阵的对角线上各元素之和。

     1 package stringDiagonal;
     2 
     3 import java.util.Scanner;
     4 
     5 public class Diagonal {
     6     public static void main(String[] args) {
     7         
     8         int[][] data=new int[3][3];
     9         int mainSum=0;
    10         int sideSum=0;
    11         
    12         Scanner scan=new Scanner(System.in);
    13 
    14         for(int i=0;i<3;i++) {
    15             for(int j=0;j<3;j++){
    16                 data[i][j]=scan.nextInt();
    17             }
    18         }
    19         scan.close();
    20         
    21         for(int i=0;i<3;i++) {
    22             for(int j=0;j<3;j++){
    23                 if(i==j){
    24                     mainSum+=data[i][j];
    25                 }
    26                 if(i+j==2) {
    27                     sideSum+=data[i][j];
    28                 }
    29             }
    30         }
    31         
    32         System.out.println("The main diagonal: "+mainSum);
    33         System.out.println("The side diagonal: "+sideSum);
    34 
    35     }
    36 }

    2. 编写程序,从键盘上输入一个字符串和子串开始位置与长 度,截取该字符串的子串并输出。

     1 package srtringSunstring;
     2 
     3 import java.util.Scanner;
     4 
     5 public class Substring {
     6     public static void main(String[] args) {
     7         
     8         System.out.println("Enter <STRING BEGINNUMBER LENGTH>:");
     9         
    10         Scanner scan=new Scanner(System.in);
    11         String s=scan.nextLine();
    12         int begin=scan.nextInt();
    13         int length=scan.nextInt();
    14         scan.close();
    15         
    16         String subS=s.substring(begin, begin+length);
    17         
    18         System.out.println(subS);        
    19         
    20     }
    21 }

    3. 编写程序,统计用户从键盘输入的字符串中包含的字母、数 字和其他字符的个数。

     1 package stringAnalysis;
     2 
     3 import java.util.Scanner;
     4 
     5 public class Analysis {
     6     public static void main(String[] args) {
     7     
     8         System.out.println("Enter <STRING>:");
     9         
    10         Scanner scan=new Scanner(System.in);
    11         String s=scan.nextLine();
    12         scan.close();
    13         
    14         char[] ss=s.toCharArray();
    15         
    16         int letter=0;
    17         int number=0;
    18         int blank=0;
    19         int other=0;
    20         
    21         int len=s.length();
    22         for(int i=0;i<len;i++){
    23             if(Character.isLetter(ss[i])) {
    24                 letter++;
    25             }else if(Character.isDigit(ss[i])) {
    26                 number++;
    27             }else if(Character.isSpaceChar(ss[i])) {
    28                 blank++;
    29             }else {
    30                 other++;
    31             }        
    32         }
    33         
    34         System.out.println("letter: " + letter);
    35         System.out.println("number: " + number);
    36         System.out.println("blank: " + blank);
    37         System.out.println("other: " + other);
    38         
    39     }
    40 }

    来支舞吧!

  • 相关阅读:
    人工神经网络(Artificial Neural Networks)
    潜语义分析(Latent Semantic Analysis)
    IOS Dictionary和Model相互转换
    jquery ajax跨域请求webservice
    日期格式转换
    1
    iptables详解
    yum报错-Network is unreachable"Error:
    41个Web开发者JavaScript实用小技巧
    比较常用的几个maven第三方镜像
  • 原文地址:https://www.cnblogs.com/qingdaodaozhu/p/15334042.html
Copyright © 2011-2022 走看看