我想首先说,如果这是常识,请原谅我并有耐心.我对
Java有些新意.
我正在尝试编写一个程序,它将在一种缓冲区中存储许多变量值.我想知道是否有办法让程序“创建”自己的变量,并将它们分配给值.
以下是我要避免的示例:
package test;
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
int inputCacheNumber = 0;
//Text File:
String userInputCache1 = null;
String userInputCache2 = null;
String userInputCache3 = null;
String userInputCache4 = null;
//Program
while (true) {
Scanner scan = new Scanner(System.in);
System.out.println("User Input: ");
String userInput;
userInput = scan.nextLine();
// This would be in the text file
if (inputCacheNumber == 0) {
userInputCache1 = userInput;
inputCacheNumber++;
System.out.println(userInputCache1);
} else if (inputCacheNumber == 1) {
userInputCache2 = userInput;
inputCacheNumber++;
} else if (inputCacheNumber == 2) {
userInputCache3 = userInput;
inputCacheNumber++;
} else if (inputCacheNumber == 3) {
userInputCache4 = userInput;
inputCacheNumber++;
}
// And so on
}
}
}
所以只是为了总结一下,我想知道程序是否有办法将无限数量的用户输入值设置为String值.我想知道是否有一种方法可以避免预定义它可能需要的所有变量.
感谢您的阅读,以及您的耐心和帮助!
〜莱恩