android - switch case及其返回值

时间:2016-12-22 15:29:50

标签: java android

对于我的新应用,我需要打印从开关盒中选择的数字。我的切换案例应该随机选择一个图像,然后if语句应检查选择了哪一个,然后更改分数。我遇到的问题是,由于Unreachable Code,我的if语句会出错。

public class MainActivity extends ActionBarActivity {

    TextView score;
    public int newscore;


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

        score = (TextView) findViewById(R.id.textView1);    
    }

        int Id() {
        Random rand = new Random();
        int imag = rand.nextInt(4);
        switch (imag) {
         case 0:
             score.setText(newscore);
             return R.drawable.a;
         case 1:
             score.setText(newscore);
             return R.drawable.b;
         case 2:
             score.setText(newscore);
             return R.drawable.c;
         default:
            score.setText(newscore);
            return R.drawable.d;
        }

        if(Id() == R.drawable.a){ 
            newscore = newscore+1;
        }

        if(Id() == R.drawable.b){ 
            newscore = newscore+10;
        }

        if(Id() == R.drawable.c){
            newscore = newscore+100;
        }

        if(Id() == R.drawable.d){ 
            newscore = newscore+1000;
        }


        }


    }

2 个答案:

答案 0 :(得分:0)

我会将switch语句放在一个方法中,这就是你的Activity应该看起来的样子:

public class MainActivity extends ActionBarActivity {

    TextView score;
    public int newscore;

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

        score = (TextView) findViewById(R.id.textView1);    


       Random rand = new Random();
    int image = rand.nextInt(4);
    int newImage = getImageResourceId(image);

    if(newImage == R.drawable.a){ 
         newscore = newscore+1;
         score.setText(newscore);
    }

    if(newImage == R.drawable.b){ 
         newscore = newscore+10;
         score.setText(newscore);
     }

     if(newImage == R.drawable.c){
          newscore = newscore+100;
          score.setText(newscore);
     }

     if(newImage == R.drawable.d){ 
           newscore = newscore+1000;
           score.setText(newscore);
     }
}

public int getImageResourceId(int image) { 

switch (image) {
     case 0:
         return R.drawable.a;
     case 1: 
         return R.drawable.b;
     case 2:
         return R.drawable.c;
     case 3: 
         return R.drawable.d;
     default:
         return R.drawable.a;
    }
}

}

进一步阅读:

  1. Writing your own Java Methods
  2. Returning a value from a Method

答案 1 :(得分:0)

您将switch并始终返回,因为switch-case default case始终返回:

if(Id() == R.drawable.a){ 
        newscore = newscore+1;
    }

    if(Id() == R.drawable.b){ 
        newscore = newscore+10;
    }

    if(Id() == R.drawable.c){
        newscore = newscore+100;
    }

    if(Id() == R.drawable.d){ 
        newscore = newscore+1000;
    }

永远不会运行。