关于字符串 菜鸟求助

jingyinling 2008-10-06 11:14:50
我想写一个函数例如给定一个字符串
String name="123";如何在System.out.println();语句中能打出来name=123;
public static void StringName(String strname)
{

System.out.println(strname+"="+strname);
}

如果按照上面的就是123=123
要是写成
public static void StringName(String strname)
{

System.out.println("strname="+strname);
}
写出来的就是strname=123
怎样才能是name=123
...全文
166 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
ssqtjffcu 2008-10-06
  • 打赏
  • 举报
回复
把它弄成一个类不就行了,属性有name和value,
ZangXT 2008-10-06
  • 打赏
  • 举报
回复
把这个String引用作为其他类的field,然后通过反射就可以得到其名字了。
不过是没事找事。
justinavril 2008-10-06
  • 打赏
  • 举报
回复
It's your problem. If you write a String as parameter, is it possible for you don't know what it is?
niuniu20008 2008-10-06
  • 打赏
  • 举报
回复
貌似Java里把.name()称之为方法,而不是函数
jingyinling 2008-10-06
  • 打赏
  • 举报
回复
System.out.println("name="+strname);
这种方式虽然是成功的 但不是我想要的 我就是想提高通用性,这样的话 传来的变量不是 String name="123";而是String a="happy"; 一样能够成功的那种
ZangXT 2008-10-06
  • 打赏
  • 举报
回复
别找了,没有。
jingyinling 2008-10-06
  • 打赏
  • 举报
回复
System.out.println(strname.name()+"="+strname);

这个String类没有.name()函数啊 我就是想找找看 有没有什么可以获得String类的变量名字的函数 大家帮帮
胡须棉花糖 2008-10-06
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 octopus1 的回复:]

System.out.println("name="+strname);
[/Quote]
right
sword80 2008-10-06
  • 打赏
  • 举报
回复
System.out.println(strname.name()+"="+strname);
octopus1 2008-10-06
  • 打赏
  • 举报
回复

System.out.println("name="+strname);
ycyn521 2008-10-06
  • 打赏
  • 举报
回复

public static void StringName(String str,String strname)
{

System.out.println(strname + " = " + str);
}
ycyn521 2008-10-06
  • 打赏
  • 举报
回复

public static void StringName(String strname)
{

System.out.println(strname+"="+strname);
}

ycyn521 2008-10-06
  • 打赏
  • 举报
回复
貌似没有什么实际意义,如果非想写的话,可以在方法中设定2个参数
一个参数为字符串的变量名,另一个为变量名的字符串
呵呵
明白?
  • 打赏
  • 举报
回复
System.out.println("name="+strname);
这样就行了
chenpingtai2008 2008-10-06
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 justinavril 的回复:]
引用 14 楼 micool 的回复:
引用 8 楼 justinavril 的回复:
It's your problem. If you write a String as parameter, is it possible for you don't know what it is?
你有什么问题吗?我英语很好的

????
[/Quote]
你的英语确实很好啊,把15的搞闷了
15楼的意思是他自己定义的变量不可能不知道叫什么
貌似
ice_Dream 2008-10-06
  • 打赏
  • 举报
回复
顺便解释一下~上面所说的老子不是指 中国古代著名的思想家~~~指本人 HOHO~
ice_Dream 2008-10-06
  • 打赏
  • 举报
回复
貌似你做的东西 即使很通用也没啥实际意义~!


看贴回贴是好习惯——老子
justinavril 2008-10-06
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 micool 的回复:]
引用 8 楼 justinavril 的回复:
It's your problem. If you write a String as parameter, is it possible for you don't know what it is?
你有什么问题吗?我英语很好的
[/Quote]
????
micool 2008-10-06
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 justinavril 的回复:]
It's your problem. If you write a String as parameter, is it possible for you don't know what it is?
[/Quote] 你有什么问题吗?我英语很好的
shixitong 2008-10-06
  • 打赏
  • 举报
回复
[code=public class StringName
{
public String output()
{
String s=new String("Name");
return s;
}

}
public class Test
{

public static void main(String args[])
{
StringName s=new StringName();
System.out.println(s.output()+"="+"123");

}
}][/code]
加载更多回复(2)

62,623

社区成员

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

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