【发布时间】:2014-12-06 22:04:42
【问题描述】:
我还是编程新手,我知道我下面的代码可能是一团糟,但是,我该如何正确使用“throws 子句”?我应该将它用于分配(在主方法头中添加一个 throws 子句)但是当我尝试运行它时,
我收到错误:线程“main”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.");
}
}
}
非常感谢您的帮助,在此先感谢您!
【问题讨论】:
-
你认为在主方法头中添加 throws 子句是什么意思?你有没有看过
throws子句是什么? -
请删除问题末尾的道歉。它们不相关。
-
读了你的评论后我才恍然大悟……我感觉很浓,哈哈。不过谢谢!
-
这看起来好像我是个混蛋,但请阅读:docs.oracle.com/javase/tutorial/essential/exceptions 您需要对异常处理进行全面的讨论,而不是快速回答。
-
你的
ifs 怎么了?它们末尾有分号,为什么它们在块语句之前???
标签: java simulator dice clause throws