Java错误,找不到现场用户2303583

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19469904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 17:33:11  来源:igfitidea点击:

Java error, cannot find feild user2303583

java

提问by user2303583

Hi I am trying to get my code to create the new constructor objects which i reqiure to create mobile phone object, I have tried naming the constuctor fields to create object, however Error comes up at

嗨,我正在尝试让我的代码来创建我需要创建手机对象的新构造函数对象,我尝试命名构造函数字段以创建对象,但是错误出现在

public Mobile(String Mobilephonetype, String Mobilescreensize,
        String Mobilememorycardcapacity, String Mobilecameraresolution,
        String MobileGPS), at the bottom of the page

why am i getting this error?

为什么我收到这个错误?

java error, cannot find symbol - variable samsung

UPDATE i have changed some of the top fields from private int to string but now it says cannot find symbol - class string, what doers this mean??

更新我已经将一些顶级字段从私有 int 更改为字符串,但现在它说找不到符号 - 类字符串,这是什么意思?

Code:

代码:

/**
 * to write a simple java class Mobile that models a mobile phone.
 * 
 * @author () 
 * @version (14/10/13)
 */
public class Mobile

{
    // type of phone
    private string phonetype;
    // size of screen in inches
    private int screensize;
    // menory card capacity
    private int  memorycardcapacity;
    // name of present service provider
    private string serviceprovider;
    // type of contract with service provider
    private int typeofcontract;
    // camera resolution in megapixels
    private int cameraresolution;
    // the percentage of charge left on the phone
    private int checkcharge;
    // wether the phone has GPS or not
    private string GPS;
    // instance variables - replace the example below with your own
    private int x;

    // The constructor method

    public Mobile(String mobilePhoneType, int mobileScreenSize,
            int mobileMemoryCardCapacity, String newserviceprovider, int mobileCameraResolution,
            String mobileGPS) {
        this.phonetype =  mobilePhoneType;
        this.screensize = mobileScreenSize;
        this.memorycardcapacity = mobileMemoryCardCapacity;
        this.cameraresolution = mobileCameraResolution;
        this.GPS = mobileGPS;

        // you do not use this ones during instantiation,you can remove them if you do not need or assign them some  default values 
        this.serviceprovider = newserviceprovider;
        this.typeofcontract = 12;
        this.checkcharge = checkcharge;

    }

    // A method to display the state of the object to the screen
    public void displayMobileDetails() {
        System.out.println("phonetype: " + phonetype);
        System.out.println("screensize: " + screensize);
        System.out.println("memorycardcapacity: " + memorycardcapacity);
        System.out.println("cameraresolution: " + cameraresolution);
        System.out.println("GPS: " + GPS);
         System.out.println("serviceprovider: " + serviceprovider);
        System.out.println("typeofcontract: " + typeofcontract);

    }

    public Mobile(String MobilephoneType, int Mobilescreensize, int Mobilememorycardcapacity, String newserviceprovider, int Mobilecameraresolution,
            String MobileGPS) {
        this.phonetype = Mobilephonetype;
        this.screensize = 3;
        this.memorycardcapacity = 4;
        this.cameraresolution = 8;
        this.GPS = GPS;
        this.serviceprovider = newserviceprovider;
        this.typeofcontract = 12;
        this.checkcharge = checkcharge;


    }

}

 class mymobile {
    public static void main(String[] args) {
        Mobile Samsung = new Mobile("Samsung", "3", "4", "8",
                "GPS");
        Mobile Blackberry = new Mobile("Blackberry", "3.", "4",
                "8", "GPS");
        Samsung.displayMobileDetails();
        Blackberry.displayMobileDetails();
    }
}

回答by anubhava

This line seems to be problem in public Mobilemethod:

这一行在方法中似乎有问题public Mobile

this.phonetype = Samsung;

since you're defining Samsungobject inside main method in class mymobilewhich is not available in class Mobile

因为您Samsung在 main 方法中定义了对象,class mymobile而在该方法中不可用class Mobile

UPDATE:I sincerely advise you to start using a IDE like Eclipse so that these type of errors can be caught in the editor itself.

更新:我真诚地建议您开始使用像 Eclipse 这样的 IDE,以便可以在编辑器本身中捕获这些类型的错误。

Most like instead of this declaration:

最喜欢而不是这个声明:

// type of phone
private int phonetype;

You meant:

你的意思是:

// type of phone
private String phonetype;

and then use this assignment:

然后使用此分配:

this.phonetype = Mobilephonetype;

回答by Mureinik

There is no such variable Samsung. In this line:

没有这样的变量Samsung。在这一行:

 this.phonetype = Samsung;

It seems as though you may have meant to assign Mobilephonetypeto it somehow, but as the comment below suggest, it's an int.

似乎您可能打算以Mobilephonetype某种方式分配给它,但正如下面的评论所暗示的那样,它是一个int.

回答by APerson

The problem looks to be this.phonetype = Samsung;, try changing it to this.phonetype = Mobilephonetype.

问题似乎是this.phonetype = Samsung;,尝试将其更改为this.phonetype = Mobilephonetype.

回答by justadeveloper

It seems you have problem with your constructor,you should use parameters which you pass to constructor.You can change them related your needs and also assign parameters to appropriate class variables

您的构造函数似乎有问题,您应该使用传递给构造函数的参数。您可以根据需要更改它们,并将参数分配给适当的类变量

In your main method you instantiate an instance named Samsung as below

在您的主要方法中,您实例化一个名为 Samsung 的实例,如下所示

Mobile Samsung = new Mobile("Samsung", "3", "4", "8",
            "GPS");

This is true way to instantiate an instance.You pass parameter to constructor when you instantiate an instance.So you should pass parameters right way in your constructor like this

这是实例化实例的正确方法。实例化实例时将参数传递给构造函数。因此,您应该像这样在构造函数中以正确的方式传递参数

  public Mobile(String mobilePhoneType, int mobileScreenSize,
            int mobileMemoryCardCapacity, int mobileCameraResolution,
            String mobileGPS) {
        this.phonetype =  mobilePhoneType;
        this.screensize = mobileScreenSize;
        this.memorycardcapacity = mobileMemoryCardCapacity;
        this.cameraresolution = mobileCameraResolution;
        this.GPS = mobileGPS;

        // you do not use this ones during instantiation,you can remove them if you do not need or assign them some  default values 
        this.serviceprovider = newserviceprovider;
        this.typeofcontract = 12;
        this.checkcharge = checkcharge;


    }

You have incompatible type variables according to your parameter passing,you can change them as below

根据您的参数传递,您有不兼容的类型变量,您可以按如下方式更改它们

private String phonetype;
private int screensize;
private int  memorycardcapacity;
private int cameraresolution;
private String GPS;

回答by abhilash

The types of private instance fields 'phonetype', 'serviceprovider' and 'GPS' should be 'String' and not 'string'.

私有实例字段“phonetype”、“serviceprovider”和“GPS”的类型应该是“String”而不是“string”。