C# 如何在每个字符串后面加逗号,而不是在前面
我的类有一个问题,它在显示字符串之前显示一个逗号,我找不到一种方法将逗号从前面去掉并保留在所有其他地方C# 如何在每个字符串后面加逗号,而不是在前面,c#,C#,我的类有一个问题,它在显示字符串之前显示一个逗号,我找不到一种方法将逗号从前面去掉并保留在所有其他地方 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Carnival { class Player {
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Carnival
{
class Player
{
public string Name { get; set; }
public double SpendingMoney { get; set; }
public string PrizesWon { get; set; }
//constructors
public Player( string n, double sp)
{
Name = n;
SpendingMoney = sp;
PrizesWon = "";
}
//methods
public string Play(GameBooth aGames)
{
string newPrize;
if (SpendingMoney >= aGames.Cost)
{
newPrize = aGames.Start();
//here is what I need to fix
PrizesWon = "," + newPrize + PrizesWon ;
return newPrize;
}
else
{
return "error no money fool";
}
}
}
}
如果需要,这里是主代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Carnival
{
class Program
{
static void Main(string[] args)
{
//local variables
string name;
double money;
int choice = 0;
string newPrize;
//Game objects.
GameBooth balloonDartToss = new GameBooth(2, "tiger plush", "sticker");
GameBooth ringToss = new GameBooth(2, "bear keychain", "pencil");
GameBooth breakAPlate = new GameBooth(1.5, "pig plush", "plastic dinosaur");
Console.ForegroundColor = ConsoleColor.Cyan;
//asking player name
Console.Write("Welcome to the Carnival. What is your name? ");
name = Console.ReadLine();
//asking how much spending money the player has
Console.Write("How much money do you have? ");
money = Convert.ToDouble(Console.ReadLine());
Console.ResetColor();
//Creating player object.
Player Gambler = new Player(name, money);
//main program loop
while (choice != 4)
{
//print menu. Check out the local method below
printMenu();
//retrieve user choice. See the local method below
choice = getChoice();
//main switch. User Choices:
// 1 - we play Baloon Darts Toss and show prize
// 2 - we play ring Toss and show prize
// 3 - we play Break a Plate and show prize
// 4 - Show users all his prizes
switch (choice)
{
case 1:
newPrize = Gambler.Play(balloonDartToss);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(newPrize);
break;
case 2:
newPrize = Gambler.Play(ringToss);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(newPrize);
break;
case 3:
newPrize = Gambler.Play(breakAPlate);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(newPrize);
break;
case 4:
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("{0}, here is your prize list: {1}", Gambler.Name, Gambler.PrizesWon);
Console.ResetColor();
break;
}//end switch
}//end while
//next line simply pauses the program until you hit Enter.
Console.ReadLine();
}//end main
//this method prints the main menu
public static void printMenu()
{
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine();
Console.WriteLine("Select the game you would like to play:");
Console.WriteLine("1. Balloon Dart Toss");
Console.WriteLine("2. Ring Toss");
Console.WriteLine("3. Break a Plate");
Console.WriteLine("4. Get Prizes and Leave Carnival");
Console.Write("Enter Your Choice: ");
Console.ResetColor();
}//end printMenu
//this methods accepts user input 1-4
public static int getChoice()
{
Console.ForegroundColor = ConsoleColor.Yellow;
int input = 0;
do
{
Console.ForegroundColor = ConsoleColor.Yellow;
input = Convert.ToInt32(Console.ReadLine());
if (input < 1 || input > 4)
{
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("Out of range. Input 1-4 only");
Console.Write("Enter your choice: ");
Console.ResetColor();
}
} while (input < 1 || input > 4);
Console.ResetColor();
return input;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Carnival
{
class GameBooth
{
//properties
public double Cost { get; set; }
public string FirstPrize { get; set; }
public string ConsolationPrize { get; set; }
public int w { get; set; }
public int l { get; set; }
//constructors
public GameBooth(double c, string fp, string cp)
{
Cost = c;
FirstPrize = fp;
ConsolationPrize = cp;
}
//methods
public string Start()
{
Random r = new Random();
w = 1;
l = 1;
for (int i = 1; i < 3; i++)
{
int randomChoice = r.Next(0, 400);
if ( randomChoice == 1)
{
w++;
}
}
if (w == 3)
{
return FirstPrize;
}
else
{
return ConsolationPrize;
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
名称空间狂欢节
{
班级计划
{
静态void Main(字符串[]参数)
{
//局部变量
字符串名;
双倍货币;
int-choice=0;
弦乐奖;
//游戏对象。
GameBooth BalloodDartToss=新GameBooth(2,“老虎毛绒”,“贴纸”);
GameBooth ringToss=新GameBooth(2,“熊钥匙链”、“铅笔”);
GameBooth breakAPlate=新GameBooth(1.5,“猪毛绒”,“塑料恐龙”);
Console.ForegroundColor=ConsoleColor.Cyan;
//询问玩家姓名
控制台。写下(“欢迎参加狂欢节。你叫什么名字?”);
name=Console.ReadLine();
//询问玩家有多少花销
控制台。写下(“你有多少钱?”);
money=Convert.ToDouble(Console.ReadLine());
Console.ResetColor();
//创建播放器对象。
玩家赌徒=新玩家(姓名、金钱);
//主程序循环
while(选项!=4)
{
//打印菜单。查看下面的本地方法
打印菜单();
//检索用户选择。请参阅下面的本地方法
choice=getChoice();
//主开关。用户选择:
//1-我们玩阳台飞镖投掷和展示奖品
//2-我们玩掷环和展示奖品
//3-我们玩破盘子和展示奖品
//4-向用户展示他的所有奖品
开关(选择)
{
案例1:
newPrize=赌徒。玩(掷气球);
Console.ForegroundColor=ConsoleColor.Yellow;
Console.WriteLine(newPrize);
打破
案例2:
newPrize=赌徒。玩(掷环);
Console.ForegroundColor=ConsoleColor.Yellow;
Console.WriteLine(newPrize);
打破
案例3:
newPrize=赌徒。游戏(breakAPlate);
Console.ForegroundColor=ConsoleColor.Yellow;
Console.WriteLine(newPrize);
打破
案例4:
Console.ForegroundColor=ConsoleColor.Yellow;
WriteLine(“{0},这是您的奖品列表:{1}”,Gambler.Name,Gambler.PrizesWon);
Console.ResetColor();
打破
}//终端开关
}//结束时
//下一行只是暂停程序,直到您按Enter键。
Console.ReadLine();
}//端干管
//此方法打印主菜单
公共静态无效打印菜单()
{
Console.ForegroundColor=ConsoleColor.Cyan;
Console.WriteLine();
Console.WriteLine(“选择您想玩的游戏:”);
控制台。写线(“1。气球掷镖”);
控制台。写线(“2。环抛”);
控制台。写线(“3。打破一个盘子”);
Console.WriteLine(“4.获得奖品并离开狂欢节”);
控制台。写(“输入您的选择:”;
Console.ResetColor();
}//结束打印菜单
//此方法接受用户输入1-4
公共静态int getChoice()
{
Console.ForegroundColor=ConsoleColor.Yellow;
int输入=0;
做
{
Console.ForegroundColor=ConsoleColor.Yellow;
输入=Convert.ToInt32(Console.ReadLine());
如果(输入<1 | |输入>4)
{
Console.ForegroundColor=ConsoleColor.Cyan;
Console.WriteLine(“超出范围,仅输入1-4”);
控制台。写(“输入您的选择:”;
Console.ResetColor();
}
}而(输入<1 | |输入>4);
Console.ResetColor();
返回输入;
}
}
}
如果你也需要,这是我的另一门课,但你可能不需要
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Carnival
{
class Program
{
static void Main(string[] args)
{
//local variables
string name;
double money;
int choice = 0;
string newPrize;
//Game objects.
GameBooth balloonDartToss = new GameBooth(2, "tiger plush", "sticker");
GameBooth ringToss = new GameBooth(2, "bear keychain", "pencil");
GameBooth breakAPlate = new GameBooth(1.5, "pig plush", "plastic dinosaur");
Console.ForegroundColor = ConsoleColor.Cyan;
//asking player name
Console.Write("Welcome to the Carnival. What is your name? ");
name = Console.ReadLine();
//asking how much spending money the player has
Console.Write("How much money do you have? ");
money = Convert.ToDouble(Console.ReadLine());
Console.ResetColor();
//Creating player object.
Player Gambler = new Player(name, money);
//main program loop
while (choice != 4)
{
//print menu. Check out the local method below
printMenu();
//retrieve user choice. See the local method below
choice = getChoice();
//main switch. User Choices:
// 1 - we play Baloon Darts Toss and show prize
// 2 - we play ring Toss and show prize
// 3 - we play Break a Plate and show prize
// 4 - Show users all his prizes
switch (choice)
{
case 1:
newPrize = Gambler.Play(balloonDartToss);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(newPrize);
break;
case 2:
newPrize = Gambler.Play(ringToss);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(newPrize);
break;
case 3:
newPrize = Gambler.Play(breakAPlate);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(newPrize);
break;
case 4:
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("{0}, here is your prize list: {1}", Gambler.Name, Gambler.PrizesWon);
Console.ResetColor();
break;
}//end switch
}//end while
//next line simply pauses the program until you hit Enter.
Console.ReadLine();
}//end main
//this method prints the main menu
public static void printMenu()
{
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine();
Console.WriteLine("Select the game you would like to play:");
Console.WriteLine("1. Balloon Dart Toss");
Console.WriteLine("2. Ring Toss");
Console.WriteLine("3. Break a Plate");
Console.WriteLine("4. Get Prizes and Leave Carnival");
Console.Write("Enter Your Choice: ");
Console.ResetColor();
}//end printMenu
//this methods accepts user input 1-4
public static int getChoice()
{
Console.ForegroundColor = ConsoleColor.Yellow;
int input = 0;
do
{
Console.ForegroundColor = ConsoleColor.Yellow;
input = Convert.ToInt32(Console.ReadLine());
if (input < 1 || input > 4)
{
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("Out of range. Input 1-4 only");
Console.Write("Enter your choice: ");
Console.ResetColor();
}
} while (input < 1 || input > 4);
Console.ResetColor();
return input;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Carnival
{
class GameBooth
{
//properties
public double Cost { get; set; }
public string FirstPrize { get; set; }
public string ConsolationPrize { get; set; }
public int w { get; set; }
public int l { get; set; }
//constructors
public GameBooth(double c, string fp, string cp)
{
Cost = c;
FirstPrize = fp;
ConsolationPrize = cp;
}
//methods
public string Start()
{
Random r = new Random();
w = 1;
l = 1;
for (int i = 1; i < 3; i++)
{
int randomChoice = r.Next(0, 400);
if ( randomChoice == 1)
{
w++;
}
}
if (w == 3)
{
return FirstPrize;
}
else
{
return ConsolationPrize;
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
名称空间狂欢节
{
班级游戏亭
{
//性质
公共双成本{get;set;}
公共字符串一等奖{get;set;}
公共字符串ConsolationPrize{get;set;}
公共整数w{get;set;}
公共整数l{get;set;}
//建设者
公共游戏亭(双c、字符串fp、字符串cp)
{
成本=c;
一等奖=fp;
安慰奖=cp;
}
//方法
公共字符串开始()
{
随机r=新随机();
w=1;
l=1;
对于(int i=1;i<3;i++)
{
int randomChoice=r.Next(0400);
如果(随机选择==1)
{
w++;
}
}
如果(w==3)
{
返回一等奖;
}
其他的
{
回报慰问奖;
}
}
}
}
在您的代码中,您在newPrize
之前添加了(,)。要解决此问题,您必须将其放在PrizesWon之后