Java嵌套循环。试图重复内部循环
我正在练习2d数组,偶然发现如何重复数组的内部循环以接受另一组输入。我被困在如何在不牺牲循环变量重新初始化的情况下重复输入上,这样它就不会重置数组的索引指针 以下是主要课程:Java嵌套循环。试图重复内部循环,java,multidimensional-array,nested-loops,Java,Multidimensional Array,Nested Loops,我正在练习2d数组,偶然发现如何重复数组的内部循环以接受另一组输入。我被困在如何在不牺牲循环变量重新初始化的情况下重复输入上,这样它就不会重置数组的索引指针 以下是主要课程: public class Games { public static void main(String args[]) { GamesClass data = new GamesClass(); List output[][] = data.createlist();
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;
}
}
}
将循环中的条件从
i
到i<(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
如果您还想在第二个游戏中显示Gamer 1和Gamer 2,请打印(i-n*ngamers+1)
,而不仅仅是(i+1)
对打印功能也做同样的更改我不明白这个问题,你能解释一下你想做什么吗?好的,我来实现它,看看它是如何进行的。我会不断更新的!
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