【问题标题】:illegal start of expression error, working with methods表达式错误的非法开始,使用方法
【发布时间】:2014-03-20 19:50:31
【问题描述】:

因此,当我尝试运行我的 DiceGame 程序时,我在第 30 行附近不断收到非法开始表达式错误,该程序是用方法练习的。这是我的代码:

import java.util.Scanner;
public class DiceGame
{
    public static void main(String[] args)
    {
        final int RANGE = 6; 
        Scanner input = new Scanner(System.in);
        int userGuess = input.next();
        int throwResult = throw2Dice(RANGE); 
        int programGuess = throw2Dice(RANGE);


        int userError = Math.abs(throwResult - userGuess);
        int programError = Math.abs(throwResult - programGuess); 
        boolean userWins = false; 
        if(userError < programError)
        {
            userWins = true; 
        {
        System.out.println("Your guess was: " + userGuess + " the program's guess was: " + programGuess + " and the result was: " + throwResult); 
        if(userWins == false)
            System.out.println("Program Wins!!!");
        else
            System.out.println("User Wins!!!"); 
    }


    public static int throw2Dice(int r)
    { 
        int number1 = (Math.random() * r + 1);
        int number2 = (Math.random() * r + 1);
        int sum = number1 + number2; 
        return sum; 
    }
}

【问题讨论】:

    标签: methods expression


    【解决方案1】:
    if(userError < programError)
    {
        userWins = true; 
    {
    

    你有两个开括号,而不是一个开括号和一个闭括号。

    【讨论】:

    • 太棒了,谢谢。那个小错误让我很沮丧。
    猜你喜欢
    • 2014-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-16
    • 2021-12-01
    • 2018-04-30
    • 2013-10-04
    相关资源
    最近更新 更多