3

我知道我做错了什么,但我真的不知道它是什么,我想要完成的是让 switch 语句知道一个 ImageView 是否消失了,然后让我知道一个已经消失。如果两个图像视图消失,则移至第 2 号,然后移至第 3 号。它适用于第一个,但如果在此处编写此代码,则不会移动两个第二个

工作代码

         if (numOne == ImageView.GONE) {
                 Toast.makeText(.......).show()
            }
         if (numTwo == ImageView.GONE + 2) {
                  Toast.makeText(.......).show() 
             }

我知道如果第一个语句为真,它不会流经 if 语句,这就是为什么我需要一个 switch 语句以便它通过所有语句

这是我的开关声明 ** 编辑的开关声明

  public void checkIfCorrect() {
      //checking the game to see if the numbers are correct

             int numOne   = ImageView.GONE;
         int numTwo   = ImageView.GONE + 2;
         int numThree = ImageView.GONE + 3;
         int numFour  = ImageView.GONE + 4;
         int numFive  = ImageView.GONE + 5;
         int numSix   = ImageView.GONE + 6;
         int numSeven = ImageView.GONE + 7;
         int numEight = ImageView.GONE + 8;
         int numNine  = ImageView.GONE + 9;
         int numTen   = ImageView.GONE + 10;

      switch (iGone = 0){ //tried using switch (ImageView.GONE) didn't work
        case 1:
            if (numOne == ImageView.GONE) {
                 Toast.makeText(this, "One Gone Now Put two in the basket", Toast.LENGTH_LONG).show();

            };
        break;
         case 2:
             if (numTwo == ImageView.GONE + 2){
             Toast.makeText(this, "Two Gone Now Put Three in the basket", Toast.LENGTH_LONG).show();
             };
        break; 
         case 3:
             if (numThree == ImageView.GONE + 3) {
                 Toast.makeText(this, "Three Gone Now Put Four in the basket", Toast.LENGTH_LONG).show();

             }
        break;
         case 4: 
             if (numFour == ImageView.GONE + 4){
             Toast.makeText(this, "Four Gone Now Put Five in the basket", Toast.LENGTH_LONG).show();
            }
        break;
         case 5:
             if (numFive == ImageView.GONE + 5) {
             Toast.makeText(this, "Five Gone Now Put two in the basket", Toast.LENGTH_LONG).show();
             }
        break;

      }

我要再次尝试完成的是,当一个 ImageView 消失时,让我知道并告诉我一个消失了,然后从 0 开始移动两个并数到两个,然后一旦你完成第二个从 0 开始并计数3等……

编辑我想做什么

所以我想要做的是计数到 10。通过删除 imageview 来做到这一点,当 imageview 到达某个位置时,它将被删除,我想要上面发布的这段代码来检查删除了多少 imageView,说如果 0在第 1 号被删除,如果在第 2 号删除 1 个图像视图,等等......请让我知道如何让它更清楚

回答大家的问题

出于某种原因,它不允许我在每个人下面发表评论,比如我有限制或其他什么,所以我要多解释一点@Shiva,我只希望它在第一个案例完成后转到第一个案例,也许如果我添加这个它会帮助大家理解

我的一个 imageViews 的代码

                public boolean onTouch(View v, MotionEvent event) {
                    int x = (int)event.getX();
                  int y = (int)event.getY();

                   switch(event.getActionMasked()) {
                         //touch down so check if finger is on ball
                        case MotionEvent.ACTION_DOWN:
                             //



                        break;

                        case MotionEvent.ACTION_MOVE:
                          //moves the image with the finger
                          if (mImageView16.getX() > 0 && mImageView16.getY() >0) {
                              mImageView16.setX(x + mImageView16.getX());
                              mImageView16.setY(y + mImageView16.getY());

                          }// color square 50 50
                          if (mImageView16.getX() >= 430 && mImageView16.getX()<= 470 && mImageView16.getY() >= 80  && mImageView16.getY() <= 120) { //colorsquare RGBY
                               mImageView16.setVisibility(gone);    

                             } else {

                                 }
                          break;

                         case MotionEvent.ACTION_UP: 
                          // touch drop will do things here after drop
                             checkIfCorrect();



                }

                    return true;
             }       

              }); // one more bracket then end of class
        } // end of class

现在我在删除 ImageView 后调用检查是否正确,现在当我调用检查是否正确时,我想知道类检查是否正确以及它们的编号是否正确。如果他们在第一个数字 1 上,则数到第一。如果他们在第 2 位,那么从 0 个 imageviews 开始计数到第 2 位,我希望这可以解决问题。

@nachokk 不会使 iGone 成为布尔值,因此使其与 switch 语句不兼容吗?我再次知道这应该是评论,但我的评论不起作用?

好的,由于某种原因,IE 10 上没有通过评论

现在我添加了这个,它通过从 1 到 5 的 case 语句递增

好的,所以我能够找到我需要的答案的解决方案,我需要在完成命令时遍历每个 case 语句,这就是它的完成方式

代码

       public void checkIfCorrect() {
      //checking the game to see if the words are correct

        //counting my numbers and image views gone
         int numOne   = ImageView.GONE;
         int numTwo   = ImageView.GONE + 2;
         int numThree = ImageView.GONE + 3;
         int numFour  = ImageView.GONE + 4;
         int numFive  = ImageView.GONE + 5;
         int numSix   = ImageView.GONE + 6;
         int numSeven = ImageView.GONE + 7;
         int numEight = ImageView.GONE + 8;
         int numNine  = ImageView.GONE + 9;
         int numTen   = ImageView.GONE + 10;

      switch (iGone++){
        case 0:

            if (numOne == ImageView.GONE) {
                 Toast.makeText(this, "One Gone Now Put one more to get to 2", Toast.LENGTH_LONG).show();
                 iGone = 1;
            }
            break;
         case 1:
             if (numTwo == ImageView.GONE + 2){
             Toast.makeText(this, "Two Gone Now Put Three in the basket", Toast.LENGTH_LONG).show();
               iGone = 2;
             }

             break;
         case 2:
             if (numThree == ImageView.GONE + 3) {
                 Toast.makeText(this, "Three Gone Now Put Four in the basket", Toast.LENGTH_LONG).show();
                  iGone = 3;
             }
        break;
         case 3: 
             if (numFour == ImageView.GONE + 4){
             Toast.makeText(this, "Four Gone Now Put Five in the basket", Toast.LENGTH_LONG).show();
              iGone = 4;
             }
        break;
         case 4:
             if (numFive == ImageView.GONE + 5) {
             Toast.makeText(this, "Five Gone Now Put two in the basket", Toast.LENGTH_LONG).show();
             iGone = 5;
             }
        break;

但是我试图完成的事情并没有解决

为了更好地解决我的问题,我试图将 iGone 的值设置为 = case 语句中的一个案例。我希望 iGone 每次 ImageView.getVisibility == 消失时增加 1;Gone 是一个 ='s ImageView.GONE; 的变量。这是例如代码

**gone code**

   gone = ImageView.GONE;

现在为了让事情更清楚,每个图像都是一个类 image5(),image6(),.... 最多 16 个,除了它们与哪个 imageview 相等的变量之外,所有类都完全相同。

现在如果你看上面你会看到 image16() 的触摸类,(mImageView16)

当它到达 setX 和 setY 坐标时,我如何使它 = iGone + 1 这是我尝试过的

在每个图像类上都试过这个

         public boolean onTouch(View v, MotionEvent event) {
            int x = (int)event.getX();
           int y = (int)event.getY();

            switch(event.getActionMasked()) {
                  //touch down so check if finger is on ball
                 case MotionEvent.ACTION_DOWN:
                      //



                 break;

                 case MotionEvent.ACTION_MOVE:
                  //moves the image with the finger
                  if (mImageView5.getX() > 0 && mImageView5.getY() > 0) {
                      mImageView5.setX(x + mImageView5.getX());
                      mImageView5.setY(y + mImageView5.getY());

                  } //color square 1
                if (mImageView5.getX() >= 430 && mImageView5.getX()<= 470 && mImageView5.getY() >= 80 && mImageView5.getY() <= 120) { //colorsquare RGBY
                       mImageView5.setVisibility(gone); 

                     }
                if (mImageView5.getVisibility() == gone) { //colorsquare RGBY
                       ++iGone;  //HERE IS WHERE I TRIED IT
                //I have also tried iGone = iGone + 1; and iGone++; nothing works


                         }
                  break;

                 case MotionEvent.ACTION_UP: 
                  // touch drop will do things here after drop
                     checkIfCorrect();

        }

            return true;
     }       

      });

出于某种原因,iGone =sa 每次我知道这一点时都不同,因为我在 checkifcorrect 类中调用了一个 toast 语句,该类是带有 switch 语句的类

检查类是否正确

        public void checkIfCorrect() {

      //checking the game to see if the words are correct

         switch (iGone){
        case 1:

                 Toast.makeText(this, "One Gone Now Put one more to get to 2", Toast.LENGTH_LONG).show();
            break;
         case 2:
             Toast.makeText(this, "Two Gone Now Put Three in the basket", Toast.LENGTH_LONG).show();


             break;
         case 3:
                 Toast.makeText(this, "Three Gone Now Put Four in the basket", Toast.LENGTH_LONG).show();

        break;
         case 4: 
             Toast.makeText(this, "Four Gone Now Put Five in the basket", Toast.LENGTH_LONG).show();
        break;
         case 5:
             Toast.makeText(this, "Five Gone Now Put two in the basket", Toast.LENGTH_LONG).show();
        break;

      }

         Toast.makeText(this, "The amount is" + iGone + "Yeah", Toast.LENGTH_LONG).show();

  }

我非常困惑为什么 iGone 每次都保留 = 不同的数字。

好的,所以现在的主要问题是。如何让 iGone 等于消失的图像视图数量,以便它可以调用正确的 switch case 语句?

4

6 回答 6

1

好的,所以我能够找到我需要的答案的解决方案,我需要在完成命令时遍历每个 case 语句,这就是它的完成方式

代码

  public boolean onTouch(View v, MotionEvent event) {
                int x = (int)event.getX();
              int y = (int)event.getY();

               switch(event.getActionMasked()) {
                     //touch down so check if finger is on ball
                    case MotionEvent.ACTION_DOWN:
                         //



                    break;

                    case MotionEvent.ACTION_MOVE:
                      //moves the image with the finger
                      if (mImageView25.getX() > 0 && mImageView25.getY() >0) {
                          mImageView25.setX(x + mImageView25.getX());
                          mImageView25.setY(y + mImageView25.getY());

                      }// color square 50 50
                      if (mImageView25.getX() >= 430 && mImageView25.getX()<= 470 && mImageView25.getY() >= 80  && mImageView25.getY() <= 120) { //colorsquare RGBY
                           mImageView25.setVisibility(gone);    

                         } else {

                             }

                      break;

                     case MotionEvent.ACTION_UP: 
                      // touch drop will do things here after drop

                        if (mImageView25.isShown() != true) {//here is what was changed
                              iGone++;//what was changed
                              }  
                         checkIfCorrect();//checking if correct moved after the if statement


            }

                return true;

检查是否正确的类

        public void checkIfCorrect() {

      //checking the game to see if the words are correct
      iGone = iGone;

         switch (iGone){
        case 1:

                 Toast.makeText(this, "One Gone Now Put one more to get to 2", Toast.LENGTH_LONG).show();

                 break;
         case 3:
             Toast.makeText(this, "Two Gone Now Put Three in the basket", Toast.LENGTH_LONG).show();


             break;
         case 6 :
                 Toast.makeText(this, "Three Gone Now Put Four in the basket", Toast.LENGTH_LONG).show();

        break;
         case 10: 
             Toast.makeText(this, "Four Gone Now Put Five in the basket", Toast.LENGTH_LONG).show();
        break;

一些底层类不包括在内,因为它不相关

我将 case 语句增加了某个数字,因为当 iGone 等于该数字时,我希望调用该 case 语句。例如,如果 iGone 在它被丢弃并且图像视图的可见性不可见时增加 1,

所以当一个imageview被删除时,它会增加1、1、1,所以最终iGone将在等于3时变为=3,它将调用第三个case语句,当它等于10时,它将调用特定的case语句,因此模拟iGone 从 0 开始的效果。

因此,对于我正在创建的计数部分,我需要用户数到 1,然后我需要他们数到 2、3 等等。它的计数方式是当图像视图消失时意味着当图像视图消失时为 1,并且计数到 1 完成。然后我需要用户从 0 开始计数到 ​​2,所以当第二个 imageview 消失时,我需要调用第二个 case 语句。但是,如果我使用案例 2:那么我试图完成的操作将不起作用,因为系统不知道我们从 0 开始的一秒钟(除非我告诉它。)但是如果我告诉系统我从 0 开始然后它会再次调用第一个 case 语句,因为 iGone 将 = 1。

那不是我想要或需要的。我需要调用第二个 case 语句,因此我实际上用我的大脑说“好的,我们知道一个 ImageView 已经消失了,我需要另外两个 imageViews 的数量等于 2。(但从逻辑上讲,我们知道那将是3 因为 1 个图像视图消失了 + 2 个图像视图消失了 =3 我们显然知道),我该怎么做?” “哦,我知道,我将调用案例 3:作为我的第二个案例陈述,因为当我们删除一个图像视图时 iGone 会增加 1,所以如果我们删除 3 个图像视图 iGone 将 = 3,但玩游戏的用户会不知道。他们认为他们是从 0 开始数到 2,因为他们已经数到了 1。所以 case 语句 6、10 和 15 是第四个。我希望你看到我试图制作的模式

我希望我让自己清楚,因为有很多人和我一样有这个问题。如果不能随意发送电子邮件(PS 我的 p 按钮有点坏了,很抱歉错过了应该在那里的任何 Ps。

如果您注意到我必须添加的更改。我希望它能帮助那些在不同时间调用课程时无法理解案例陈述中每一个陈述的人。

于 2013-08-23T03:12:23.040 回答
0

我不确定我是否完全理解您的问题,但您的 iGone 变量似乎是选择一个开始执行代码的点,并且您希望该起点之后的所有案例都执行。如果是这种情况,您可以简单地删除 break 语句。

于 2013-08-23T01:59:53.930 回答
0

如果breakcase 中有语句,switch 语句不会落空。

从文档http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

break 语句是必要的,因为没有它们,switch 块中的语句就会失败:匹配 case 标签之后的所有语句都按顺序执行,无论后续 case 标签的表达式如何,直到遇到 break 语句。

因此,如果您想失败,请删除 break 语句

于 2013-08-23T02:15:21.763 回答
0

在 switch 语句中,我们只能使用数值和 Java 7 字符串文字来进行相等检查。请查看以下内容并在您的代码中尝试一下。

通过使用数值:

int number_to_compare = 3;

switch(number_to_compare) {
case 1:
       System.out.println("value is equal to 1");
       break;
case 2:
       System.out.println("value is equal to 2");
       break;
case 3:
       System.out.println("value is equal to 3");
       break;
}

通过使用字符串文字:

String string_to_compare = "A";

switch(string_to_compare) {
case "A":
       System.out.println("value is equal to A");
       break;
case "B":
       System.out.println("value is equal to B");
       break;
case "C":
       System.out.println("value is equal to C");
       break;
}

如果您需要其他类型的检查,则必须通过if/else结构来完成

于 2013-08-23T02:52:51.460 回答
0

我认为你做错了switch (iGone = 0),你应该用switch (iGone). 那可行。

switch (iGone = 0)将始终返回 1, true

于 2013-08-23T03:17:38.670 回答
0

请查看 ApiDemos。在 ApiDemos/Views/Drag and Drop 中有您要搜索的内容

于 2013-08-25T10:50:34.887 回答