这是我的一道课后练习题,要求大小写字母互相转换并计数转换的个数,我写完了,小写能换成大写,大写却转换不成小写,希望大家不要嫌这个问题菜不予回答。
class Convert{
public static void main(String args[])
throws java.io.IOException{
char ch;
int count=0;
do{
do{
ch=(char)System.in.read();
}while(ch == '\n'|ch == '\r');
if (ch!='.'){
if(ch<'A'&ch>='a') {
ch+=32;
count+=1;
}
if(ch>='A'&ch<'Z'){
ch-=32;
count+=1;
}
System.out.println("The convert char is"+ch);
}
}while(ch!='.');
System.out.println("The numbers of convert is:"+count);
}
}
class Convert{
public static void main(String args[])
throws java.io.IOException{
char ch;
int count=0;
do{
do{
ch=(char)System.in.read();
}while(ch == '\n'|ch == '\r');
if (ch!='.'){
if(ch<'A'&ch>='a') {
ch+=32;
count+=1;
}
if(ch>='A'&ch<'Z'){
ch-=32;
count+=1;
}
System.out.println("The convert char is"+ch);
}
}while(ch!='.');
System.out.println("The numbers of convert is:"+count);
}
}
public static void main(String args[]) throws java.io.IOException {
char ch;
int count = 0;
do {
System.out.print("Please input a letter(input . to end the program): ");
do {
ch = (char) System.in.read();
} while (ch == '\n' || ch == '\r');
if (ch != '.') {
if (ch >= 'a' && ch <= 'z') {
ch -= 32;
} else if (ch >= 'A' && ch <= 'Z') {
ch += 32;
} else {
System.out.println("It's not a letter.");
continue;
}
count++;
System.out.println("The convert char is: " + ch);
}
} while (ch != '.');
System.out.println("The numbers of convert is:" + count);
}
}
public static void main(String args[])
throws java.io.IOException{
char ch;
int count=0;
do{
do{
ch=(char)System.in.read();
}while(ch == '\n'|ch == '\r');
if (ch!='.'){
if((ch<='Z')&&(ch>='A')) {
ch+=32;
count+=1;
}
else if((ch<='z')&&(ch>='a')){
ch-=32;
count+=1;
}
System.out.println("The convert char is: "+ch);
}
}while(ch!='.');
System.out.println("The numbers of convert is: "+count);
}
}在你的第二个if的前面要加上else,否则经过第一个if处理后,ch又满足第二个if的条件了。