Androidで簡易電卓を作成しています。
最後の計算結果表示の式でエラーが出てしまいます。
エラーの内容は下記になります。
Unreachable statement
式の方貼り付けますのでご指摘頂ければと思います。
package jp.hit.simplecalk2;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
//コントロール参照
private EditText tfValue1, tfValue2;
private TextView Reslut;
private Button buttonpurasu, buttonkakeru, buttonhiku, buttonwaru;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } private void init() { //コントロール参照設定 buttonpurasu = (Button) findViewById(R.id.buttonpurasu); buttonkakeru = (Button) findViewById(R.id.buttonkakeru); buttonhiku = (Button) findViewById(R.id.buttonhiku); buttonwaru = (Button) findViewById(R.id.buttonwaru); tfValue1 = (EditText) findViewById(R.id.tfValue1); tfValue2 = (EditText) findViewById(R.id.tfValue2); Reslut = (TextView) findViewById(R.id.Reslut); buttonpurasu.setOnClickListener(this); buttonwaru.setOnClickListener(this); buttonhiku.setOnClickListener(this); buttonkakeru.setOnClickListener(this); } @Override public void onClick(View view) { //入力値の取得 String str01 = tfValue1.getText().toString(); String str02 = tfValue2.getText().toString(); //値の方変化 int num01 = 0; int num02 = 0; try { num01 = Integer.parseInt(str01); num02 = Integer.parseInt(str02); } catch (NumberFormatException e) { e.printStackTrace(); Toast.makeText( this, "計算が出来ません", Toast.LENGTH_LONG).show(); return; } catch (Exception e) { Toast.makeText( this, "エラー", Toast.LENGTH_LONG).show(); return; } int numRes = 0; //ボタン判定処理 switch (view.getId()) { case R.id.buttonpurasu: numRes = num01 + num02; break; case R.id.buttonhiku: numRes = num01 - num02; break; case R.id.buttonwaru: numRes = num01 / num02; break; case R.id.buttonkakeru: if (num02 == 0) { Toast.makeText( this, "0割ダメ", Toast.LENGTH_LONG).show(); return; } numRes = num01 * num02; break; //計算結果の表示 Reslut.setText(String.valueOf(numRes)); } }
}