亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

盡管在函數內更改了輸入,但函數返回輸入不變

盡管在函數內更改了輸入,但函數返回輸入不變

動漫人物 2022-07-20 20:23:35
就以下代碼而言,我遇到了一個問題,即一切都運行良好,但我沒有得到所需的輸出。代碼應該接受用戶輸入并打印它,但所有字母的大小寫都顛倒了。然而,即使在將輸入返回到 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;    }}
查看完整描述

2 回答

?
白板的微信

TA貢獻1883條經驗 獲得超3個贊

toggleStringCase(input);

我想你可能想得到那個函數的輸出。您似乎假設輸入將被更改 - 事實并非如此。請參閱Java 是“按引用傳遞”還是“按值傳遞”?


查看完整回答
反對 回復 2022-07-20
?
慕田峪4524236

TA貢獻1875條經驗 獲得超5個贊

Java中的參數是按值傳遞的。您不能以這種方式更改傳遞給方法的值。但是,您正在返回您想要的值 - 只需將其分配回您的變量:

input = toggleStringCase(input);


查看完整回答
反對 回復 2022-07-20
  • 2 回答
  • 0 關注
  • 75 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號