Java,括号放置问题{}

Java,括号放置问题{},java,Java,我正在做一个任务,我已经完成了所有的事情,但是我的if和else语句都在执行。如果我将括号添加到我认为应该添加的地方,我会得到一个错误,即“else”语句没有与“If”关联 如果我的'If'语句不执行,它将执行'else',一切正常,但如果'If'语句执行,它仍然执行'else'语句 import java.util.Scanner; public class LabProgram { public static void main(String[] args) { Sca

我正在做一个任务,我已经完成了所有的事情,但是我的if和else语句都在执行。如果我将括号添加到我认为应该添加的地方,我会得到一个错误,即“else”语句没有与“If”关联

如果我的'If'语句不执行,它将执行'else',一切正常,但如果'If'语句执行,它仍然执行'else'语句

import java.util.Scanner; 

public class LabProgram {
   public static void main(String[] args) {
      Scanner scnr = new Scanner(System.in); 
      int highwayNumber;
      int primaryNumber;
      int getnServed;

      highwayNumber = scnr.nextInt();
      getnServed = highwayNumber % 100;
      
      if (highwayNumber < 1)
         System.out.println(highwayNumber + " is not a valid interstate highway number.");
      
      if (highwayNumber > 999)
         System.out.println(highwayNumber + " is not a valid interstate highway number.");
         
      if (highwayNumber < 100 && highwayNumber > 0)
         if (highwayNumber % 2 == 0)
            System.out.println("I-" + highwayNumber + " is primary, going east/west.");
         else
            System.out.println("I-" + highwayNumber + " is primary, going north/south.");
      
      else
      
         if (highwayNumber > 999) 
            System.out.println(highwayNumber + " is not a valid interstate highway number.");
            
         if (highwayNumber < 100)
            System.out.println(highwayNumber + " is not a valid interstate highway number.");
            
         else
            if (highwayNumber > 99 && highwayNumber < 1000)
               if (highwayNumber % 2 == 0)
                  System.out.println("I-" + highwayNumber + " is auxiliary, serving I-" + getnServed + ", going east/west.");
               else 
                  System.out.println("I-" + highwayNumber + " is auxiliary, serving I-" + getnServed + ", going north/south.");
   }
}
import java.util.Scanner;
公共类实验室计划{
公共静态void main(字符串[]args){
扫描仪scnr=新扫描仪(System.in);
国际公路号码;
整数原数;
int获得服务;
highwayNumber=scnr.nextInt();
getnServed=公路编号%100;
如果(公路编号<1)
System.out.println(highwayNumber+“不是有效的州际公路编号”);
如果(公路编号>999)
System.out.println(highwayNumber+“不是有效的州际公路编号”);
如果(公路编号<100&&公路编号>0)
如果(公路编号%2==0)
System.out.println(“I-”+highwayNumber+“为主,向东/向西。”);
其他的
System.out.println(“I-”+highwayNumber+“是主要的,向北/向南。”);
其他的
如果(公路编号>999)
System.out.println(highwayNumber+“不是有效的州际公路编号”);
如果(公路编号<100)
System.out.println(highwayNumber+“不是有效的州际公路编号”);
其他的
如果(公路编号>99&&公路编号<1000)
如果(公路编号%2==0)
System.out.println(“I-”+公路号+”是辅助的,服务于I-“+getnServed+”,向东/向西行驶。”);
其他的
System.out.println(“I-”+公路号+”是辅助的,服务于I-“+getnServed+”,向北/向南);
}
}

您的逻辑表达得不太清楚,应该始终使用大括号,即使大括号是可选的。此外,您正在使用大量的
if
(s)而没有在链中匹配
else if
(s)。我试着把它清理干净

if (highwayNumber < 1) {
    System.out.println(highwayNumber + " is not a valid interstate highway number.");
} else if (highwayNumber > 999) {
    System.out.println(highwayNumber + " is not a valid interstate highway number.");
} else if (highwayNumber < 100 && highwayNumber > 0) {
    if (highwayNumber % 2 == 0) {
        System.out.println("I-" + highwayNumber + " is primary, going east/west.");
    } else {
        System.out.println("I-" + highwayNumber + " is primary, going north/south.");
    }
} else {
    if (highwayNumber > 999) {
        System.out.println(highwayNumber + " is not a valid interstate highway number.");
    } else if (highwayNumber < 100) {
        System.out.println(highwayNumber + " is not a valid interstate highway number.");
    } else if (highwayNumber > 99 && highwayNumber < 1000) {
        if (highwayNumber % 2 == 0) {
            System.out.println(
                    "I-" + highwayNumber + " is auxiliary, serving I-"
                    + getnServed + ", going east/west.");
        } else {
            System.out.println(
                    "I-" + highwayNumber + " is auxiliary, serving I-"
                    + getnServed + ", going north/south.");
        }
    }
}
if(公路编号<1){
System.out.println(highwayNumber+“不是有效的州际公路编号”);
}否则,如果(公路编号>999){
System.out.println(highwayNumber+“不是有效的州际公路编号”);
}否则如果(公路编号<100&&公路编号>0){
如果(公路编号%2==0){
System.out.println(“I-”+highwayNumber+“为主,向东/向西。”);
}否则{
System.out.println(“I-”+highwayNumber+“是主要的,向北/向南。”);
}
}否则{
如果(公路编号>999){
System.out.println(highwayNumber+“不是有效的州际公路编号”);
}否则,如果(公路编号<100){
System.out.println(highwayNumber+“不是有效的州际公路编号”);
}否则,如果(公路编号>99&&公路编号<1000){
如果(公路编号%2==0){
System.out.println(
“I-”+公路号码+“是辅助的,服务于I-”
+getnServed+“,向东/向西行驶。”);
}否则{
System.out.println(
“I-”+公路号码+“是辅助的,服务于I-”
+getnServed+“,向北/向南行驶。”);
}
}
}

在每个if和else块周围放置适当的
{
}
。编译器不考虑你的缩进,所以当你有嵌套的IFS和ELSE而没有括号时,它将无法解释你的意思。如果条件是真的(如果IF),括号需要环绕所有要执行的代码,或者不是真的(其他),你能告诉我们你认为它们应该怎么走吗?然后,我们可以解释为什么编译器会感到不安,如果前面的注释不清楚的话……代码缩进的方式不是编译器实际解释逻辑的方式。如果你认为缩进与逻辑相符,那么这可能就是你搞砸的地方。也可能值得注意的是,其中一些是死分支(即第二个>999),仍处于半休眠状态,但整个第二个
或者
看起来是多余的