你狠你来!

cyclone_8000 2007-08-13 10:10:42
int a0=0,a1=1,……a9=9;
写一程序
来控制变量使其完成
ai=i(i=0,1,2……9)
其中两个i均为变量,主要卡在a1是一个变量,现在要用变量i去替换1
...全文
828 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
goosman 2008-04-06
  • 打赏
  • 举报
回复
nt a0=0,a1=1,……a9=9;
写一程序
来控制变量使其完成
ai=i(i=0,1,2……9)
其中两个i均为变量,主要卡在a1是一个变量,现在要用变量i去替换1

int b[]=new int[10];
for(int i=0;i<=b.length;i++)
{System.out.println("a"+b[i]+"="+i);}
justicesun 2007-08-14
  • 打赏
  • 举报
回复
想了半天,没搞出来...呵呵

楼主啊...搞出来告诉我啊...我去吃喜酒
piaopiao11 2007-08-14
  • 打赏
  • 举报
回复
javassist 可以动态创建属性方法
piaopiao11 2007-08-14
  • 打赏
  • 举报
回复
package test;

import javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtField;


public class testmain {

public static void main(String[] args) throws CannotCompileException, InstantiationException, IllegalAccessException, IllegalArgumentException, SecurityException, NoSuchFieldException {
String classname="test.AA";
CtClass ctClass=null;
try{
ctClass=ClassPool.getDefault().get(classname);
}catch (Exception e) {
e.printStackTrace();
// ctClass=ClassPool.getDefault().makeClass(classname);
}
for(int i=0;i<10;i++){
ctClass.addField(CtField.make("public int a"+i+"="+i+";",ctClass));
}
Object o=ctClass.toClass().newInstance();
for(int i=0;i<10;i++){
System.out.println(o.getClass().getField("a"+i).getInt(o));
}
}
}

class AA{

}
cchaha 2007-08-14
  • 打赏
  • 举报
回复
题目看了, 不明白这个有什么实际应用,动态生成变量名有什么优点?
cyberpeng 2007-08-14
  • 打赏
  • 举报
回复
如果ai(i=0..9)只是a0..a9的话,那就像david2083() 那样都定义出来,而后用反射。
否则就只有借助第三方了。
hoperun 2007-08-14
  • 打赏
  • 举报
回复
private static void test13(int n) {
String[] arrayS = new String[n + 1];
int[] arrayI = new int[n + 1];
for (int i = 0; i <= n; i++) {
arrayS[i] = "a" + i;
arrayI[i] = i;
System.out.println(arrayS[i] + "=" + arrayI[i]);
}

}
mysqlaping 2007-08-14
  • 打赏
  • 举报
回复
这要是php,就相当容易了。
java处理这样的事情是上确实很笨拙。
daniel_kaka 2007-08-14
  • 打赏
  • 举报
回复
上面代码中
interpreter.set("i", i);
多余,去掉~
qianzongli 2007-08-14
  • 打赏
  • 举报
回复
up
daniel_kaka 2007-08-14
  • 打赏
  • 举报
回复
有一个java的插件叫做bsh,你可以在网上下载到bsh-2.0b4.jar版本~
这个插件可以满足你的需求:

package test;

import bsh.EvalError;
import bsh.Interpreter;

public class OtherTest {
public static void main(String[] args) {
try{
Interpreter interpreter = new Interpreter();
for (int i = 0; i< 10; i ++){
interpreter.set("a" + i, i);
interpreter.set("i", i);
}
String []_val = interpreter.getNameSpace().getVariableNames();
for (String a : _val){
if (a.startsWith("a")){
System.out.println(a + "=" + interpreter.get(a));
}
}
}catch(EvalError ee){
ee.printStackTrace();
}
}
}

输出结果:
a3=3
a2=2
a1=1
a0=0
a9=9
a8=8
a7=7
a6=6
a5=5
a4=4
zdjray 2007-08-14
  • 打赏
  • 举报
回复
我不狠我来了
mebio 2007-08-14
  • 打赏
  • 举报
回复
进来之前我觉得我挺狠的
进来之后我发现我也就是那么回事了。
yandebin2006 2007-08-14
  • 打赏
  • 举报
回复
这个标题强悍!!
lalakid 2007-08-14
  • 打赏
  • 举报
回复
动态声明变量????如果可以的话,那就可以动态生成所有的东西了
晨星 2007-08-14
  • 打赏
  • 举报
回复
这有点像宏的功能,可惜Java不支持宏。
如果是类的成员变量,那么可以用反射实现。可惜反射也属于超语言的特性,在这点上跟宏又没啥区别了。
liangquan05170139 2007-08-14
  • 打赏
  • 举报
回复
andycpp(幻瞳) ( ) 信誉:100 2007-8-13 23:03:59 得分: 0



数组是干啥用的?数组就是为了满足变量名相同而下标不同的一群变量,也就是楼主的需求
可是楼主和数组貌似有仇,宁死不用数组,不得不说,还是楼主狠啊


你也狠呀
  • 打赏
  • 举报
回复
换个思路,使用 Map 来替代。

Map<String, Integer> map = new TreeMap<String, Integer>();
for (int i = 1; i < 10; i++) {
  map.put("a" + i, i);
}

for (int i = 1; i < 10; i++) {
  System.out.printf("a%d = %d%n", i, map.get("a" + i));
}
Dan1980 2007-08-14
  • 打赏
  • 举报
回复
如果是动态创建属性(实例变量),还有那么一点点意义;如果是局部变量,那就完全没有意义了。
还是那句话:为什么不用数组?
internet2006yn 2007-08-13
  • 打赏
  • 举报
回复
我觉得楼主的意思是他想用循环生成变量名a1-a10,
数组可以实现同名不同下标,但是难以实现a1,a2,a3这样的名字,因为这里a后面的数字并不是下标,而是一个字符!
我也曾经试图这样搞过,不过到现在没搞 出来,不知道楼主是不是我这个意思!
加载更多回复(6)

62,614

社区成员

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

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