日期:2014-05-20  浏览次数:20689 次

超菜鸟的问题。
这是我的一道课后练习题,要求大小写字母互相转换并计数转换的个数,我写完了,小写能换成大写,大写却转换不成小写,希望大家不要嫌这个问题菜不予回答。
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);
}
}

------解决方案--------------------
你的程序有几个问题,比如把“&&”写成了“&”,把“||”写成了“|”,if语句的判断范围也写错了,等等。程序修改如下:

class Convert {
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);
}
}