不知道为什么我的程序看不到 x>=

我刚开始学习 Java,我知道我的代码很丑,所以不要介意丑陋。因此,对于学校,我必须用 Java 编写一个程序来检查您输入的坐标中的颜色。我被分配了这张图片http://prntscr.com/kt8jyh


我写了一些代码,但是每当我写出像 2;2 这样的坐标时,它就会输出红色,我该如何解决这个问题。我不确定这是如何工作的,但我需要程序在没有颜色的坐标时输出“白色”。每当我尝试

else System.out.println("white") 它不起作用我收到错误消息“线程“main”中的异常java.lang.Error:未解决的编译问题:令牌“else”上的语法错误,删除此令牌“


Scanner a = new Scanner(System.in);

Scanner b = new Scanner(System.in);


System.out.println("Ievadi x");

String x1 = a.nextLine(); 


System.out.println("ievadi y"); 

String y1 = b.nextLine();   


float x = Float.parseFloat(x1);

float y = Float.parseFloat(y1);


if (x>=6 && x<=8 || y>=2 && y<=4)

    System.out.println("Red");

else if (x>=3-x && x<=x-11 || y<=4 && y>=9) {

    System.out.println("blue");

}

else if ((x-7) + (x-7) + (y-9) + (y-9) == 25) {

    System.out.println("Green") ;


    System.out.println("white");


}


ABOUTYOU
浏览 150回答 2
2回答

函数式编程

在这段代码中&nbsp; &nbsp; else if ((x-7) + (x-7) + (y-9) + (y-9) == 25) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Green") ;&nbsp; &nbsp; // you can't put else here because it is inside a { } block&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("white");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }您可以做的是关闭该块并开始一个新的块。&nbsp; &nbsp; else if ((x-7) + (x-7) + (y-9) + (y-9) == 25) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Green") ;&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("white");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }或不使用 { } 块&nbsp; &nbsp; else if ((x-7) + (x-7) + (y-9) + (y-9) == 25)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Green") ;&nbsp; &nbsp; else&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("white");
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java