Else Statement是语法错误? [英] Else Statement is a syntax error?

查看:150
本文介绍了Else Statement是语法错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码对很多人来说可能是业余的,所以如果我的任何逻辑搞砸了,那没关系。我稍后会解决这个问题。只是想知道是否有人能让我知道为什么我的else语句会出现:线程中的异常主java.lang.Error:未解决的编译问题:令牌上的语法错误else,删除此令牌

My code is probably amateur to a lot of you, so if any of my logic in it is messed up, that's okay. I'll fix that later. Just wondering if anyone could let me know why my else statement is coming up with: "Exception in thread "main" java.lang.Error: Unresolved compilation problem: Syntax error on token "else", delete this token"

我在这里阅读了一些其他问题,通常问题是人们正在检查条件(否则(blah< bleh){)与else,但我没有这样做。

I read some of the other questions on here and usually the problem is people are checking conditions (else (blah < bleh) {) with else, but I didn't do that.

import java.util.Scanner;

public class minOfThree {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner kb = new Scanner (System.in);
        int num1, num2, num3, num4, min = 0;

        System.out.println ("Please enter three numbers.");
        System.out.print ("First value: ");
        num1=kb.nextInt();
        System.out.print ("Second value: ");
        num2=kb.nextInt();
        System.out.print ("Third value: ");
        num3=kb.nextInt();
        System.out.print ("Fourth value: ");
        num4=kb.nextInt();

        if (num1 < num2)
            if (num1 < num3)
                min=num1;
            else
                min=num3;
        else if (num2 < num3)
                min=num2;
            else
                min = num3;
        ***else*** {
            min = num4;
        }
        System.out.println ("Minimum value is: " + min);
    }
}


推荐答案

获取养成总是在括号内写if-else语句的习惯,那么你永远不会想知道哪个语句与哪个测试有关。我相信以下是您希望代码的样子:

Get into the habit of always writing if-else statements within brackets, then you will never have to wonder what statement goes with which test. I believe the following is what you want your code to look like:

if (num1 < num2) {
    if (num1 < num3) {
        min = num1;
    } else {
        min = num3;
    }
} else {
    if (num2 < num3) {
        min = num2;
    } else {
        min = num3;
    }
} else {
    min = num4;
}

虽然这没有充分意义,因为在这里你得到两个 else s。

Although that does not make full sense because here you get two elses as well.

这篇关于Else Statement是语法错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆