简体   繁体   中英

Java nested loop. Trying to reiterate the inside loop

I'm practicing 2d array and stumbled upon on how to reiterate the inside loop of array to accept another set of inputs. I got stuck on how to reiterate the inputs without sacrificing the reinitialization of the loop variable so that it will not reset the index pointer of the array.

Here's the main class:

public class Games {
    public static void main(String args[]) {
        GamesClass data = new GamesClass();
        List output[][] = data.createlist();
        data.print(output);
    }
}

class List {
    private String Gamers, Status;
    private int Score;
    
    public List()
    {
        Gamers = "";
        Score = 0;
        Status = "";
    }
    
    public List(String name, int score, String status)
    {
        Gamers = name;
        Score = score;
        Status = status;
    }
    
    public void setGamers(String name)
    {
        Gamers = name;
    }
    public String getGamers()
    {
        return Gamers;
    }
    
    public void setScores(int score)
    {
        Score = score;
    }
    public int getScores()
    {
        return Score;
    }
    
    public void setStatus(String status)
    {
        Status = status;
    }
    public String getStatus()
    {
        return Status;
    }
}

And here's the invoking class:

import java.util.*;
class GamesClass {
    private int ngames,ngamers;
    private String[] games;
    
    public void nloops()
    {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter no. of games: ");
        ngames = in.nextInt();
        System.out.print("Enter number of gamers each game: ");
        ngamers = in.nextInt();
        games = new String[ngames];
    }
    
    public String[] inputgames()
    {
        Scanner in = new Scanner(System.in);
        for(int i=0 ; i<ngames ; ++i)
        {
            System.out.print("Enter Game: ");
            games[i] = in.next();
        }
        return games;
    }
    
    public List[][] createlist()
    {
        nloops();
        inputgames();
        List[][] list = new List[ngames*ngamers][3];
        Scanner in = new Scanner(System.in);
        int score, n, i=0;
        for(n=0 ; n<ngames ; ++n)
        {
            System.out.println("\nGame#" + (n+1) + " " + games[n]);
            for(; i<list.length/ngames ; ++i)
            {
                list[i][0] = new List();
                System.out.print("Gamer " + (i+1) + ": ");
                list[i][0].setGamers(in.next());
                System.out.print("Score: ");
                score = in.nextInt();
                list[i][0].setScores(score);
                if(score>=1 && score<=50) list[i][0].setStatus("Noob");
                else if(score>=51 && score<=100) list[i][0].setStatus("Savage");
                else if(score>=101 && score<=150) list[i][0].setStatus("Expert");
                else if(score>=151 && score<=200) list[i][0].setStatus("Master");
                else if(score>=201 && score<=250) list[i][0].setStatus("Veteran");
                else if(score>=251 && score<=300) list[i][0].setStatus("Legendary");
            }
            i+=ngamers;
        }
        return list;
    }
    
    public void print(List[][] value)
    {
        int n, i=0;
        for(n=0 ; n<ngames ; ++n)
        {
            System.out.println("\nGame#" + (n+1) + " " + games[n]);
            System.out.println("\nGamer\t\tScore\t\tStatus");
            for( ;i<value.length/ngames ; ++i)
            {
                System.out.println((i+1) + " " + value[i][0].getGamers() + "\t\t" + value[i][0].getScores() + "\t\t" + value[i][0].getStatus());
            }
            i+=ngamers;
        }
    }
}

The output keeps stucked after inputting the first set of inputs.

Enter no. of games: 2
Enter number of gamers each game: 2
Enter Game: VALORANT
Enter Game: LOL

Game#1 VALORANT
Gamer 1: Jrk
Score: 100
Gamer 2: Dash
Score: 200

Game#2 LOL

Game#1 VALORANT

Gamer       Score       Status
1 Jrk       100     Savage
2 Dash      200     Master

Game#2 LOL

Gamer       Score       Status

Change the condition in the loop from i < list.length/ngames to i < (list.length/ngames)*(n+1) and also remove this line at the end of the loop : i+=ngamers; The variable is being incremented in the loop itself, there's no need to change it again.

However, this will produce the output

Game#1 VALORANT
Gamer 1: Jrk
Score: 100
Gamer 2: Dash
Score: 200

Game#2 LOL
Gamer 3 : name
Score : 300
Gamer 4 : name2
Score : 400

If you want to display Gamer 1 and Gamer 2 for the second game as well, print (i - n*ngamers + 1) instead of just (i+1)

Make these same changes to the print function as well

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM