java菜鸟2个问题

来源:百度知道 编辑:UC知道 时间:2024/09/18 05:10:20
问题1:我想先定义一个普通的类,然后建立一个方法,方法里面包含for,for的参数在主函数中调用时赋值,但总说我for中的变量未定义,怎么办
public class Fortest{
public static void main(String args[]){
construction con=new construction();
con.show(0,10);
}
}
class construction{
int x;
int y;
construction(){
x=0;
y=0;
}
public void show(int a,int b){
x=a;
y=b;
for(x;x<y;x++){//这一行报错,x y未定义
System.out.println("Bingo!!!!");
}
}
}
问题2:java中可否可以像ActionScript那样,在变量名中有变量,可以又如何实现

(1)for(;x<y;x++){
(2)反射技术是有可能的,但不能是函数的局部变量,必须是类成员。以你的水平来说有难度,还是学些基础的吧。

1.
public class Fortest {
public static void main(String args[]) {
construction con = new construction();
con.show(0, 10);
}
}

class construction {
int x;
int y;

construction() {
x = 0;
y = 0;
}

public void show(int a, int b) {
x = a;
y = b;

for (; x < y; x++) {//
System.out.println("Bingo!!!!");
}
}
}

2.没明白啥意思

改过了,你自己看看,你要的是这个意思吧

public class Fortest{
public static void main(String args[]){
construction con=new construction();
con.show(0,10);
}
}
class construction{
int x;
int y;
construction(){
x=0;
y=0;
}

//这里改过了
public void show(int x,int y){
for(;x<y;x++){
System.out.println("Bingo!!!!