怎么解决?
如果a,b,c,d都是不为零的整数(小于等于9)比如a=1,b=2,能用程序把他定义成ab=12或ba=21,吗
如果要ab或ba变量的话还是需要自己定义的
如果是为了输出的话可以实现
import java.lang.reflect.Field; public class VarTest { private int a = 2; private int b = 3; //获取类中所有的成员变量 public Field[] getFields()throws Exception{ return VarTest.class.getDeclaredFields(); } //按成员变量出现的顺序输出 public String getOutputInOrder()throws Exception{ StringBuffer name = new StringBuffer(); StringBuffer value = new StringBuffer(); Field[] fields = getFields(); for(Field field:fields){ name.append(field.getName()); value.append(field.get(this)); } return "" + name + "=" + value; } //按成员变量出现的顺序的相反顺序输出 public String getOutputReverseOrder()throws Exception{ StringBuffer name = new StringBuffer(); StringBuffer value = new StringBuffer(); Field[] fields = getFields(); for(int i=fields.length-1; i>=0; i--){ name.append(fields[i].getName()); value.append(fields[i].get(this)); } return "" + name + "=" + value; } public static void main(String[] args) throws Exception{ VarTest test = new VarTest(); System.out.println(test.getOutputInOrder()); System.out.println(test.getOutputReverseOrder()); } }
public class GetNum { private int a,b,c,d; private int getAB(){ return a*10 + b; } private int getCD(){ return c*10 + d; } private int getDC(){ return d*10 + c; } public void getABCD(){ for(a=1; a<10; a++) for(b=1; b<10; b++) for(c=1; c<10; c++) for(d=1; d<10; d++){ if(getAB()+getCD() == getDC()) System.out.println(getAB() + "+" + getCD() + "=" + getDC()); } } public static void main(String[] args) { new GetNum().getABCD(); } }
public class GetNum { private int a,b,c,d; private int getXY(int x,int y){ return x*10 + y; } public void getABCD(){ for(a=1; a<10; a++) for(b=1; b<10; b++) for(c=1; c<10; c++) for(d=1; d<10; d++){ if(getXY(a,b)+getXY(c,d) == getXY(d,c)) System.out.println(getXY(a,b) + "+" + getXY(c,d) + "=" + getXY(d,c)); } } public static void main(String[] args) { new GetNum().getABCD(); } }