在Java中重用字符串

Col*_*ter 0 java user-input

我一直想知道是否有任何方法可以重用字符串,如:

import java.io.Console;

public class input {
    public static void main (String[] args) {
        Console console = System.console();
        String input = console.readLine("Type username: "); //user input
        // name code
        // clear string
        String input = console.readLine("Hello, what would you like to do: ");  
        // other code
    }
}
Run Code Online (Sandbox Code Playgroud)

通过清除字符串//clear string,所以如果你有一个循环的用户输入,如果一个东西没有出现,它不会中断.

Nic*_*udo 5

您不能多次声明同一个变量.但是,您可以根据需要随时重新分配,前提是您尚未将其声明为最终.