0

我想访问我的数组中的某个值,该值被分配用于运行我的代码的特定部分。例如,如果我的数组由数字 1 到 6 组成,其中每个数字都分配给通过键盘输入的变量,该变量运行我的代码的一部分,我将如何实现它?到目前为止,我只需要...

public class yTerminal {
public static void main(String[] args)
{               
    screen.println("Press key to access function: ");
    screen.println("1 - Open \n2 - Close \n3 - Help \n" 
    + "4 - Internet \n5 - Call \n6 "
    + "- Go");

    int[] numberInput = new int[5];
    int i;
    for (i=1; i < 7; i++)
        numberInput[i] = keyboard.readInt("Enter key corresponding to function: ");     

}

}

4

2 回答 2

0

我相信这将是一个更好的实现,而不是使用数组......

Scanner sc = new Scanner(System.in);
int choice;
while(true){
     System.out.print("Enter key to access functions:");
     choice = sc.nextInt();
     switch(choice) {
     case(1):
          do function:open;
          break;
     case(2):
          do function:close;
          break;
     case(3):
          do function:help;
          break;
     case(4):
          do function:internet;
          break:
     case(5):
          do function:call;
          break;
     case(6):
          do function:go;
          break;
     default:
          System.out.println("Invalid Input");
          break;
     }
}
于 2013-11-04T17:09:18.000 回答
0

几个概念 -

1)读取用户输入。您可以使用Scanner通过键盘获取用户输入。

for eg.
    Scanner reader = new Scanner(System.in);
    System.out.println("Enter key corresponding to function: ");

2)根据用户输入调用相应的代码。开关盒效果最好。

switch(reader.nextInt()) {
    case '<whatever_number>': 
    // your piece of code. 
    break;
}
于 2013-11-04T17:11:49.560 回答