验证中...
Your message has been delivered
change_letter.java
Raw Copy
package test;
import java.util.Scanner;
public class change_letter {
public static String Upper(String str){
String result = "";
for(int i = 0 ; i < str.length(); i++){
int begin = (int)str.charAt(i) - 32;
if(begin>=65 && begin<=90){
char end = (char)begin;
result = result + end;
}
else {
char end = (char)(begin+32);
result = result + end;
}
}
return result;
}
public static String Lower(String str){
String result = "";
for(int i = 0 ; i < str.length(); i++) {
int begin = (int) str.charAt(i) + 32;
if (begin >= 97 && begin <= 122) {
char end = (char) begin;
result = result + end;
}
else{
char end = (char) (begin-32);
result = result + end;
}
}
return result;
}
public static void main(String[] args) {
System.out.print("请输入字符串 : ");
Scanner sr = new Scanner(System.in);
String str = sr.next();
System.out.println("输入的字符串为:"+str);
System.out.println("字符串全变为大写为 :"+ Upper(str));
System.out.println("字符串全变为小写为 :"+Lower(str));
}
}

Comment list( 3 )

Admin 2017-11-15 13:45

NICE

余欢 2017-11-15 14:54

请问int begin = (int)str.charAt(i) - 32;里的 (int)str这个是强制转换成int类型的意思么

uestc_yhr 2017-11-16 19:42

对的,(int)str.charAt(i) 就是将str.charAt(i)强制转换成int型,即ASCII码,然后做运算。

You need to Sign in for post a comment