这个程序哪里错了??谢谢。

ttl_web 2004-08-05 07:10:40
using System;

namespace UCOM.HR{
public class P76A{
public static int Main(){
int[] MyArray=new int[4];
MyArray[0]=1;
MyArray[1]=2;
MyArray[2]=3;
MyArray[3]=4;
for(int i=0;i<4;i++){
Console.WriteLine("MyArray[{0}] is {1} ",i,MyArray[i]);
}
Console.WriteLine("MyArray's length is {2} .",MyArray.Length);
Console.WriteLine("MyArray first[]'s length is {3} .",MyArray.GetLength(0));
return 1;
}
}
}



*******************************

MyArray[0] is 1
MyArray[1] is 2
MyArray[2] is 3
MyArray[3] is 4

未处理的异常: System.FormatException: 索引(从零开始)必须大于或等于零,且小于参
数列表的大小。
at System.Text.StringBuilder.AppendFormat(IFormatProvider provider, String fo
rmat, Object[] args)
at System.String.Format(IFormatProvider provider, String format, Object[] arg
s)
at System.IO.TextWriter.WriteLine(String format, Object arg0)
at System.IO.SyncTextWriter.WriteLine(String format, Object arg0)
at UCOM.HR.P76A.Main() in f:\net\jtgj\P76A.cs:line 15
...全文
123 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
ttl_web 2004-08-06
  • 打赏
  • 举报
回复
感谢各位的精彩解答。

谢谢。
mobydick 2004-08-06
  • 打赏
  • 举报
回复
{0,8:c}
0表示第一个参数,在本例中就是billTotal
8表示输出宽度为8,如果billTotal = 100,输出就是_____100,注意:我在这里用"_"代替了空格。
c是格式化字符,有很多种,c表示本地化货币符号,如果用的是英文(美国)操作系统,承上例输出为____$100
ttl_web 2004-08-06
  • 打赏
  • 举报
回复
参照
MSDN2003 /
NET Framework 类库 /
Console.WriteLine 方法 (String, Object[]) [C#] /
示例 [Visual Basic, C#, JScript] 下面的代码示例说明 WriteLine 的用法:

public class TipCalculator {
private const double tipRate = 0.18;
public static int Main(string[] args) {
double billTotal;
if (args.Length == 0) {
Console.WriteLine("usage: TIPCALC total");
return 1;
}
else {
try {
billTotal = Double.Parse(args[0]);
}
catch(FormatException) {
Console.WriteLine("usage: TIPCALC total");
return 1;
}
double tip = billTotal * tipRate;
Console.WriteLine();
Console.WriteLine("Bill total:\t{0,8:c}", billTotal);
Console.WriteLine("Tip total/rate:\t{0,8:c} ({1:p1})", tip, tipRate);
Console.WriteLine(("").PadRight(24, '-'));
Console.WriteLine("Grand total:\t{0,8:c}", billTotal + tip);
return 0;
}
}
}

这里的
Console.WriteLine("Bill total:\t{0,8:c}", billTotal);
=================================*****
Console.WriteLine("Grand total:\t{0,8:c}", billTotal + tip);
=================================*******
具体何解?谢谢!!

efeng4209 2004-08-05
  • 打赏
  • 举报
回复
把你的程序修改如下:
using System;

namespace UCOM.HR{
public class P76A{
public static int Main(){
int[] MyArray=new int[4];
MyArray[0]=1;
MyArray[1]=2;
MyArray[2]=3;
MyArray[3]=4;
for(int i=0;i<4;i++){
Console.WriteLine("MyArray[{0}] is {1} ",i,MyArray[i]);
}
Console.WriteLine("MyArray's length is {0} .",MyArray.Length);
Console.WriteLine("MyArray first[]'s length is {0} .",MyArray.GetLength(0));
return 1;
}
}
}
SHAUKI 2004-08-05
  • 打赏
  • 举报
回复
问题出在这两句上:
Console.WriteLine("MyArray's length is {2} .",MyArray.Length);
Console.WriteLine("MyArray first[]'s length is {3} .",MyArray.GetLength(0));
改成如下即可:
Console.WriteLine("MyArray's length is {0} .",MyArray.Length);
Console.WriteLine("MyArray first[]'s length is {0} .",MyArray.GetLength(0));
Eddie005 2004-08-05
  • 打赏
  • 举报
回复
using System;

namespace UCOM.HR{
public class P76A{
public static int Main(){
int[] MyArray=new int[4];
MyArray[0]=1;
MyArray[1]=2;
MyArray[2]=3;
MyArray[3]=4;
for(int i=0;i<4;i++){
Console.WriteLine("MyArray[{0}] is {1} ",i,MyArray[i]);
}
Console.WriteLine("MyArray's length is {0} .",MyArray.Length);
Console.WriteLine("MyArray first[]'s length is {0} .",MyArray.GetLength(0));
return 1;
}
}
}

110,499

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧