java变量的创建,如何让程序用Java创建自己的变量?

我想首先说,如果这是常识,请原谅我并有耐心.我对

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值.我想知道是否有一种方法可以避免预定义它可能需要的所有变量.

感谢您的阅读,以及您的耐心和帮助!

〜莱恩

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值