java - 变量不递增 1 等于正确的 switch case 方法

标签 java android imageview switch-statement android-imageview

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

工作代码

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

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

这是我的SWITCH 语句 ** EDITED 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,说如果在第 1 位删除 0,如果在第 2 位等处删除 1 个 ImageView ......请告诉我如何使其更清楚

回答大家的问题

出于某种原因,它不允许我在每个人下面发表评论,比如我有限制之类的,所以我要多解释一下 @Shiva,我只想在第一个案例完成后转到第一个案例,也许如果我添加这个它会帮助每个人理解

我的一个 imageView 的代码

                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 后调用 check if correct,现在当我调用 check if correct 时我想知道类检查是否正确以及它们在哪个编号上。如果他们在第一个数字 1 上,则数到第一。如果他们在第 2 位,那么从 0 个图像浏览量开始数到第 2 个,我希望这能解决问题。

@nachokk 不会使 iGone 成为 boolean 值,从而使其与 switch 语句不兼容吗?我知道这应该是一条评论,但我的评论不起作用?

由于某些原因,在 IE 10 上评​​论无法通过

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

好的 所以我能够找到我需要的答案的解决方案,我需要在完成命令时循环遍历每个 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 语句中的一个 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 = 每次我知道这个时都会有一个不同的数字,因为我在 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 等于消失的 ImageView 数量,以便它可以调用正确的 switch case 语句?

最佳答案

好的 所以我能够找到我需要的答案的解决方案,我需要在完成命令时循环遍历每个 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 的可见性不可见,

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

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

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

我希望我已经说清楚了,因为很多其他人都和我一样有这个问题。如果没有,请随时发送电子邮件(P.S. 我的 p 按钮有点坏了,很抱歉错过了任何应该在那里的 Ps。

如果您注意到我必须添加到其中的更改。我希望它能帮助那些在不同时间调用类时无法访问 case 语句中的每个语句的其他人。

关于java - 变量不递增 1 等于正确的 switch case 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18393365/

相关文章:

java - RXJava2 : correct pattern to chain retrofit requests

java - 从 jenkins 运行 selenium testNG 时,chrome 不会最大化

android - 关闭 USB 附件连接的正确方法

android - 使用 RotateAnimation 在 Android 中围绕固定点旋转 ImageView

android 在显示来自 url 的图像时出错

java - LDAP 查询检查用户是否存在于组或子组中

java - 为什么我的标题会混淆 onClicklistener

android - 跨安卓版本的 openOptionsMenu()

android - android中的动画垂直滑动菜单

android - Actionbarsherlock 自定义布局可点击 imageview