将字符串arrgument传递给java中的方法时出错

时间:2017-07-04 14:20:20

标签: java string parameters

    class Smartphone
    {
        private float screensize;
        private float androidversion;
        private int ram;
        private int memory;
        private int processor;
        private String brand;
//I also created String brand variable...at the rite position
    public void setData(String brand,int screensize,int ram,int memory,int processor,float androidversion)
    {
        this.screensize=screensize;
        this.androidversion=androidversion;
        this.ram=ram;
        this.memory=memory;
        this.processor=processor;
        this.brand=brand;
    }
    public void dispData()
    {
        System.out.println("the brand of the mobile is "+brand+"Smartphones");
        System.out.println("the screensize of the mobile is "+screensize+"inches");
        System.out.println("the ram of the mobile is "+ram);
        System.out.println("the memory of the mobile is "+memory+"GB");
        System.out.println("the processor of the mobile is "+processor+"Ghz");
        System.out.println("the androidversion of the mobile is "+androidversion);
    }
    }
    class App
    {
        public static void main(String args[])
        {
            Smartphone xiaomi=new Smartphone();
//Error is causing here...the xiaomi string is not passing..
            xiaomi.setData("Xiaomi",5.5,3,32,2.6,8.0);
        }
    }

1 个答案:

答案 0 :(得分:1)

您已通过无效参数。您可以修复发送参数,例如:

xiaomi.setData("Xiaomi",5.5f,3,32,2,8.0f);

或将您的功能签名更改为

String brand,float screensize,int ram,int memory,double processor,float androidversion

并将变量更改为 私人浮动屏幕;

private float androidversion;
private int ram;
private int memory;
private double processor;
private String brand;