用JAVA编程!从键盘输入一个字母,输出这个字母的加密结果,加密操作:将字母变成倒序的字母,如把a变成z。
2个回答
展开全部
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ChangeLetter {
public static void main(String[] args) {
String outputStr = "";
System.out.print("请输入字母:");
//从键盘输入字母
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
//读取行,赋值给inputStr
String inputStr = br.readLine();
for(int i=0;i<inputStr.length();i++){
//将转换后的字符连接成字符串输出
outputStr = outputStr + changeLetter(inputStr.charAt(i));
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(outputStr);
}
public static char changeLetter(char c){
//用于后台打印,知道Unicode码
/*Integer num1 = 'A';
Integer num2 = 'Z';
Integer num3 = 'a';
Integer num4 = 'z';
System.out.println(num1);
System.out.println(num2);
System.out.println(num3);
System.out.println(num4);*/
//unicode码
//A值为65
//Z值为90
//a值为97
//b值为122
int letter = c;
char character = 0;
if(c>=65&&c<=90){
//比如输入B,则运算步骤如下:
//90-(66-65)=89,89转换成字符则为Y
//以下小写的同理
character = (char)(90-(letter-65));
}
if(c>=97&&c<=122){
character = (char)(122-(letter-97));
}
return character;
}
}
运行结果:
请输入字母:ABCXYZabcxyz
ZYXCBAzyxcba
import java.io.IOException;
import java.io.InputStreamReader;
public class ChangeLetter {
public static void main(String[] args) {
String outputStr = "";
System.out.print("请输入字母:");
//从键盘输入字母
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
//读取行,赋值给inputStr
String inputStr = br.readLine();
for(int i=0;i<inputStr.length();i++){
//将转换后的字符连接成字符串输出
outputStr = outputStr + changeLetter(inputStr.charAt(i));
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(outputStr);
}
public static char changeLetter(char c){
//用于后台打印,知道Unicode码
/*Integer num1 = 'A';
Integer num2 = 'Z';
Integer num3 = 'a';
Integer num4 = 'z';
System.out.println(num1);
System.out.println(num2);
System.out.println(num3);
System.out.println(num4);*/
//unicode码
//A值为65
//Z值为90
//a值为97
//b值为122
int letter = c;
char character = 0;
if(c>=65&&c<=90){
//比如输入B,则运算步骤如下:
//90-(66-65)=89,89转换成字符则为Y
//以下小写的同理
character = (char)(90-(letter-65));
}
if(c>=97&&c<=122){
character = (char)(122-(letter-97));
}
return character;
}
}
运行结果:
请输入字母:ABCXYZabcxyz
ZYXCBAzyxcba
展开全部
public class Test {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String str=in.next();
char ch=str.charAt(0);
if (str.length()>1||!((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))) {
System.out.println("你输入的不是一个字母!");
} else {
if (ch>='a'&&ch<='z') {
char converChar=(char) (219 - ch);
System.out.println(converChar);
}
if (ch>='A'&&ch<='Z') {
char converChar=(char) (155 - ch);
System.out.println(converChar);
}
}
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String str=in.next();
char ch=str.charAt(0);
if (str.length()>1||!((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))) {
System.out.println("你输入的不是一个字母!");
} else {
if (ch>='a'&&ch<='z') {
char converChar=(char) (219 - ch);
System.out.println(converChar);
}
if (ch>='A'&&ch<='Z') {
char converChar=(char) (155 - ch);
System.out.println(converChar);
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询