【问题标题】:I get an error related to encapsulation programme [duplicate]我收到与封装程序相关的错误 [重复]
【发布时间】:2017-12-21 22:47:11
【问题描述】:

在这个程序中,我收到了错误symbol not found,请解决我的问题。我检查了所有行,但我发现所有行都是正确的。尽管编译器在ec.setName = input.next(); 行中给出了这个错误。所以请有人帮我解决这个问题。

这是我的代码:

import java.util.Scanner;
  class Encapsul 
   { 
    private String name;
    private int roll_no;
    private float marks;
    private double total;
    private String school;

    public String getName() {
    return name;
    }
    public int getRoll_no() {
     return roll_no;
    }
    public float getMarks(){
     return marks;
    }
    public double getTotal(){
      return total;
    }
    public String getSchool(){
      return school;
    }

     public void setName(String newName) {
        name = newName;
    }

    public void setRoll_no(int newRoll_no) {
        roll_no = newRoll_no;
    }

    public void setMarks(float newMarks) {
        marks = newMarks;
    }

    public void setTotal(double newTotal) {
         total = newTotal;
    }

    public void setSchool(String newSchool) {
        school = newSchool;
    }

 }


class Test{
   public static void main(String args[]){
      Scanner input = new Scanner(System.in);

    Encapsul c = new Encapsul();
    System.out.println("please enter the Name");
     c.setName = input.next();

    System.out.println("please enter the roll_no");
     c.setRoll_no = input.nextInt();

    System.out.println("please enter the marks");
     c.setMarks = input.nextFloat();

    System.out.println("please enter the total");
     c.setTotal = input.nextDouble();

    System.out.println("please enter the school");
     c.setSchool = input.next();

   System.out.println("Name :"+ c.getName());
   System.out.println("Roll no :"+ c.getRoll_no());
   System.out.println("Marks :"+ c.getMarks());
   System.out.println("Total :"+ c.getTotal());
   System.out.println("School :"+ c.getSchool());
  }
}

【问题讨论】:

  • setName 是一种方法。不是字段。
  • @Ivar 不是字段
  • 我该怎么办
  • @sudo 你是对的。

标签: java arrays loops exception


【解决方案1】:

setName() 和其他人是 methods,而不是 fields。

//...
System.out.println("please enter the Name");
c.setName(input.next());

System.out.println("please enter the roll_no");
c.setRoll_no(input.nextInt());

System.out.println("please enter the marks");
c.setMarks(input.nextFloat());

System.out.println("please enter the total");
c.setTotal(input.nextDouble());

System.out.println("please enter the school");
c.setSchool(input.next());
//...

【讨论】:

    猜你喜欢
    • 2013-10-22
    • 1970-01-01
    • 1970-01-01
    • 2020-12-12
    • 2019-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多