2017-11-07 2 views
0

함수에서 일부 사용자 입력을 얻으려고하고 있지만 어떤 이유로 내 코드가 입력 스트림을 인식하지 못합니다. 컴파일 할 때 81 행에 reader.readLine()을 찾을 수 없다는 오류가 발생합니다. 누구든지이 문제를 해결하는 방법을 알고 있습니까? 아니면 실행 기능이 문제없이 초기 DO-while 루프에서 일어나는 것이 가능하다?의기능에서 기호를 찾을 수 없습니다.

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 메소드 내에 정의와 그 범위가 실행 방법이 액세스하는 데, 그 안에, 당신은() 방법을 실행에 그것을 전달할 수 있습니다.

관련 문제