java第四次上机

Question

编写一个程序,实现下列功能

  1. 测试两个字符串String str1 = "It is" 和String str2= "It is"是否相等。

  2. 将“a book ” 和其中的字符串 str1相连接

  3. 用m替换新字符串中的i

## Code


public class testString {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String str1 = "It is";
        String str2 = "It is";
        String str3 = " a book";
        System.out.println(com(str1, str2));
        str1 += str3;
        System.out.println(str1);
        System.out.println(str1.replace("i", "m"));
    }
    public static int com(String str1, String str2) {
        int flag = 0;
        if (str1.equals(str2)) {
            flag = 1;
        }
        return flag;
    }

}

Question

创建一个HashMap对象,添加一些学生的姓名和成绩:张三:90分,李四:83分。接着从HashMap中获取他们的姓名和成绩,然后把李四的成绩改成100分,再次输出他们的信息

Code


import java.util.HashMap;
import java.util.*;
public class test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        HashMap map = new HashMap();
        map.put("张三",90);
        map.put("李四",95);
        System.out.println(map);
        map.put("张三", 100);
        System.out.println(map);
    }

}

Question

编写成熟,将long类型的数据转化为字符串,将十进制的365转化为十六进制数表示的字符串

Code



public class test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        long num = 987654;
        String num1 =String.valueOf(num);
        System.out.println(num1);
        int x = 365;
        System.out.printf("%x",x);
    }

}

Question

设计一个程序,基于泛型Map实现10个英文单词的汉语翻译,即通过单词得到它的中文含义


import java.util.*;
import java.util.HashMap;
public class test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        HashMap map = new HashMap();
        map.put("hello", "你好");
        map.put("hello1", "你好1");
        map.put("hello2", "你好2");
        map.put("hello3", "你好3");
        map.put("hello4", "你好4");
        map.put("hello5", "你好5");
        map.put("hello6", "你好6");
        map.put("hello7", "你好7");
        map.put("hello8", "你好8");
        map.put("hello9", "你好9");
        String name;
        Scanner sc = new Scanner(System.in);
        name = sc.nextLine();
        System.out.println(map.get(name));
    }

}

猜你喜欢

转载自blog.csdn.net/yjd19970908/article/details/80318504