【问题标题】:Referencing 3 variables using a for loop and i使用 for 循环和 i 引用 3 个变量
【发布时间】:2014-11-08 18:33:57
【问题描述】:

是否可以有三个预定义的变量:

int randomNum1 = 5;
int randomNum2 = 4;
int randomNum3 = 3;

然后使用 for 循环调用这些变量:

for(int i = 1; i < 4; i++){
switch(randomNumi){
...
}

每次 for 循环运行时它在哪里调用每个 randomNum 变量?

【问题讨论】:

  • 你希望如何“调用”一个变量?你的问题真的没有任何意义。那么,您如何回溯并解释问题所在?
  • 您的意思是您希望randomNumi 成为randomNum1randomNum2randomNum3?使用array
  • @fge 我希望 switch 语句首先调用randomNum1,然后是randomNum2,然后是randomNum3

标签: java variables for-loop switch-statement


【解决方案1】:

尝试定义数组:

int randomNum[] = {5, 4, 3};

for(int i = 0; i < randomNum.length; i++){
    switch(randomNum[i]){
       case:......
    }
}

【讨论】:

  • 啊,谢谢。我们的课程还没有涉及数组,我试图想出一个具有相同概念但实际上并没有使用它们的解决方案。
  • 不,你有三个不同的变量。而且你不能像 switch(randomNumi) 一样,否则编译器不会对此感到满意,并会说 randonNum1 是未定义的。
猜你喜欢
  • 2012-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多