我在练习2d数组,偶然发现了如何重复数组的内部循环以接受另一组输入。我被困在如何在不牺牲循环变量的重新初始化的情况下重复输入上,这样它就不会重置数组的索引指针。
主要课程如下:
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
1条答案
按热度按时间0dxa2lsx1#
将循环中的条件从
i < list.length/ngames
至i < (list.length/ngames)*(n+1)
同时移除循环末尾的这一行:i+=ngamers;
变量本身在循环中递增,无需再次更改。但是,这将产生输出
如果要在第二个游戏中同时显示gamer 1和gamer 2,请打印
(i - n*ngamers + 1)
而不仅仅是(i+1)
对打印功能也做同样的更改