简体   繁体   中英

Java cannot find symbol error - a method from another class

I'm trying to access the method changeAll from class MarkMaker the following way:

 import java.util.Scanner;

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

      System.out.print("Enter mark 1: ");
      int newm1=input.nextInt();
      System.out.print("Enter mark 2: ");
      int newm2=input.nextInt();
      System.out.print("Enter mark 3: ");
      int newm3=input.nextInt();
      String linem=input.nextLine();
      System.out.print("Enter a master password: ");
      String masterpass = input.next();
      linem=input.nextLine();

      MarkMaker mm = new MarkMaker(masterpass);
      Mark masterMark1 = mm.makeMark(newm1);
      Mark masterMark2 = mm.makeMark(newm2);
      Mark masterMark3 = mm.makeMark(newm3);

      try{
      System.out.println("The new mark 1 is "+masterMark1.provisional(masterpass));
      System.out.println("The new mark 2 is "+masterMark2.provisional(masterpass));
      System.out.println("The new mark 3 is "+masterMark3.provisional(masterpass));
      System.out.println("The new master password is is "+masterMark1.returnPass());
      int avg = mm.average();
      System.out.println("The average is "+avg);

      changeAll(5.5, 3);

      }
      catch(IncorrectPasswordException e){}

     }

    }

This is the MarkMaker class:

import java.util.*;

  class MarkMaker{
private String masterPass = "";
private ArrayList<Mark> masterArr = new ArrayList<Mark>();

public MarkMaker(String masterPass)
{
    this.masterPass = masterPass;
}

public Mark makeMark(int m)
{
    Mark newMarkObj = new Mark(m,masterPass);
    masterArr.add(newMarkObj);
    return newMarkObj;
}

public ArrayList<Mark> returnMasterArr()
{
    return masterArr;
}


public int average() throws IncorrectPasswordException
{
    int n = 0;
    for(int i=0; i<masterArr.size(); i++)
    {
        n = n + masterArr.get(i).provisional(masterPass); 
    }

    int avg = n/masterArr.size();
    return avg;
}

public void changeAll(double d, int x) throws IncorrectPasswordException
{
    for(int i=0; i<masterArr.size(); i++)
    {
        double currentMark = masterArr.get(i).provisional(masterPass); 
        System.out.println("Current mark is: "+currentMark);
        currentMark = currentMark*d; 
        System.out.println("Current mark is: "+currentMark);
        currentMark = Math.ceil(currentMark); 
        System.out.println("Current mark is: "+currentMark);
    }
} }

And this is the Mark class:

class Mark
  {
private int value;
private String password;
boolean released;

public Mark(int value, String password) 
{
    this.value = value;
    this.password = password;
    released = false;
}

public void release(String p) throws IncorrectPasswordException
{
    if(p.equals(password))
    {
    if(released==false)
        released = true;
    }
    else throw new IncorrectPasswordException(p);
}

public int value() throws UnReleasedException
{
    if(released==true)
        return value;
    else
        throw new UnReleasedException();
}

public int provisional(String p) throws IncorrectPasswordException
{
    if(p.equals(password))
    return value;
    else
    throw new IncorrectPasswordException(p);
}

public void change(String p, int arg) throws IncorrectPasswordException
{
    if(p.equals(password))
    value = arg;
    else
    throw new IncorrectPasswordException(p);
}

public String returnPass()
{
    return password;
}

public boolean isReleased()
{
    return released;
}

public boolean equals(Mark m2) throws UnReleasedException
{
    if(this.isReleased() && m2.isReleased())
    { //it throws an error, that's why i'm using the Unreleased Exception
        if(this.value()==m2.value())
            return true;
    }

     throw new UnReleasedException();
} }

The problem is that I always get a "cannot find symbol error - method changeAll(double, int), location class Question10e"

Question10e doesn't have this method. Perhaps you intended to call this on an instance of a class which does like.

mm.changeAll(5.5, 3);

changeAll是属于MarkMaker类的方法,而不是您尝试在其中调用该方法的当前Question10e类:

mm.changeAll(5.5, 3);

You need to call changeAll() through a MarkMarker object. It doesn't exist in your Question10e class. So, you could do this by:

mm.changeAll(5.5, 3)

Just because changeAll() is public doesn't mean that you can call it from anywhere. It simply means that a MarkMarker object can call it from anywhere.

你需要

 mm.changeAll(5.5, 3);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM