我编写了一个Java程序,但一行中有一个错误,我希望您能帮助先生找到此错误的更正

问题描述:

写一个班级人员,每个人都有姓名,年龄和住址.

该类还具有构造函数以及以下方法:
Display()显示人员数据
设置Address()和getAddress()来设置和获取人员地址

编写Java程序以创建一个用户,他的数据由用户输入,然后显示他的数据


代码:

Write a class person each person has a name age address and job.

The class also has a constructor as well as the following methods:
Display() to display the person data
set Address() and getAddress() to set and get the persons address

write java program to create a person his data was entered by the user then display his data


The code:

import java.util.Scanner;
public class person
{
    String name,address,job;
    byte age;
    person(String name,byte age,String address,String job)
    {
        this.name=name;
        this.age=age;
        this.address=address;
        this.job=job;
    }
    void Display()
    {
        System.out.println("name="+name);
        System.out.println("age="+age);
        System.out.println("address="+address);
        System.out.println("job="+job);
    }
    void setAddress()
    {
        String add=address;
    }
    String getAddress()
    {
        return address;
    }

    public static void main(String[] args)
    {
        Scanner input=new Scanner (System.in);
        person p = new person();
        System.out.print("name is");
        String s=input.next();
        System.out.print("age is");
        byte x=input.nextByte();
        System.out.print("address is");
        String y=input.next();
        System.out.print("job is");
        String z=input.next();
        p.Display();
    }
}

此处存在许多问题,其中大多数与Java编译器不灵通有关! :laugh:

您需要返回您的讲义,然后再次阅读它们,因为这表明您确实除了Java的基本语法之外什么都不懂.

例如,查看您的setAddress和getAddress例程:后者返回一个值,但是前者根本不接受要设置的值.因此,您已经创建了一个临时值,而不是将当前地址加载到其中的临时值,原因可能是因为您知道必须在其中执行某些操作,但不知道要做什么.

对不起,但是我不会解决问题",因为这将需要编写您的整个作业,并且在此处提供代码将对您没有任何帮助!您似乎尚未完全掌握太多基本概念.
回到您的笔记,再阅读一遍,看看可以解决什么-如果遇到特定的问题,我们将很乐意为您提供帮助.
There are a number of problems here, most of which are related to the java compiler not being psychic! :laugh:

You need to go back to your lecture notes, and read them again, because this shows that you really haven''t understood anything except teh basic syntax of java.

For example, look at your setAddress and getAddress routines: the latter returns a value, but the former does not accept a value to set at all. So you have created a temporary value into which you load the current address instead, presumably because you know you have to do something there, but don''t know what.

I''m sorry, but I am not going to fix "the problem" because it would entail writing the whole of your homework, and giving you the code here will teach you nothing! There are too many fundamental concepts that you appear to have completely not grasped yet.
Go back to you notes, read them again, and see what you can work out - if you get a specific problem we will be happy to help.


您已经创建了参数化的构造函数.创建对象时需要将参数传递给它

main()中的这一行代码
You have created a parameterized constructor. You need to pass the parameters to it while creating the object

This line of code in the main()
person(String name,byte age,String address,String job)



将更改为



will change to

person p =new person("aspnet_regiis",21,"Pune India","Software Developer")


import java.util.Scanner;
public class Person
{
    String name,address,job;
    byte age;
    Person(){}
    Person(String name,byte age,String address,String job)
    {
        this.name=name;
        this.age=age;
        this.address=address;
        this.job=job;
    }
    void Display()
    {
        System.out.println("name   = "+name);
        System.out.println("age    = "+age);
        System.out.println("address= "+address);
        System.out.println("job    = "+job);
    }
    void setAddress(String address)
    {
        String add=address;
    }
    String getAddress(String address)
    {
        return address;
    }
 
    public static void main(String[] args)
    {
        Scanner input=new Scanner (System.in);
        Person p = new Person();
        
        System.out.print("name is ");
        String s=input.next();
        System.out.print("age is ");
        byte x=input.nextByte();
        System.out.print("address is ");
        String y=input.next();
        System.out.print("job is ");
        String z=input.next();
        Person P = new Person(s,x,y,z);
        P.Display();
}
}




它非常简单好吧,如果您在类中定义了带有自变量的任何类型的构造函数,那么您还必须定义一个不带自变量的默认构造函数.在这种情况下,java不提供默认构造函数.类.




Its Very simple Well if u define any type of Constructor with arguments in a class then u have to define also a default Constructor with no arguments .java not provide a default Constructor in this your case .Java Can provide default Constructor when no Constructor in the class.