簡體   English   中英

Java 嵌套循環。 試圖重申內部循環

[英]Java nested loop. Trying to reiterate the inside loop

我正在練習二維數組並偶然發現如何重申數組的內部循環以接受另一組輸入。 我被困在如何在不犧牲循環變量的重新初始化的情況下重申輸入,以便它不會重置數組的索引指針。

這是主類:

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;
    }
}

這是調用類:

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;
        }
    }
}

輸入第一組輸入后,輸出保持卡住。

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

將循環中的條件從i < list.length/ngamesi < (list.length/ngames)*(n+1)並在循環結束時刪除此行: i+=ngamers; 變量在循環本身中遞增,無需再次更改它。

但是,這將產生輸出

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

如果您還想為第二個游戲顯示玩家 1 和玩家 2,請打印(i - n*ngamers + 1)而不是(i+1)

也對打印功能進行這些相同的更改

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM