Hello, it became interesting, is it possible to use the values of variables in the program code? The most commonplace examples:
- Enter a string from the keyboard, for example,
+or any other character of the operation. It is necessary to carry out the operation, theзнакwhich was entered from the keyboard, and display the result on the screen. (Without usingswitch-caseand any other cycles) - We have several variables, say, their names are
foo_1,foo_2, ...,foo_10. The numbernis entered from the keyboard. It is necessary to display the value of the variable foo_n. Again withoutswitch-caseand similar methods.
Perhaps this is nonsense, but I have been interested in this question for a long time.
This is not a task, it is a personal interest. The question is how to use the values of variables, whether strings or numeric variables, in the program code, and not comparing them with something and then performing actions.
Explanation for commentators.Не нужно приводить примеры других циклов или способов сравнить значение введенной переменной и в зависимости от её значения выполнять что-то. Нужно именно встроить её значение в код. Например, чтобы использовать это для объявления переменных. Пусть, мне нужно объявить значение переменной, которая будет начинаться с foo_, а закончится введённым с клавиатуры значением и никак иначе.
For example, we enter several String variables from the keyboard: odin , test , foo . We fill them with some arr array. Also, we have the variables foo_odin , foo_dva , foo_tri , foo_test , foo_foo . Is there any way to implement access to variables as follows:
for (String n : arr) System.out.println("foo_" + n + " = " + foo_..n) Here by means of .. I have marked the complement of the name of a variable by means of another variable. Suppose if we have the variable foo_odin and the variable n with the value odin , then, if we turn to foo_..n , we will call foo_idin .