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;
    }
​
}
​
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);
    }
​
}
​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);
    }
​
}
​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));
    }
​
}
​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));
    }
​
}
​

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值