Java 之 实验四 -- 字符串和Math类

发布时间 2023-09-18 17:55:54作者: Ivan丶c

实验四

字符串和Math类

练习一

填写以下空格
 (a)声明一个变量town,将其指向一个字符串类型,并初始化为“Anytown, USA”;
 (b)写一个赋值语句,调用字符串类的length方法,返回”college”字符串对象的长度,并将其赋值给变量stringLength;
 (c)完成赋值语句,使change1变量包含与college相同的字符,但是全部大写;
 (d)完成赋值语句,使change2包含与change1相同的字符,但是所有字符“O”全部要用“*”替换。
 (e)完成赋值语句,将colledge和town两个字符串连接起来,并把值赋给change3(使用String类的concat方法)。

//  **********************************************
//  StringPlay.java
//  
//  Play with String objects
//  **********************************************
public class StringPlay{
    public static void main (String[] argo){
        String college = new String ("RoDunk College") ;
        String town = new String("Anytown,USA");  // part (a)
        int stringLength;
        String change1,change2,change3;
        stringLength = college.length();  // part (b)
        System.out.println(college + " contains " + stringLength + " characters");
        change1 = new String(college.toUpperCase());  //part (c)
        System.out.println("The final string is "+ change1);
        change2 = new String(change1.replace("O","*")) ;  //part (d)
        System.out.println("The final string is "+ change2);
        change3 = college.concat(town);  //part (e)
        System.out.println("The final string is "+ change3);
    }
}

练习二

  以下程序读入直角三角形的两条直边的值,然后计算斜边的边长(斜边的计算方法:直边平方之和的平方根)。将程序里面的空格填写完整,注意要使用Math类的方法。

import java.util.Scanner;
import java.math.*;

public class RightTriangle {
    public static void main(String[] args) {
        double side1,side2;
        double hypotenuse;
        Scanner scan=new Scanner(System.in);
        System.out.print ( "Please enter the lengths of the two sides of " +
            " a right triangle (ceparate by a blank space) : ");
        side1=scan.nextDouble();
        side2=scan.nextDouble();
        hypotenuse=Math.sqrt(Math.pow(side1, 2)+Math.pow(side2, 2));
        
        System.out.println ("Length of the hypotenuse: " + hypotenuse) ;
        scan.close();
    }
}

练习三

以下程序阐述了String类的一些用法。仔细阅读该程序,理解其功能。

  上面的程序保存在文件StringManips.java中。将文件放置在本地路径下,编译并运行。仔细查看程序的输出结果,然后按照以下要求修改程序:
 1. 声明一个名为middle3的字符串类型变量,将声明变量放在程序的顶端,使用一个赋值语句和substring方法,将phrase字符串中间三个字符赋值给middle3变量(该字符串中间的字符,及其左右各一个字符)。添加一个打印语句,用于输出结果。保存,编译并运行该程序。
 2. 添加一个赋值语句,替代swithchedPhrase字符串中的所有空格为“*”。该结果应该返回给switchPhrase。
 3. 声明两个新的字符串类型变量city和state。添加语句,提示用户输入他们的籍贯所在的city和state,并使用Scanner类读入该信息。然后使用String类创建并打印一个新字符串,包含state名(全部大写),city名(全部小写),state名(全部大写)。如下例所示:
NORTH CAROLINAlilesvilleNORTH CAROLINA

import java.util.Scanner;

public class StringManips {
    public static void main(String[] args) {
        String middle3;
        Scanner scan=new Scanner(System.in);
        String phrase = new String ("This is a String test.");
        int phraseLength; 
        int niddleIndex;
        String firstHalf;
        String secondHalf;
        String switchedPhrase; 
        String city;
        String state;
        System.out.println("请输入籍贯所在的city");
        city=scan.nextLine();
        System.out.println("请输入籍贯所在的state");
        state=scan.nextLine();
        String Origin;
        state = state.toUpperCase();
        city = city.toLowerCase();
        Origin =  state.concat(city).concat(state);
        phraseLength = phrase.length() ;
        niddleIndex = phraseLength/2 ;
        firstHalf = phrase.substring (0,niddleIndex) ;
        secondHalf = phrase.substring (niddleIndex,phraseLength) ;
        switchedPhrase = secondHalf.concat (firstHalf) ;
        middle3=phrase.substring ((niddleIndex-1),(niddleIndex+2));
        String switchPhrase=switchedPhrase.replaceAll(" ", "*");
        System.out.println ( "Original phrase: " + phrase);
        System.out.println ("Length of the phrase: " + phraseLength + " characters" ) ;
        System.out.println ( "Index of the niddle: " + niddleIndex) ;
        System.out.println ( "Character at the riddle index: " + phrase.charAt (niddleIndex) ) ;
        System.out.println ( " Switched phrase: " +switchedPhrase) ;
        System.out.println("phrase中间的三个字符为"+middle3);
        System.out.println("替代swithchedPhrase字符串中的所有空格为“*”  "+switchPhrase);
        System.out.println("所在籍贯为"+Origin);
        scan.close();
    }
}

END