2014-10-12 87 views
1

我还是新手编程,我知道我下面的代码可能是一团糟,但是,如何正确使用“throws子句”?我应该使用它作为一个任务(添加一个throws子句的主要方法头),但当我尝试运行它,如何正确使用Java中的“throws clause”?

我得到的错误:线程“主”java.lang.Error异常:未解决的编译问题: 此表达式的目标类型必须是功能性接口 对标记“抛出”的语法错误,请删除此标记。

我在做什么错?我也应该使用三位小数格式打印输出文件的平均值和标准偏差,但是我不明白怎么去做。

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

你觉得_add一个抛出子句的主要方法header_意味着什么?你有没有看过“抛出”条款是什么? – 2014-10-12 02:55:02

+0

请在问题结尾处删除道歉。它们不相关。 – 2014-10-12 02:55:37

+0

看完你的评论后,我才明白...我感到如此密集的大声笑。谢谢你! – 2014-10-12 02:56:50

回答

2

试试这个:

PrintWriter outputFile = new PrintWriter("Results"); 

您不必声明构造函数(或与此有关的任何其他方法)调用构造函数(或方法)时抛出异常。我们在方法声明中使用throws关键字,而不是在实际调用它时使用。固定在此之后,Java将抱怨抛出一个异常,您可以:

  1. 声明,main()方法throws IOException
  2. 或环绕违规行以try/catch声明
1

更改您的第一个行到:

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