+ 5

Any better ways to shorten user inputs please?

// this is tedious i may need 20 of them 😋 Scanner scn = new Scanner(System.in); int input1 = scn.nextInt(); int input2 = scn.nextInt(); int input3 = scn.nextInt(); int input4 = scn.nextInt();

24th Oct 2017, 5:41 PM
D_Stark
D_Stark - avatar
4 odpowiedzi
+ 32
use enhanced for loop //for making it more small & use if inside it to do a particular task for specified if condition
24th Oct 2017, 6:35 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 12
Consider using array. int [ ] inputs = new int[20]; for(int i=0; i<inputs.length; i++){ inputs[i] = scan.nextInt(); }
24th Oct 2017, 5:48 PM
Shamima Yasmin
Shamima Yasmin - avatar
+ 12
You can access any element using index, but it's better to use loop. Your way is valid as well :) But loop will check all elements one by one. for(int i=0; i<20; i++){ if(inputs[i] == 42){ // do something } }
24th Oct 2017, 6:01 PM
Shamima Yasmin
Shamima Yasmin - avatar
+ 4
can i call a specific input with this? like if( input[2] == 42){System.out.print();}
24th Oct 2017, 5:53 PM
D_Stark
D_Stark - avatar