1

我正在尝试通过以下方式从 MarkMaker 类访问方法 changeAll:

 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){}

     }

    }

这是 MarkMaker 类:

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);
    }
} }

这是 Mark 类:

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();
} }

问题是我总是收到“找不到符号错误 - 方法 changeAll(double, int), location class Question10e”

4

4 回答 4

2

Question10e 没有这个方法。也许您打算在喜欢的类的实例上调用它。

mm.changeAll(5.5, 3);
于 2013-11-03T21:56:39.800 回答
2

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

mm.changeAll(5.5, 3);
于 2013-11-03T21:56:44.143 回答
2

你需要changeAll()通过一个MarkMarker对象来调用。Question10e它在你的班级中不存在。因此,您可以通过以下方式执行此操作:

mm.changeAll(5.5, 3)

仅仅因为changeAll()ispublic并不意味着您可以从任何地方调用它。它只是意味着一个MarkMarker对象可以从任何地方调用它。

于 2013-11-03T21:57:02.703 回答
1

你需要

 mm.changeAll(5.5, 3);
于 2013-11-03T21:57:57.933 回答