需要帮助使用 for 循环递增 1。每次只加1
Need help to increment by 1 using a for loop. Only increment by 1 each time
所以我有一个计分方法,它是 运行 在我们玩完之后,如果用户想重玩游戏的话。我在其中显示分数。
比如
第一次尝试:
Game 1 : He scored 20
然后用户决定重玩游戏,得到不同的分数。然后我要它显示。
第二次尝试:
Game 1 : He scored 20
Game 2 : He scored 10
第三次尝试:等等
Game 1 : He Scored 20
Game 2 : He scored 10
Game 3 : He scored 5
我试过在 foreach 中使用 for 循环,然后将 i 放入另一个 int
public void HighScore()
{
int gameList = 1;
foreach (var item in Points)
{
for (int i = 1; i < Points.Count; i++)
{
gameList = i++;
}
Console.WriteLine($"{name} : Game {gameList} Score : {item} : Level [{GameLevel}]");
}
}
//Points是一个Int List
//我要更改游戏编号Game 1, 2, 3, 4,.
// use like this
gameList++;
// instead of doing this
gameList = i++;
我希望你现在能解决。
不需要 for
循环:
public void HighScore()
{
int gameList = 1;
foreach (var item in Points)
{
Console.WriteLine($"{name} : Game {gameList} Score : {item} : Level [{GameLevel}]");
gameList++;
}
}
首先,您不需要像上面那样定期存储每个游戏的 loop.Create a class 访问点。
public class Score
{
public Score(int point ,int game)
{
Point = point;
Game = game;
}
public int Point { get; set; }
public int Game { get; set; }
}
然后创建一个全局变量
List<Score> scores = new List<Score>();
所以你可以保存你的分数和 gameCount。
int point = 10;//any number
int lastGame = (scores.Any()) ? scores.Last().Game : 0;//this is about first game if no score its first game
scores.Add(new Score(point, lastGame += 1));
你可以用循环向用户展示。
foreach(var score in scores){
Console.WriteLine($"Game {score.Game} Point: {score.Point}");
}
Game Score
1 10
2 20
试试下面的方法。
使用gameList++
代替gameList = i++;
Unary Increment Operator
public static void HighScore()
{
int gameList = 1;
List<int> Points = new List<int>() { 10,20,39,40,50};
foreach (var item in Points)
{
Console.WriteLine($"Game {gameList} Score : {item}");
gameList++;
}
}
所以我有一个计分方法,它是 运行 在我们玩完之后,如果用户想重玩游戏的话。我在其中显示分数。
比如
第一次尝试:
Game 1 : He scored 20
然后用户决定重玩游戏,得到不同的分数。然后我要它显示。
第二次尝试:
Game 1 : He scored 20
Game 2 : He scored 10
第三次尝试:等等
Game 1 : He Scored 20
Game 2 : He scored 10
Game 3 : He scored 5
我试过在 foreach 中使用 for 循环,然后将 i 放入另一个 int
public void HighScore()
{
int gameList = 1;
foreach (var item in Points)
{
for (int i = 1; i < Points.Count; i++)
{
gameList = i++;
}
Console.WriteLine($"{name} : Game {gameList} Score : {item} : Level [{GameLevel}]");
}
}
//Points是一个Int List
//我要更改游戏编号Game 1, 2, 3, 4,.
// use like this
gameList++;
// instead of doing this
gameList = i++;
我希望你现在能解决。
不需要 for
循环:
public void HighScore()
{
int gameList = 1;
foreach (var item in Points)
{
Console.WriteLine($"{name} : Game {gameList} Score : {item} : Level [{GameLevel}]");
gameList++;
}
}
首先,您不需要像上面那样定期存储每个游戏的 loop.Create a class 访问点。
public class Score
{
public Score(int point ,int game)
{
Point = point;
Game = game;
}
public int Point { get; set; }
public int Game { get; set; }
}
然后创建一个全局变量
List<Score> scores = new List<Score>();
所以你可以保存你的分数和 gameCount。
int point = 10;//any number
int lastGame = (scores.Any()) ? scores.Last().Game : 0;//this is about first game if no score its first game
scores.Add(new Score(point, lastGame += 1));
你可以用循环向用户展示。
foreach(var score in scores){
Console.WriteLine($"Game {score.Game} Point: {score.Point}");
}
Game Score
1 10
2 20
试试下面的方法。
使用gameList++
代替gameList = i++;
Unary Increment Operator
public static void HighScore()
{
int gameList = 1;
List<int> Points = new List<int>() { 10,20,39,40,50};
foreach (var item in Points)
{
Console.WriteLine($"Game {gameList} Score : {item}");
gameList++;
}
}