java,否则如果"错误:非法开始表达"

时间:2016-11-19 17:45:29

标签: java

//代码的想法,如果要看是点是在矩形。输入是一个6位数字(abcdef)。矩形的左上角有坐标(a,b),右下角(c,d)和点(e,f)

enter codeimport java.util.Scanner;

public class Rectangle{
 Scanner sc = new Scanner(System.in);

 public static void run() {
 int object;
 System.out.println("input:");
 object = sc.nextInt();

 if(object/100000 >= (object/1000)%10 || (object/10000)%10 <= (object/100)%10){
   System.out.print("inside");
 }else if (object/100000 <= (object/10)%10 && (object/10)%10 <= (object/1000)%10 && (object/100)%10) <= object%10 && object%10 <= (object/10000)%10){
   System.out.print("inside");
 }else {
   System.out.print("outside");
 }


  public static void main(String[] args) {
    ( new Rectangle()).run();
  }
 }

2 个答案:

答案 0 :(得分:1)

您的括号错误,请使用代码工具的格式。我发布下面的固定代码。请注意,条件必须包含在()括号之间。

if (condition) { ... }

// In case there are complete calculations within condition
if ((condition) && (condition) && (condition)) { ... }

此外它肯定会说:

  

无法从静态上下文引用非静态方法

应该通过删除static关键字来解决此错误,因为您使用的Scanner实例也不是静态的。

Scanner sc = new Scanner(System.in);

public void run() {
    int object;
    System.out.println("input:");
    object = sc.nextInt();

    if (object / 100000 >= (object / 1000) % 10 || (object / 10000) % 10 <= (object / 100) % 10) {
        System.out.print("inside");
    } else if (((object / 100000 <= (object / 10) % 10) && 
            ((object / 10) % 10 <= (object / 1000) % 10) && 
            ((object / 100) % 10) <= object % 10) && 
            (object % 10 <= (object / 10000) % 10)) 
    {
        System.out.print("inside");
    } else {
        System.out.print("outside");
    }
}

public static void main(String[] args) {
    new Rectangle().run();
}

答案 1 :(得分:0)

这是因为您在)声明中的第三个条件中有额外的else if

else if (object/100000 <= (object/10)%10 && 
         (object/10)%10 <= (object/1000)%10 && 
         (object/100)%10 remove it--->) <= object%10 && 
           object%10 <= (object/10000)%10){

此外,您无法在静态方法中引用非静态'Scanner sc',将Scanner sc设为静态或run()方法为非静态。