就以下代碼而言,我遇到了一個(gè)問(wèn)題,即一切都運(yùn)行良好,但我沒(méi)有得到所需的輸出。代碼應(yīng)該接受用戶輸入并打印它,但所有字母的大小寫(xiě)都顛倒了。然而,即使在將輸入返回到 toggleStringCase 后,toggleCase 正常工作,它也會(huì)恢復(fù)到它被發(fā)送到 toggleCase 之前的狀態(tài)。我無(wú)法理解為什么會(huì)發(fā)生這種情況。有人可以指出我正確的方向。理想情況下,我不希望你告訴我答案,而只是幫助我以正確的方式解決這個(gè)問(wèn)題。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; }}
盡管在函數(shù)內(nèi)更改了輸入,但函數(shù)返回輸入不變
動(dòng)漫人物
2022-07-20 20:23:35