Java 我如何正确使用“a”;第条;在爪哇?
我还是编程新手,我知道下面的代码可能很混乱,但是,如何正确使用“throws子句”?我应该将它用于赋值(在main方法头中添加一个throws子句),但是当我尝试运行它时 我在线程“main”java.lang中得到错误:异常。错误:未解决的编译问题: 此表达式的目标类型必须是函数接口 令牌“throws”出现语法错误,请删除此令牌 我做错了什么?我还应该使用三位小数格式将平均值和标准偏差打印到输出文件中,但是,我也不知道如何进行Java 我如何正确使用“a”;第条;在爪哇?,java,simulator,dice,clause,throws,Java,Simulator,Dice,Clause,Throws,我还是编程新手,我知道下面的代码可能很混乱,但是,如何正确使用“throws子句”?我应该将它用于赋值(在main方法头中添加一个throws子句),但是当我尝试运行它时 我在线程“main”java.lang中得到错误:异常。错误:未解决的编译问题: 此表达式的目标类型必须是函数接口 令牌“throws”出现语法错误,请删除此令牌 我做错了什么?我还应该使用三位小数格式将平均值和标准偏差打印到输出文件中,但是,我也不知道如何进行 import java.util.Random; import
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.");
}
}
}
import java.util.Random;
导入java.util.Scanner;
导入java.io.*;
公共类模拟{
公共静态void main(字符串[]args){
final int NUMBER=10000;//掷骰子的次数
随机生成器=新随机();
File myFile=新文件(“结果”);
扫描仪输入文件=新扫描仪(myFile);
PrintWriter outputFile=新的PrintWriter(“结果”)抛出::IOException;
println(“FileChooser.txt”);
println(“Filereader.txt”);
println(“FileWriter.txt”);
outputFile.close();
扫描仪键盘=新扫描仪(System.in);
int die1Value;//第一个模具上的斑点数
int die2Value;//第二个模具上的斑点数
int count=0;//掷骰子的次数
int snakeEyes=0;//蛇眼滚动的次数
int twos=0;//翻滚double two的次数
int threes=0;//翻滚双三的次数
int fours=0;//翻滚double fours的次数
int fives=0;//翻滚双五的次数
int-sixes=0;//滚动双六的次数
做{
新随机数();
System.out.println(“您滚动:”);
die1Value=keyboard.nextInt();
新随机数();
System.out.println(“您滚动:”);
die2Value=keyboard.nextInt();
}同时(计数尝试以下方法:
PrintWriter outputFile = new PrintWriter("Results");
在调用构造函数(或方法)时,您不必声明构造函数(或任何其他方法)抛出异常。我们在方法声明中使用throws
关键字,而不是在实际调用它时。修复该问题后,Java会抱怨抛出异常,您可以:
声明main()
方法抛出IOException
或者用try/catch
语句将有问题的行包围起来
将第一行更改为:
public static void main(String[] args) throws IOException {
你认为在主方法标题中添加一个throws子句意味着什么?你有没有看过throws
子句是什么?请删除问题末尾的道歉。它们不相关。我刚在读了你的评论后意识到……我觉得很闷lol。谢谢你!这可能看起来我是个混蛋,但是读一下这个:您需要的是关于异常处理的详细讨论,而不是快速回答。您的if
s有什么用?它们在末尾有分号,为什么它们在块语句之前???