0

I am trying to print output Win or Lose based on input but getting error. Please below the question for my code -

A new fighting game has become popular. There is n number of villains in it, each having some strength. There are n players in the game with each having some energy. The energy is used to kill the villains. A villain can be killed only if the energy of the player is greater than the strength of the villain.

Maxi is playing the game and at a particular time wants to know if it is possible for him to win the game or not with the given set of energies and strengths of players and villains. Maxi wins the game if his players can kill all the villains with the allotted energy.

Input Format The first line of input consists of a number of test cases, T. The first line of each test case consists of a number of villains and player, N. The second line of each test case consists of the N space-separated by strengths of Villains. The third line of each test case consists of N space-separated by the energy of players.

Error message

Exception in thread "main" java.util.NoSuchElementException
at java.base/java.util.Scanner.throwFor(Scanner.java:937)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
at Main.main(Main.java:8)

My Code

import java.util.*;
import java.lang.*;
import java.io.*;
        
public class Main {
    public static void main(String args[]) {
        Scanner input = new Scanner(System.in);
        int numberOfTestcases = input.nextInt();
        for (int k = 0; k < numberOfTestcases; k++) {
            int number = input.nextInt();
            System.out.println(number + "");
        
            // Scanner input = new Scanner(System.in);
            int[] players_energy = new int[number];
            int[] villain_strength = new int[number];
        
            for (int i = 0; i < villain_strength.length; i++) {
                System.out.println("Please enter villain strengths ");
                villain_strength[i] = input.nextInt();
            }
        
            for (int i = 0; i < players_energy.length; i++) {
                System.out.println("Please enter player energies ");
                players_energy[i] = input.nextInt();
            }
        
            for (int i = 0; i < players_energy.length; i++) {
                System.out.println("player energies are " + players_energy[i] + "");
            }
            for (int i = 0; i < villain_strength.length; i++) {
                System.out.println("villain strengths are " + villain_strength[i] + "");
            }
            Arrays.sort(players_energy);
            Arrays.sort(villain_strength);
        
            for (int i = 0; i < players_energy.length; i++) {
                System.out.println("player energies after sorting are " + players_energy[i] + "");
            }
            for (int i = 0; i < villain_strength.length; i++) {
                System.out.println("villain strengths after sorting are " + villain_strength[i] + "");
            }
            boolean result = true;
            for (int i = 0; i < number; i++) {
                if (players_energy[i] > villain_strength[i]) {
                    result = true;
                } else {
                    result = false;
                }
            }
            if (result) {
                System.out.println("WIN");
            } else {
                System.out.println("LOSE");
            }
        }
    }    
}

AP11
  • 598
  • 3
  • 10
Siddhesh
  • 71
  • 6
  • 1
    I mean, the code runs without any error, when I run it in Intellij. Also parameters inside `main` method should be `String[] args` - you are declaring it in `C` language. – AP11 Jan 29 '21 at 16:07
  • It worked for me too, and I think your ```"Please enter villain strengths"``` things only needed to be printed once since all the input of villain/player are on the same line. – lier wu Jan 29 '21 at 16:10
  • @AP11 but don't have to, https://stackoverflow.com/questions/5997235/is-there-a-difference-between-mainstring-args-and-mainstring-args – lier wu Jan 29 '21 at 16:11
  • @lierwu I know, but still, if you code in `Java`, you should use Java syntax. – AP11 Jan 29 '21 at 16:12
  • 1
    Does this answer your question? [Scanner error with nextInt()](https://stackoverflow.com/questions/12832006/scanner-error-with-nextint) – Wortig Jan 29 '21 at 16:12
  • You already have answers here: https://stackoverflow.com/questions/12832006/scanner-error-with-nextint and also check this: https://stackoverflow.com/questions/26446599/how-to-use-java-util-scanner-to-correctly-read-user-input-from-system-in-and-act – Wortig Jan 29 '21 at 16:12
  • @AP11 but you wrote it like you have to... But ok :) – lier wu Jan 29 '21 at 16:13
  • 1
    @lierwu should is strong recommendation, not command – AP11 Jan 29 '21 at 16:14
  • Exceptions are thrown at run time, your problem does not occur while compiling. – Mark Rotteveel Jan 30 '21 at 11:11

1 Answers1

1

In my IDE, the code runs, but you have implemented wrong win or lose decision, because right now you base the output only on the last pair of player and villain, so if all the players lose and the last one wins, your result is win, which I think is wrong.

If you base it only on every player beating every villain, then if one loses, you lose the whole game:

for (int i = 0; i < number; i++) {
    result = result && players_energy[i] > villain_strength[i];
}

If it is energy of all players vs. strength of all villains, then compare those sums:

int energySum = 0;
int strengthSum = 0;
for (int i = 0; i < number; i++) {
    energySum += players_energy[i];
    strengthSum += villain_strength[i];
}
boolean result = energySum > strengthSum;
AP11
  • 598
  • 3
  • 10