您好我必须将我的项目从java转换为C#,我需要帮助以下行将它们转换为c#。请看下面我在这里输入的图像和代码。
for (String profile : userProfiles)
{
int maxuser = 100;
if (profile.equals("astd"))
maxuser = 300;
for (String suffix : suffixes)
{
for (int i = 0; i <= maxuser; i++)
{
String prefix = profile;
System.out.println("prefix" + prefix);
String num = Integer.toString(i);
if (num.length() < 2)
num = "0" + num;
String postfix = num;
String username = prefix + postfix + suffix;
System.out.println(username);
//TODO add a
Gson gson = new Gson();
User u = new User();
u.setFirstName(username
答案 0 :(得分:2)
不仅是foreach;在给定的代码中有很多不同,我将在这里指定一些:
for(:)
.ToString()
代替Integer.toString
这是更正后的代码
foreach (string profile in userProfiles)
{
int maxuser = 100;
if (profile.Equals("astd"))
maxuser = 300;
foreach (string suffix in suffixes)
{
for (int i = 0; i <= maxuser; i++)
{
string prefix = profile;
Console.WriteLine("prefix" + prefix);
string num = i.ToString();
if (num.Length < 2)
num = "0" + num;
string postfix = num;
string username = prefix + postfix + suffix;
Console.WriteLine(username);
//TODO add a
Gson gson = new Gson();
User u = new User();
u.setFirstName(username);
}
}
}
答案 1 :(得分:1)
<强>等同于:强>
爪哇
for (String profile : userProfiles)
C#
foreach (string profile in userprofiles)
爪哇
System.out.println("text");
C#
Console.WriteLine("text");
爪哇
Integer.toString(i);
C#
i.ToString();
爪哇
num.length()
C#
num.Length
答案 2 :(得分:0)
来自:for (String profile : userProfiles)
:foreach(String profile in userProfiles)
由此:
if (profile.equals("astd"))
maxuser = 300;
对此:
if(profile == "astd")
maxuser=300;
来自:System.out.println("prefix" + prefix);
对此:Console.WriteLine("prefix" + prefix); //assuming you're doing a console app
最后,您的num.length()
应为num.length
答案 3 :(得分:0)