How do you reintroduce the keyboard and reduce the Java code?
-
Method
numCheckEquals
accepts 4 whole numbers with a,b,c,d. In the cycle for me, I'm going to the method.numCheckEquals
4 digits from the console, and I write every time.sc.nextInt();
What if the program requires 100 digits, imagine what a huge code is gonna be like, how to reduce it.sc.nextInt();
public class Main { public static void main(String[] args) {
Scanner sc = new Scanner(System.in); for (int i = 0; i <= 4; i++) { System.out.println(Solution.checkNumEquals(sc.nextInt(), sc.nextInt(), sc.nextInt(), sc.nextInt())); }
}
♪
-
Accept values in the endless cycle. ♪ ♪
Scanner console = new Scanner(System.in); while (true) { int nextInt = console.nextInt();
System.out.println(nextInt); if (nextInt == -1) // условие выхода break;
Also available
Varargs
- these are the variable length arguments that allow us to create methods with an arbitrary number of arguments.public class Solution {
static void varArgs(int... nums) {
for (int num : nums) {
System.out.print(num);
}
System.out.println();
}public static void main(String[] args) { varArgs(1,2,3,4,5,6,7,8,9); varArgs(1,2,3,4); varArgs(5,6,7,8,9); }
}
Or read the method hasNext
https://stackoverflow.com/questions/34355062/how-to-use-hasnext-from-the-scanner-class