NumbersWar.java
This program is basically a war between even and odd integers. The more even numbers than odd numbers, the even numbers will win.
It takes an array of numbers and checks whether the number is even or odd.
By determining this, it adds on to a counter to track how many even and odd integers there are. So there are two counters since there’s one for even and one for odd.
It returns the difference between the two counters. So basically, if even wins, you take the number of even numbers and subtract by odd. You do the opposite if odd wins.
The code has been modified slightly and now uploaded to the site.
Normal Code Snippet
/*
Author: ENTPRESTIGIOUS
Date Finished: April 15, 2021
Compiler: JDK 11.0.10 LTS
Link: https://edabit.com/challenge/7fHsizQrTLXsPWMyH
*/
import java.util.Scanner;
public class NumbersWar {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("How many numbers would you like to submit?:");
int amount = input.nextInt();
int arrayNum = 0;
int[] arr1 = new int[amount];
System.out.println("Please enter " + amount + " numbers below:");
for (int i = 0; i < amount; i++){
arrayNum = input.nextInt();
arr1[i] = arrayNum;
}
int result = warOfNumbers(arr1);
System.out.println("The larger group is: " + result + " times bigger!");
}
public static int warOfNumbers(int[] numbers){
int even = 0;
int total = 0;
int odd = 0;
for (int i = 0; i < numbers.length; i++){
if (numbers[i] % 2 == 0){
even += numbers[i];
}
if (numbers[i] % 2 != 0){
odd += numbers[i];
}
}
if (even > odd){
total = even - odd;
}
if (odd > even){
total = odd - even;
}
return total;
}
}
Below is the janky JOptionPane Java GUI version.
JOptionPane Code Snippet
/*
Author: ENTPRESTIGIOUS
Date Finished: April 15, 2021
Compiler: JDK 11.0.10 LTS
Link: https://edabit.com/challenge/7fHsizQrTLXsPWMyH
*/
import javax.swing.JOptionPane;
public class NumbersWarGUI {
public static void main(String[] args){
String input = JOptionPane.showInputDialog(null, "How many numbers would you like to submit?:");
int amount = Integer.parseInt(input);
int arrayNum = 0;
int[] arr1 = new int[amount];
for (int i = 0; i < amount; i++){
String value = JOptionPane.showInputDialog(null, "Please enter " + amount + " numbers below");
arrayNum = Integer.parseInt(value);
arr1[i] = arrayNum;
}
int result = warOfNumbers(arr1);
JOptionPane.showMessageDialog(null, "The larger group is: " + result + " times bigger!");
}
public static int warOfNumbers(int[] numbers){
int even = 0;
int total = 0;
int odd = 0;
for (int i = 0; i < numbers.length; i++){
if (numbers[i] % 2 == 0){
even += numbers[i];
}
if (numbers[i] % 2 != 0){
odd += numbers[i];
}
}
if (even > odd){
total = even - odd;
}
if (odd > even){
total = odd - even;
}
return total;
}
}