关于String的一个问题,用Java语言。
/*编写一个Java应用程序,实现如下功能:1)判断两个字符串是否相同,s1=”youareastudent”,s2=”howareyou”;2)判断字符串”220302...
/*编写一个Java应用程序,实现如下功能:
1) 判断两个字符串是否相同,s1=”you are a student”,s2=”how are you”;
2) 判断字符串”22030219851022024”的前缀、后缀是否和某个字符串”220302”相同;
3) 按字典顺序比较两个字符串”你”和”我”的大小关系;
4) 将数字型字符串”100”和”123.678”转换为数字;
5) 将字符串”FEDCBA”存放到数组中,调用for循环读出数组数据显示在屏幕上。
(本题20分) 展开
1) 判断两个字符串是否相同,s1=”you are a student”,s2=”how are you”;
2) 判断字符串”22030219851022024”的前缀、后缀是否和某个字符串”220302”相同;
3) 按字典顺序比较两个字符串”你”和”我”的大小关系;
4) 将数字型字符串”100”和”123.678”转换为数字;
5) 将字符串”FEDCBA”存放到数组中,调用for循环读出数组数据显示在屏幕上。
(本题20分) 展开
3个回答
展开全部
你好,代码如下,请参考:
public class TestString {
public static void main(String[] args) {
String s1 = "you are a student";
String s2 = "how are you";
System.out.println("s1和s2是否相等:" + s1.equals(s2)) ;
String a1 = "22030219851022024" ;
String sub = "220302" ;
if(a1.indexOf(sub) == 0 || a1.lastIndexOf(sub) == a1.length()-1){
System.out.println(a1 + "的前缀或后缀是由" + sub + "组成");
}
if("你".compareTo("我") > 0){
System.out.println("你大于我");
}else{
System.out.println("我大于你");
}
String n1 = "100" ;
String n2 = "123.678" ;
int num1 = Integer.parseInt(n1) ;
double num2 = Double.parseDouble(n2) ;
System.out.println(num1+num2);
String str = "FEDCBA" ;
char s[] = str.toCharArray() ;
for(int i=0;i<s.length;i++){
System.out.print(s[i] + " ");
}
}
}
public class TestString {
public static void main(String[] args) {
String s1 = "you are a student";
String s2 = "how are you";
System.out.println("s1和s2是否相等:" + s1.equals(s2)) ;
String a1 = "22030219851022024" ;
String sub = "220302" ;
if(a1.indexOf(sub) == 0 || a1.lastIndexOf(sub) == a1.length()-1){
System.out.println(a1 + "的前缀或后缀是由" + sub + "组成");
}
if("你".compareTo("我") > 0){
System.out.println("你大于我");
}else{
System.out.println("我大于你");
}
String n1 = "100" ;
String n2 = "123.678" ;
int num1 = Integer.parseInt(n1) ;
double num2 = Double.parseDouble(n2) ;
System.out.println(num1+num2);
String str = "FEDCBA" ;
char s[] = str.toCharArray() ;
for(int i=0;i<s.length;i++){
System.out.print(s[i] + " ");
}
}
}
展开全部
1) 判断两个字符串是否相同,s1=”you are a student”,s2=”how are you”;
if(s1.equals(s2))
2) 判断字符串”22030219851022024”的前缀、后缀是否和某个字符串”220302”相同;
if(”22030219851022024”.startWith(”220302”))
if(”22030219851022024”.afterWith(”220302”))
3) 按字典顺序比较两个字符串”你”和”我”的大小关系;
”你”.compareTo("我")
4) 将数字型字符串”100”和”123.678”转换为数字;
nteger.valueOf(”100”);
Float.valueOf(”123.678”);
5) 将字符串”FEDCBA”存放到数组中,调用for循环读出数组数据显示在屏幕上。
char [] arr =”FEDCBA”.toCharArray();
for(char x:arr){
System.out.println(x);
}
if(s1.equals(s2))
2) 判断字符串”22030219851022024”的前缀、后缀是否和某个字符串”220302”相同;
if(”22030219851022024”.startWith(”220302”))
if(”22030219851022024”.afterWith(”220302”))
3) 按字典顺序比较两个字符串”你”和”我”的大小关系;
”你”.compareTo("我")
4) 将数字型字符串”100”和”123.678”转换为数字;
nteger.valueOf(”100”);
Float.valueOf(”123.678”);
5) 将字符串”FEDCBA”存放到数组中,调用for循环读出数组数据显示在屏幕上。
char [] arr =”FEDCBA”.toCharArray();
for(char x:arr){
System.out.println(x);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
方法头就不写了
1). boolean isEqual = "you are a student".equals("how are you");
System.out.println("isEqual:" + isEqual);
2). String s1 = "220302"; String s2 = "22030219851022024";
boolean isPre = s1.regionMatches(0, s2, 0, 6);
boolean isLast = s1.regionMatches(0, s2, s2.length()-6, 6);
System.out.println("isPre:" + isPre);
System.out.println("isLast:" + isLast);
3). int result = "你".compareTo("我");
System.out.println("result:" + result);
4). int result1 = Integer.parseInt("100");
float result2 = Float.parseFloat("123.678");
System.out.println("result1:" + result1);
System.out.println("result2:" + result2);
5). char[] charArray = "FEDCBA".toCharArray() ;
for(int i = 0;i <charArray.length;i++){
System.out.println("char"+i+":" + charArray[i]);
}
希望对你有帮助,好好学习
1). boolean isEqual = "you are a student".equals("how are you");
System.out.println("isEqual:" + isEqual);
2). String s1 = "220302"; String s2 = "22030219851022024";
boolean isPre = s1.regionMatches(0, s2, 0, 6);
boolean isLast = s1.regionMatches(0, s2, s2.length()-6, 6);
System.out.println("isPre:" + isPre);
System.out.println("isLast:" + isLast);
3). int result = "你".compareTo("我");
System.out.println("result:" + result);
4). int result1 = Integer.parseInt("100");
float result2 = Float.parseFloat("123.678");
System.out.println("result1:" + result1);
System.out.println("result2:" + result2);
5). char[] charArray = "FEDCBA".toCharArray() ;
for(int i = 0;i <charArray.length;i++){
System.out.println("char"+i+":" + charArray[i]);
}
希望对你有帮助,好好学习
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询