0

newbie here.

This is the code

for (i = 0; i < 5; i++) {
    System.out.println("Enter Name of the event " + (i + 1) + " that the teams are entering");
    Tevent[i] = scan.next();
} // event names
            
for (int a = 0; a < 1; a++) {
    System.out.println("Enter rank of the team on the event"); // event ranks
    teamRank[i] = scan.nextInt();
    if (teamRank[i] > 3) {
        System.out.println("This team will not be awarded points");
    }
    if (teamRank[i] == 3) {
        System.out.println("5 points is granted for this event");
        teamScore[i] = teamScore[i] + 5;
    }
    if (teamRank[i] == 2) {
        System.out.println("10 points is granted for this event");
        teamScore[i] = teamScore[i] + 10;
    }
    if (teamRank[i] == 1) {
        System.out.println("20 points is granted for this event");
        teamScore[i] = teamScore[i] + 20;
    }

The code is supposed to ask for event name and then ask for the rank.

According to the rank, it is supposed to add points to the team on each event and then print out the summarized value.

Example:

  • if I enter rank 1 5 times, instead of showing 100 it shows 20

I tried using multiple loops, it didn't work.

I feel like I am totally wrong on the code. Maybe there's another way of doing this? Can someone show me the way please :D thanks

Nowhere Man
  • 19,170
  • 9
  • 17
  • 42
Bonzowo
  • 3
  • 2
  • Could it because of "for (int a = 0; a < 1; a++)" as this only runs once ? – Sandeep Gupta Jul 17 '21 at 07:14
  • The code should be formatted properly - this makes easier to see if the logic is wrong. Here the loops are NOT nested, and the loop variable `i` is somehow used in the second loop, which contains ONLY one iteration. – Nowhere Man Jul 17 '21 at 07:16
  • Also, the code is not complete, there's no _list_ mentioned in the code, do you mean `teamRank` array?` – Nowhere Man Jul 17 '21 at 07:24

1 Answers1

0

An implementation with the fixes:

  • use constant values to define the size of arrays and the number of ranking attempts
  • replaced duplicated code in if with switch statement calculating rank
  • fixed data input to handle famous Scanner issue
final int teamSize = 2;
String[] Tevent = new String[teamSize];
int[] teamScore = new int[teamSize];
int[] teamRank  = new int[teamSize];

final int rankCount = 3;

Scanner scan = new Scanner(System.in);

for (int i = 0; i < Tevent.length; i++) {
    System.out.println("Enter Name of the event " + (i + 1) + " that the teams are entering");
    Tevent[i] = scan.nextLine();
    
    for (int a = 0; a < rankCount; a++) {
        System.out.println("Enter rank of the team on the event");
        teamRank[i] = scan.nextInt();
        int rank = 0;
        switch (teamRank[i]) {
            case 3: rank =  5; break;
            case 2: rank = 10; break;
            case 1: rank = 20; break;
        }
        if (rank == 0) {
            System.out.println("This team will not be awarded points");
        } else {
            teamScore[i] += rank;
            System.out.println(rank + " points is granted for this event");
        }
    }
    if (scan.hasNextLine()) {
        scan.nextLine();
    }
    System.out.println("----");
}
System.out.println("Team ranks:  " + Arrays.toString(teamRank));
System.out.println("Team scores: " + Arrays.toString(teamScore));

Example run:

Enter Name of the event 1 that the teams are entering
event1
Enter rank of the team on the event
2 10 points is granted for this event
Enter rank of the team on the event
2 10 points is granted for this event
Enter rank of the team on the event
2
10 points is granted for this event
----
Enter Name of the event 2 that the teams are entering
event 2
Enter rank of the team on the event
1
20 points is granted for this event
Enter rank of the team on the event
3
5 points is granted for this event
Enter rank of the team on the event
3
5 points is granted for this event
----
Team ranks:  [2, 3]
Team scores: [30, 30]
Nowhere Man
  • 19,170
  • 9
  • 17
  • 42