为什么我的 if 语句不起作用?

Why doesn't my if statement work?

我在 Android Studio 中制作了一个应用程序。

快速总结:这是一款面向儿童的应用程序,当用户开始游戏时会呈现一个随机形状。用户有 4 个选项可供选择,其中一个形状是正确的形状。然后用户需要将形状拖放到轮廓中。示例图片如下所示。

问题是,我需要下面 4 个形状中的 1 个来匹配要猜的形状。我有 2 组 18 个形状,第一组是带有 ?里面。

    int[] outlines = new int[] {R.drawable.outline_0, R.drawable.outline_1, R.drawable.outline_2,
            R.drawable.outline_3, R.drawable.outline_4, R.drawable.outline_5, R.drawable.outline_6,
            R.drawable.outline_7, R.drawable.outline_8, R.drawable.outline_9, R.drawable.outline_10,
            R.drawable.outline_11, R.drawable.outline_12, R.drawable.outline_13, R.drawable.outline_14,
            R.drawable.outline_15, R.drawable.outline_16,R.drawable.outline_17};

第二组是带有面部的实际彩色形状。

    int[] images = new int[] {R.drawable.img_0, R.drawable.img_1, R.drawable.img_2, R.drawable.img_3, R.drawable.img_4,
    R.drawable.img_5, R.drawable.img_6, R.drawable.img_7, R.drawable.img_8, R.drawable.img_9, R.drawable.img_10,
            R.drawable.img_11, R.drawable.img_12, R.drawable.img_13, R.drawable.img_14, R.drawable.img_15, R.drawable.img_16,
            R.drawable.img_17};

我需要某种函数或语句,其中底部的 4 个形状不能相同,以及与需要猜测的形状对应的 1 个形状。

注意:outline_0形状对应img_0,outline_1对应img_1等

这是 activity 的全部代码。

public class SecondActivity extends AppCompatActivity {

int n;
ImageView shape1, shape2, shape3, shape4, guessShape;
ImageButton exit;
Random rand = new Random();
ImageView[] shapes = new ImageView[4];


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);

    shape1 = (ImageView) findViewById(R.id.shape1);
    shape2 = (ImageView) findViewById(R.id.shape2);
    shape3 = (ImageView) findViewById(R.id.shape3);
    shape4 = (ImageView) findViewById(R.id.shape4);
    guessShape = (ImageView) findViewById(R.id.guessShape);
    shapes[0] = shape1;
    shapes[1] = shape2;
    shapes[2] = shape3;
    shapes[3] = shape4;

    //store all the shapes in an array
    int[] images = new int[] {R.drawable.img_0, R.drawable.img_1, R.drawable.img_2, R.drawable.img_3, R.drawable.img_4,
    R.drawable.img_5, R.drawable.img_6, R.drawable.img_7, R.drawable.img_8, R.drawable.img_9, R.drawable.img_10,
            R.drawable.img_11, R.drawable.img_12, R.drawable.img_13, R.drawable.img_14, R.drawable.img_15, R.drawable.img_16,
            R.drawable.img_17};

    int[] outlines = new int[] {R.drawable.outline_0, R.drawable.outline_1, R.drawable.outline_2,
            R.drawable.outline_3, R.drawable.outline_4, R.drawable.outline_5, R.drawable.outline_6,
            R.drawable.outline_7, R.drawable.outline_8, R.drawable.outline_9, R.drawable.outline_10,
            R.drawable.outline_11, R.drawable.outline_12, R.drawable.outline_13, R.drawable.outline_14,
            R.drawable.outline_15, R.drawable.outline_16,R.drawable.outline_17};

    //generate random number between 0 and image.length
   int img1 = (int) Math.round((Math.random() * images.length));
    int img2 = (int) Math.round((Math.random() * images.length));
    int img3 = (int) Math.round((Math.random() * images.length));
    int img4 = (int) Math.round((Math.random() * images.length));
    int outlineID = (int) Math.round((Math.random() * outlines.length));


    //set the image
    guessShape.setBackgroundResource(outlines[outlineID]);
    shape1.setBackgroundResource(images[img1]);
    shape2.setBackgroundResource(images[img2]);
    shape3.setBackgroundResource(images[img3]);
    shape4.setBackgroundResource(images[img4]);

      //set tags for the imageViews
    guessShape.setTag("RandomImage");
    shape1.setTag("Shape1");
    shape2.setTag("Shape2");
    shape3.setTag("Shape3");
    shape4.setTag("Shape4");

    //1 of the 4 image views needs to match outline of the shape that needs to be guessed
    if(guessShape.getBackground().getConstantState().equals(R.drawable.outline_0)){
        int random = new Random().nextInt(shapes.length);
        shapes[random].setBackgroundResource(R.drawable.img_0);
    }
    if(guessShape.getBackground().getConstantState().equals(R.drawable.outline_1)){
        int random = new Random().nextInt(shapes.length);
        shapes[random].setBackgroundResource(R.drawable.img_1);
    }
    if(guessShape.getBackground().getConstantState().equals(R.drawable.outline_2)){
        int random = new Random().nextInt(shapes.length);
        shapes[random].setBackgroundResource(R.drawable.img_2);
    }
    if(guessShape.getBackground().getConstantState().equals(R.drawable.outline_3)){
        int random = new Random().nextInt(shapes.length);
        shapes[random].setBackgroundResource(R.drawable.img_3);
    }
    if(guessShape.getBackground().getConstantState().equals(R.drawable.outline_4)){
        int random = new Random().nextInt(shapes.length);
        shapes[random].setBackgroundResource(R.drawable.img_4);
    }
    if(guessShape.getBackground().getConstantState().equals(R.drawable.outline_5)){
        int random = new Random().nextInt(shapes.length);
        shapes[random].setBackgroundResource(R.drawable.img_5);
    }
    if(guessShape.getBackground().getConstantState().equals(R.drawable.outline_6)){
        int random = new Random().nextInt(shapes.length);
        shapes[random].setBackgroundResource(R.drawable.img_6);
    }
    if(guessShape.getBackground().getConstantState().equals(R.drawable.outline_7)){
        int random = new Random().nextInt(shapes.length);
        shapes[random].setBackgroundResource(R.drawable.img_7);
    }
    if(guessShape.getBackground().getConstantState().equals(R.drawable.outline_8)){
        int random = new Random().nextInt(shapes.length);
        shapes[random].setBackgroundResource(R.drawable.img_8);
    }
    if(guessShape.getBackground().getConstantState().equals(R.drawable.outline_9)){
        int random = new Random().nextInt(shapes.length);
        shapes[random].setBackgroundResource(R.drawable.img_9);
    }
    if(guessShape.getBackground().getConstantState().equals(R.drawable.outline_10)){
        int random = new Random().nextInt(shapes.length);
        shapes[random].setBackgroundResource(R.drawable.img_10);
    }
    if(guessShape.getBackground().getConstantState().equals(R.drawable.outline_11)){
        int random = new Random().nextInt(shapes.length);
        shapes[random].setBackgroundResource(R.drawable.img_11);
    }
    if(guessShape.getBackground().getConstantState().equals(R.drawable.outline_12)){
        int random = new Random().nextInt(shapes.length);
        shapes[random].setBackgroundResource(R.drawable.img_12);
    }
    if(guessShape.getBackground().getConstantState().equals(R.drawable.outline_13)){
        int random = new Random().nextInt(shapes.length);
        shapes[random].setBackgroundResource(R.drawable.img_13);
    }
    if(guessShape.getBackground().getConstantState().equals(R.drawable.outline_14)){
        int random = new Random().nextInt(shapes.length);
        shapes[random].setBackgroundResource(R.drawable.img_14);
    }
    if(guessShape.getBackground().getConstantState().equals(R.drawable.outline_15)){
        int random = new Random().nextInt(shapes.length);
        shapes[random].setBackgroundResource(R.drawable.img_15);
    }
    if(guessShape.getBackground().getConstantState().equals(R.drawable.outline_16)){
        int random = new Random().nextInt(shapes.length);
        shapes[random].setBackgroundResource(R.drawable.img_16);
    }
    if(guessShape.getBackground().getConstantState().equals(R.drawable.outline_17)){
        int random = new Random().nextInt(shapes.length);
        shapes[random].setBackgroundResource(R.drawable.img_17);
    }
}

}

  1. 我在这里做错了什么?
  2. 为什么我的 if 语句不能确保下面的随机形状之一对应于要猜测的形状?
  3. 如何确保底部的 4 个形状不同?

任何解决我的问题的技巧都会很棒。谢谢! :)

没有日志、堆栈跟踪或输出,很难准确知道哪里出错了。您可以采取多种措施来修复此错误或防止以​​后出现其他错误。

设置变量

让我们将 guessShape.getBackground().getConstantState() 保存为变量。现在我不知道这是什么类型,所以我现在就称它为对象。请更新为正确的类型。

Object currentBackground = guessShape.getBackground().getConstantState();

If-Else If

目前您正在使用 if 语句,后面跟着更多的 if 语句。这具有每次测试第一个 if 而不是第二个而不是第三个的效果。相反,我们只想匹配每一个一次。这里有一个tutorial

让我们改变一下:

if(currentBackground.equals(R.drawable.outline_0){
    ...
}
if(currentBackgorund.equals(R.drawable.outline_1){
    ...
} .... 

为此:

if(currentBackground.equals(R.drawable.outline_0){
    ...
} else if(currentBackgorund.equals(R.drawable.outline_1){
    ...
} .... 

其他

现在我们知道它只会匹配一次,我们要确保它确实被找到。我们希望在刚刚完成的 if-else if 语句末尾捕获一个 else

.... } else if (currentBackground.equals(R.drawable.outline_17){
       ... 
} else {
     // How do you want to handle if the background did not equal any of your images? 
}

调试

现在我们有了一些重大改进。你需要追踪它哪里出错了。一种有用的方法是将内容打印到控制台。在 NetBeans 或 Eclipse 等 IDE 中有更酷更强大的调试方法,但 System.out.println 目前非常好!

诀窍是知道你可能哪里出错了。我看到一对 "points of failure"。这些是您出错的风险更高的区域。这将有助于检查该值是否符合您当时的想法。

根据您的随机数。弄清楚数字是什么。它们在正确的范围内吗?这也将帮助您调试它应该是什么形状。

  //generate random number between 0 and image.length
int img1 = (int) Math.round((Math.random() * images.length));
int img2 = (int) Math.round((Math.random() * images.length));
int img3 = (int) Math.round((Math.random() * images.length));
int img4 = (int) Math.round((Math.random() * images.length));
int outlineID = (int) Math.round((Math.random() * outlines.length));

// Print these to know what they are, especially outlineID. 

您可以在每个 if 语句中打印以查看哪个语句(包括 else)被捕获以及为什么。您可以确保它是您想要的那个。

结论

试试看。它可能不会捕获您的错误,但至少您会具体了解将其设置为什么以及如果捕获了哪个。这将帮助您彻底调试。如果大纲每次都进入正确的 if 语句,那么我们就知道 if 语句内部有问题!调试可能不会在第一时间发现问题,但它确实缩小了范围。

不匹配的图像答案

我正在编辑我的答案以包含此内容。我很确定我发现了你的问题。

在您的代码中:

//generate random number between 0 and image.length
int img1 = (int) Math.round((Math.random() * images.length));
int img2 = (int) Math.round((Math.random() * images.length));
int img3 = (int) Math.round((Math.random() * images.length));
int img4 = (int) Math.round((Math.random() * images.length));
int outlineID = (int) Math.round((Math.random() * outlines.length));

您正在为图像创建 4 个随机图像,但随后您为轮廓创建随机图像。这意味着您可以获得图像 1、2、3、4,但随后会在轮廓上得到图像 17 的轮廓!这将匹配您的 if 语句,但它会给您 17 的图像而不是您想要的图像。

在这种情况下,您想要获得 4 个随机数,然后从中挑选出您的大纲。这是执行此操作的一种方法(注意:有 better/easier 种方法,但我想确保它处于您当前的水平。您很快就会到达那里!)

//generate random number between 0 and image.length
int img1 = (int) Math.round((Math.random() * images.length));
int img2 = (int) Math.round((Math.random() * images.length));
int img3 = (int) Math.round((Math.random() * images.length));
int img4 = (int) Math.round((Math.random() * images.length));

int whichImg = (int) Math.round((Math.random() * 4));

if(whichImg == 1){
   whichImg = img1;
} else if(whichImg == 2){
   whichImg = img2;
} else if(whichImg == 3){
   whichImg = img3;
} else {
   whichImg = img4;
}

int outlineID = outlines[whichImg];

这会给你一个随机数 1-4。然后,您可以使用将正确的图像保存到 whichImg。然后你用它在你的 outlines 数组中得到相同的数字。

旁注 您可能还没有学过它们,但是 for loop 将是将您的代码从所有这些 if 语句缩减为只有一个的绝佳方式。 :) 如果您不熟悉它们,请先修复您的错误,然后再尝试。 Here 是一个 link 教程,如果您想查看它。