如何重新创建下表?

http://img.mukewang.com/618ced5b0001a41002730264.jpg

大家好,我正在尝试为作业重新创建上面的表格,但由于有关 && 函数的错误而卡住了。请注意,我们不允许使用数组,并且仅限于“switch”和“If”


这是我到目前为止所得到的:


if ((BEAK_MM == 1) && (CLAW_MM == 0) && (COLOR = "Grey")) {

   System.out.println ("The type of bird is A.");}

else if ((BEAK_MM == 2) && (CLAW_MM == 1) && (COLOR = "Grey")) 

   System.out.println ("The type of bird is A.");}

else if ((BEAK_MM == 3) && (CLAW_MM == 2) && (COLOR = "Grey")) 

   System.out.println ("The type of bird is A.");}

else if ((BEAK_MM == 4) && (CLAW_MM == 3) && (COLOR = "Grey")) 

   System.out.println ("The type of bird is A.");}

else if ((BEAK_MM <= 4.5) && (CLAW_MM == 4) && (COLOR = "Grey")) 

   System.out.println ("The type of bird is A.");}


白猪掌柜的
浏览 127回答 1
1回答

慕的地10843

假设BEAKandCLAW是整数并且COLOR是 a String,那么表格可以更像:// must be greyif ("Grey".equals(COLOR)) {&nbsp; if ((BEAK_MM == 1 && CLAW_MM == 0)&nbsp; &nbsp; &nbsp; || (BEAK_MM == 2 && CLAW_MM == 1)&nbsp; &nbsp; &nbsp; || (BEAK_MM == 3 && CLAW_MM AW == 2)&nbsp; &nbsp; &nbsp; || (BEAK_MM == 4 && CLAW_MM == 3)&nbsp; &nbsp; &nbsp; || ( (BEAK_MM == 4 || BEAK_MM == 5) && CLAW_MM == 4))) {&nbsp; &nbsp; &nbsp;System.out.println("The bird is the word");&nbsp; }}这里的逻辑是,根据表格,鸟A型一定是灰色的。然后有一些关于喙和爪类型的特定检查,但如果它不是灰色,则它不是 A 型鸟。也不需要所有 OP 堆叠的“else if”语句。有许多其他方法可以解决问题空间,但我没有数组的限制,因此大概没有其他有用的数据结构,例如Listor Set。正如@Nicholas K 所指出的,String对象必须与.equals().我也会把这些东西移到一个方法上 isBirdTypeA(int beak, int claw, String color) { ... }@Testpublic void testBirds(){&nbsp; &nbsp; final String G = "Grey";&nbsp; &nbsp; final String P = "Pink";&nbsp; &nbsp; assertTrue(isBirdTypeA(1, 0, G));&nbsp; &nbsp; assertFalse(isBirdTypeA(1, 0, P));&nbsp; &nbsp; assertTrue(isBirdTypeA(2, 1, G));&nbsp; &nbsp; assertTrue(isBirdTypeA(3, 2, G));&nbsp; &nbsp; assertTrue(isBirdTypeA(4, 3, G));&nbsp; &nbsp; assertTrue(isBirdTypeA(4, 4, G));&nbsp; &nbsp; assertTrue(isBirdTypeA(5, 4, G));&nbsp; &nbsp; assertFalse(isBirdTypeA(4, 5, G));&nbsp; &nbsp; assertFalse(isBirdTypeA(4, 0, G));&nbsp; &nbsp; assertFalse(isBirdTypeA(1, 1, G));}private static boolean isBirdTypeA(int beak, int claw, String color){&nbsp; &nbsp; if ("Grey".equals(color)) {&nbsp; &nbsp; &nbsp; &nbsp; if ((beak == 1 && claw == 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; || (beak == 2 && claw == 1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; || (beak == 3 && claw == 2)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; || (beak == 4 && claw == 3)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; || ( (beak == 4 || beak == 5) && claw == 4)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return false;&nbsp;}public static void main(String[] args) {&nbsp; int BEAK_MM = Integer.parseInt(args[0]);&nbsp; int CLAW_MM = Integer.parseInt(args[1]);&nbsp; String COLOR = args[2];&nbsp; if (isBirdTypeA(BEAK_MM, CLAW_MM, COLOR)) {&nbsp; &nbsp; System.out.println("The type of bird is A");&nbsp; }}$ java BirdBeak 1 0 Gray鸟的类型是 A
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java