就以下代码而言,我遇到了一个问题,即一切都运行良好,但我没有得到所需的输出。
代码应该接受用户输入并打印它,但所有字母的大小写都颠倒了。然而,即使在将输入返回到 toggleStringCase 后,toggleCase 正常工作,它也会恢复到它被发送到 toggleCase 之前的状态。
我无法理解为什么会发生这种情况。
有人可以指出我正确的方向。
理想情况下,我不希望你告诉我答案,而只是帮助我以正确的方式解决这个问题。
package loopy;
import java.io.*;
public class loopy {
public static void main (String[] args) throws IOException {
// TODO: Use a loop to print every upper case letter
for (int i = 65; i < 91; i++) {
System.out.println((char)i);
}
// TODO: Get input from user. Print the same input back but with cases swapped. Use the helper functions below.
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String input = in.readLine();
in.close();
toggleStringCase(input);
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
out.write(input);
out.close();
}
//Takes a single Character and reverse the case if it is a letter
private static char toggleCase(char c) {
int asciiValue = (int) c;
if (asciiValue > 96 && asciiValue < 123){
asciiValue = asciiValue - 32;
}
else if (asciiValue > 64 && asciiValue < 91){
asciiValue = asciiValue + 32;
}
else {
}
c = (char) asciiValue;
return c;
}
// Splits a string into individual characters that are sent to toggleCase to have their case changed
private static String toggleStringCase(String str) {
String reversedCase = new String();
for (int i = 0; i < str.length(); i++) {
char letter = str.charAt(i);
toggleCase(letter);
reversedCase = reversedCase + letter;
}
str = reversedCase;
return str;
}
}
白板的微信
慕田峪4524236
相关分类