扫描程序的构造函数(Java)

时间:2011-12-02 03:40:42

标签: constructor

public Note( Scanner input )
{
    freq = input.nextInt();
}

上面的代码在一个单独的文件中,包含了运行所需的所有内容。

    public void keyTyped( KeyEvent e )
    {
    char key = e.getKeyChar();

    if( 'a' <= key && key <= 'y' )
    {
      // Ex5:  put code here to add a new note to the list of notes
      //       in position current and increment current.
      //       Tip:    key - 'a'   will be an integer between 0 and 24,
      //       inclusive, corresponding to the desired note number when
      //       the user presses 'a' through 'x'

    //    Scanner input = new Scanner( System.in );
---->//     song.add( current, new Note( ? ));
    //      current++;
        System.out.println(key - 'a');

    }

我需要找到一种方法将int传递给需要扫描器的构造函数。请注意,这不是整个代码,而只是我认为需要的代码。

1 个答案:

答案 0 :(得分:0)

int中使用Note constructor,就像这样

public Note( int num ) { freq = num; }

然后使用input.nextInt() song.add( current, new Note(input.nextInt() ) );

进行调用