有关字符串显示格式的问题帮回答下 谢谢

nbe01 2009-11-12 12:41:20

package P78.EX16;

class MyRectangle
{
private int Length,Width;
void setLength(int Length)
{
this.Length=Length;
}
void setWidth(int Width)
{
this.Width=Width;

}
int getWidth()
{
return Width;
}
int getLength()
{
return Length;
}
int getArea()
{
return Length*Width;
}
String tostring()
{
int Length=new int(Length);
int Width=new int(Width);
String s=Length.tostring()+","+Width.tostring();
return s;
//这里要求显示矩形的格式,我有点不太会编辑,帮我下。
}


}
帮我下 这个字符串该怎么转换啊???
...全文
79 6 打赏 收藏 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
bayougeng 2009-11-12
  • 打赏
  • 举报
回复
int Length=new int(Length);
int Width=new int(Width);
这两句都不要,直接:
String s=Length +","+Width ;
bayougeng 2009-11-12
  • 打赏
  • 举报
回复
String s=Length +","+Width ;
zhuzeitou 2009-11-12
  • 打赏
  • 举报
回复
int是基本类型,不是封装类,封装类是Integer……而且变量名重复了

试试看
    String tostring()
{
String s = String.format("%d,%d", Length, Width);
return s;
}
nbe01 2009-11-12
  • 打赏
  • 举报
回复
int Length=new int(Length);
int Width=new int(Width);
new int这里提示出错
justinavril 2009-11-12
  • 打赏
  • 举报
回复
用4#的办法就能正确打印出矩形的长和宽。或者这样也行:

System.out.println("Length: "+ this.Length +"\n"+ "Width: "+ this.Width);

[Quote=引用楼主 nbe01 的回复:]
Java codepackage P78.EX16;class MyRectangle
{privateint Length,Width;void setLength(int Length)
{this.Length=Length;
}void setWidth(int Width)
{this.Width=Width;

}int getWidth()
{return Width;
}int getLength()
{return Length;
}int getArea()
{return Length*Width;
}
String tostring()
{int Length=newint(Length);int Width=newint(Width);
String s=Length.tostring()+","+Width.tostring();return s;//这里要求显示矩形的格式,我有点不太会编辑,帮我下。 }


}
帮我下 这个字符串该怎么转换啊???
[/Quote]

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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