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

标签 java android android-drawable

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

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

MyGame

问题是,我需要下面 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 语句。这具有每次测试第一个、第二个和第三个的效果。相反,我们只想将其中每个匹配一次。这是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 目前非常有用!

诀窍是知道你可能会在哪里出错。我看到了几个“失败点”。这些领域出现问题的风险较高。这将有助于检查该值是否是您当时认为的值。

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

  //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 个随机数字,然后从这些数字中选择您的大纲。这是执行此操作的一种方法(注意:有更好/更简单的方法,但我想确保它处于您当前的水平。您很快就会到达那里!)

//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。然后使用它在轮廓数组中获取相同的数字。

旁注 您可能还没有学习它们,但是 for 循环 是一种很好的方法,可以让您的代码从所有这些 if 语句减少到只有一个。 :) 如果您不熟悉它们,请先修复您的错误,然后再尝试。 Here是一个教程的链接,如果您想查看的话。

关于java - 为什么我的 if 语句不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43503132/

相关文章:

java - 线程 "main"java.util.UnknownFormatConversionException : Conversion = '-' 中出现异常

android - 为不同语言使用相同可绘制名称的最佳方法

java - 进入新 Activity 时在后台运行应用程序

java.io.IOException : Permission denied with Xuggle

java - 级联中的简单日期过滤器

java - 如何从Json对象中获取数据?

android - 尝试访问 API 时无法验证颁发者

android - 通过 Activity 设置 imageview src

android - Android drawables 文件夹中的图像存储位置?

android - 可绘制填充不起作用