日期:2014-05-20  浏览次数:20571 次

传值不成功
Java code

import java.util.*;
public class Beltandmethod {
    
    private static final int old = 20;//年纪
    
    private String name ;//姓名
    
    private int height ;//身高
    private int weight ; //体重]
    
    Scanner sc = new Scanner (System.in);
    
    public void input (String name,int height,int weight){
        this.name=name;
        this.height=height;
        this.weight=weight;
        System.out.println("请输入姓名:");
        name=sc.next();
        System.out.println("请输入身高:");
        height=sc.nextInt();
        System.out.println("请输入体重:");
        weight=sc.nextInt();
    }
    
    public void show (){
        System.out.println("姓名是:"+name+" 身高是:"+height+" 体重是:"+weight+" 年龄是:"+old);
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Beltandmethod center =new Beltandmethod ();
        String name = "";
        int height= 0 ;
        int weight = 0 ;
        center.input(name, height, weight);
        center.show();

    }

}


参数传递不成功,求高手。。

------解决方案--------------------
Java code

    public void input (String name,int height,int weight){
        this.name=name;
        this.height=height;
        this.weight=weight;
        System.out.println("请输入姓名:");
        this.name=sc.next();
        System.out.println("请输入身高:");
        this.height=sc.nextInt();
        System.out.println("请输入体重:");
        this.weight=sc.nextInt();
    }

------解决方案--------------------
Java code
public void input (String name,int height,int weight){
        System.out.println("请输入姓名:");
        name=sc.next();
        System.out.println("请输入身高:");
        height=sc.nextInt();
        System.out.println("请输入体重:");
        weight=sc.nextInt();

 //先把值读进来,在赋值嘛
        this.name=name;
        this.height=height;
        this.weight=weight;

    }

------解决方案--------------------
探讨

你没加this
就是相当于把传进来的值给修改了,没有修改对象的值~~

------解决方案--------------------
探讨

你没加this
就是相当于把传进来的值给修改了,没有修改对象的值~~