我如何正确使用" throws条款"在Java?

时间:2014-10-12 02:54:17

标签: java simulator dice clause throws

我还是编程新手,我知道我下面的代码可能很乱,但是,我如何正确使用" throws条款"?我应该将它用于赋值(将一个throws子句添加到main方法头)但是当我尝试运行它时,

我收到错误:线程中的异常" main" java.lang.Error:未解决的编译问题:     此表达式的目标类型必须是功能接口     令牌上的语法错误" throws",删除此令牌。

我做错了什么?我也应该使用三位十进制格式打印输出文件的平均值和标准差,但是,我也不知道如何去做。

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.");
        }
    }
}

我非常感谢您的帮助,谢谢您的提前!

2 个答案:

答案 0 :(得分:2)

试试这个:

PrintWriter outputFile = new PrintWriter("Results");

在调用构造函数(或方法)时,您不必声明构造函数(或任何其他方法)抛出异常。我们在方法声明中使用throws关键字,而不是在实际调用它时。修复之后,Java会抱怨抛出异常,你可以:

  1. 声明main()方法throws IOException
  2. 或使用try/catch声明
  3. 包围违规行

答案 1 :(得分:1)

将您的第一行更改为:

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