2017-11-07 21 views
0

関数でユーザー入力を取得しようとしていますが、何らかの理由でコードが入力ストリームを認識しません。私がコンパイルすると、81行目でreader.readLine()が見つからないというエラーが出ます。誰もがこれを修正する方法を知っていますか?それとも、実行機能が問題なく初期DO-whileループで発生することが可能です?sの機能でシンボルを見つけることができません。

import java.io.*; 

public class JavaLab3 { 


public static void main(String[] args) { 
    // first we define our input streams. 
    InputStreamReader input = new InputStreamReader(System.in); 
    BufferedReader reader = new BufferedReader(input); 

    // variable declarations 
    String sName, playAgain; 

    // we catch exceptions if some are thrown. 
    // an exception would be entering a string when a number is expected 
    try { 
     System.out.println("what is your name?"); 

     // we read a string from the stream 
     sName = reader.readLine(); 
     do { 
      run(); 
      System.out.println(sName + "would you like to play again?"); 
      System.out.println("Please answer in lowercase 'yes' or 'no'."); 
      playAgain = reader.readLine(); 
     } while (playAgain != "no"); 

    } catch (IOException e){ 
     System.out.println("Error reading from user"); 
    } 

} 

public static int maxRun (int runTotal) { 
int highScore = 0; 
if (runTotal > highScore) { 
    highScore = runTotal; 
} else { 
    `highScore = highScore`} 
return highScore; 
} 


public static int run() { 

Integer currentRun = 0, uNumber, counter; 
final Integer MAX = 4; 
final Integer MAX_NUMBER = 100; 

//While current total is less than the max 
while (currentRun < MAX) { 
    System.out.println("Please enter a number."); 
    //store user number 
    uNumber = Integer.parseInt(reader.readLine()); //Line throwing the error. 
    //for each number under 5 repetitions 
    for (counter = 0; counter <= MAX_NUMBER ; counter++) { 
    if (uNumber < 0) { 
     System.out.println("Please enter a positive number."); 
    } else if ((uNumber % 2) != 0) { 
     System.out.println("Please enter an even number."); 
    } else { 
     //sets current total and restarts the loop 
     currentRun = uNumber + currentRun; 
    } 
    } 
} 
//Calls maxRun function to see if score the score is the highest. 
maxRun(currentRun); 
System.out.println("The max for this run was, " + currentRun + "."); 
return currentRun; 
} 

}

答えて

1

readermain()方法の範囲内で定義されています。 run()の範囲には存在しません。

これをクラスのメンバーとして定義することができます。したがって、両方のメソッドにアクセスできるようになります。または、メソッド間でパラメータとして渡します。

-1

BufferedInputリーダーの定義は、メイン関数の外で、つまりクラス内で宣言し、そのクラスのどのメソッドでもグローバルでアクセス可能にする必要があります。

Class name { 
     buffereInput reader = .... 
..... 
} 
0

リーダーはmainメソッドの内部で定義されており、その範囲は、実行方法とにアクセスするために、その内部にあることが利用可能であることができるように、あなたはラン()方法でそれを通過することができます。

関連する問題