java 中if条件的用法请求详细解说

下面是作业 ,请求咨询
2025-01-04 18:43:58
推荐回答(3个)
回答1:

class Swap{
    private int a;
    private int b;
    public static void main(String[] argv){
        Swap swp = new Swap(8,3);
         System.out.println(swp.toString());
    }
    public Swap(int a, int b){
        this.a=a;
        this.b=b;
    }
    private void confirm(){
        int tmp;
        if(this.a>=this.b){
            tmp = this.a;
            this.a=this.b;
            this.b=tmp;
            System.out.println("a b swaped.");
        }else{
            System.out.println("a b not swaped.");
        }
    }
    private String toString(){
        confirm();
        return "a="+this.a+" b="+this.b;
    }
}

回答2:

public class compare{ //定义类
static int a,b;
public static int function(int a,int b){

int c;
if(a>=b)

c=a;
this.a=b;
this.b=c;
}
public static void main(String[] args){
compare.function(x,y);
System.out.println("a:"+compare.a+"b:"+compare.b);
}

回答3:

int temp=0;
if(a>=b){
temp=a;
a=b;
b=temp;
}
System.out.println("a="+a+"---------b="+b);