2014-10-12 7 views
1

저는 프로그래밍에 익숙하지 않고 아래에있는 코드가 엉망이라고 알고 있지만 어떻게 "throws clause"를 올바르게 사용합니까? 할당 (주 메서드 헤더에 throws 절을 추가) 할 때 사용해야하지만, 실행하려고하면Java에서 "throws clause"를 올바르게 사용하는 방법은 무엇입니까?

오류가 발생합니다. 스레드 "main"의 예외 java.lang.Error : 해결되지 않은 컴파일 문제 : 이 표현식의 대상 유형은 기능 인터페이스 여야합니다 토큰 구문 오류가 "throws"되어이 토큰을 삭제하십시오.

내가 뭘 잘못하고 있니? 또한 3 진수 형식을 사용하여 출력 파일에 평균 및 표준 편차를 인쇄하기로되어 있지만, 그 중 하나에 대해서도 이해할 수 없습니다.

import java.util.Random; 
import java.util.Scanner; 
import java.io.*; 

public class DiceSimulation { 
    public static void main(String[] args) { 
     final int NUMBER = 10000; // the number of times to roll the dice 

     Random generator = new Random(); 
     File myFile = new File("Results"); 
     Scanner inputFile = new Scanner(myFile); 
     PrintWriter outputFile = new PrintWriter("Results")throws::IOException; 
     outputFile.println("FileChooser.txt"); 
     outputFile.println("Filereader.txt"); 
     outputFile.println("FileWriter.txt"); 
     outputFile.close(); 
     Scanner keyboard = new Scanner(System.in); 

     int die1Value; // number of spots on the first die 
     int die2Value; // number of spots on the second die 
     int count = 0; // number of times the dice were rolled 
     int snakeEyes = 0; // number of times snake eyes is rolled 
     int twos = 0; // number of times double two is rolled 
     int threes = 0; // number of times double three is rolled 
     int fours = 0; // number of times double four is rolled 
     int fives = 0; // number of times double five is rolled 
     int sixes = 0; // number of times double six is rolled 

     do { 
      new Random(); 
      System.out.println("You rolled: "); 
      die1Value = keyboard.nextInt(); 

      new Random(); 
      System.out.println("You rolled: "); 
      die2Value = keyboard.nextInt(); 

     } while (count <= 0); 

     if (die1Value == 1) 
      ; 
     { 
      ++snakeEyes; 
     } 
     if (die1Value == 2) 
      ; 
     { 
      ++twos; 
     } 
     if (die1Value == 3) 
      ; 
     { 
      ++threes; 
     } 
     if (die1Value == 4) 
      ; 
     { 
      ++fours; 
     } 
     if (die1Value == 5) 
      ; 
     { 
      ++fives; 
     } 
     if (die1Value == 6) 
      ; 
     { 
      ++sixes; 
     } 
     ++count; 
     { 

      System.out.println("You rolled snake eyes " + snakeEyes 
        + " out of " + count + " rolls."); 
      System.out.println("You rolled double twos " + twos + " out of " 
        + count + " rolls."); 
      System.out.println("You rolled double threes " + threes 
        + " out of " + count + " rolls."); 
      System.out.println("You rolled double fours " + fours + " out of " 
        + count + " rolls."); 
      System.out.println("You rolled double fives " + fives + " out of " 
        + count + " rolls."); 
      System.out.println("You rolled double sixes " + sixes + " out of " 
        + count + " rolls."); 
     } 
    } 
} 

정말 감사드립니다. 미리 감사드립니다!

+0

주 방법 head_에 throws 절을 추가한다고 생각하십니까? 'throws' 조항이 무엇인지 보셨습니까? –

+0

질문 끝에 사과를 삭제하십시오. 그것들은 관련이 없습니다. –

+0

당신의 코멘트를 읽은 후에 그것은 단지 나를 떠올 랐다. .. 나는 매우 조밀 한 LOL 느낌이 든다. 그래도 감사합니다! –

답변

2

이 시도 :

PrintWriter outputFile = new PrintWriter("Results"); 

당신은 생성자 (또는 방법)를 호출 할 때 생성자 (또는 그 문제에 대해 다른 방법) 예외 를 throw 것을 주장 할 필요가 없습니다. 선언throws 키워드를 사용합니다. 실제로는 호출하지 않습니다.

  1. main() 방법 throws IOException
  2. 가 아니면 try/catch 문을 일으키는 라인을 둘러싸는 것을 선언
1

첫 번째 변경 : 있음을 고정 후, 자바는 예외가 발생 불평 할 것이다, 당신도 줄에 :

public static void main(String[] args) throws IOException {