-1

以前は機能していたコードがあり、ボタン イベントの case ステートメントに関していくつかのエラーがスローされているようです。ケースの構造は、どのボタンがクリックされたかを見て何かを実行します。case R.id.button2: という行の Eclipse のエラーは、「トークン "case" の構文エラー、@ 予期された」と言っており、その下の行 (インテント) では、「構文エラー、挿入」と言っています。 ;"アサート文を完成させる". ただし、エラー メッセージは常に変化しているようです。以前、Eclipse は私に「;」を入れるようにしつこく要求していました。@ の代わりに、以前のコードは問題なかったので理由はわかりません。「;」を入れてみました 2 番目のケース行のコロンの後でもエラーがスローされます。なぜだかよくわからない」突然発生したり、エラーメッセージが必要なものを入力しているにもかかわらず、エラーメッセージが変化し続けるのはなぜですか (通常、Eclipse では問題が修正されます)。ありがとう。

これが私のコードです(完全に投稿):

import java.text.DecimalFormat;
import java.text.NumberFormat;

import android.R.integer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;


public class MainActivity extends Activity {
EditText mile,diesel;
Button button1, button2;
TextView tv, tv2, tv3;
private double x, y, z, costper, gallon, litres, ophours, ophour, drive, stopdrive;
CheckBox checkBox1, checkBox2, checkBox3, checkBox4;
NumberFormat format  = NumberFormat.getCurrencyInstance();
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mile= (EditText) findViewById(R.id.mile);
    checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
    checkBox2 = (CheckBox) findViewById(R.id.checkBox2);
    checkBox3 = (CheckBox) findViewById(R.id.checkBox3);
    checkBox4 = (CheckBox) findViewById(R.id.checkBox4);         
    button1 = (Button) findViewById(R.id.button1);
    button2 = (Button) findViewById(R.id.button2);
    button1.setOnClickListener(onClickListener);
    button2.setOnClickListener(onClickListener);
    tv = (TextView) findViewById(R.id.cost);
    tv2 = (TextView) findViewById(R.id.cpm);
    tv3 = (TextView) findViewById(R.id.gallons);
    diesel= (EditText) findViewById(R.id.diesel);
         }

private OnClickListener onClickListener = new OnClickListener() {
   @Override
   public void onClick(final View v) {
       switch(v.getId()){
         case R.id.button1:
            x=Double.parseDouble(mile.getText().toString());
            y=Double.parseDouble(diesel.getText().toString());
            ophour = x/55;
            ophours = 0;
            if (ophour>10){
                drive = ophour/10;
                if (drive>1) {
                    stopdrive = drive-1;
                    ophour = ophour + 10;
                    if (stopdrive>1);
                    ophour = ophour + (10*stopdrive);
                    if (stopdrive<1);
                    ophour = ophour + 10;
                }
                                    }
                ophours = ((x/55)*10);
            }
            if (checkBox2.isChecked()) {
                 x=x*2;
             }
            if (checkBox1.isChecked()) {
                x=x*0.62137;
            }
            gallon = x/5.5;
            if (checkBox4.isChecked()) {
                gallon = gallon + (ophour*1.1);
            }
            if (checkBox3.isChecked()) {
                litres = gallon*3.785;
                tv3.setText(new DecimalFormat("####.##").format(litres)+"L");
            }
            z=(gallon*y)+(x*0.655);
            costper=z/x;
            tv.setText(format.format(z));
            tv2.setText(format.format(costper)+"/mile"); 
            tv3.setText(new DecimalFormat("####.##").format(gallon)+"gal.");
         break;
         case R.id.button2:
            Intent browserIntent = 
            new Intent(Intent.ACTION_VIEW, Url.parse("http://www.google.com"));
                                 startActivity(browserIntent);   
         break;
       }
   ;};}         
4

1 に答える 1

4

あなた{}の は単に一致していません。これは、インデントを含め、コードを適切にフォーマットすることが重要である理由の優れた例です。

Control+ Shift+を使用してEclipseで実行できますが、時間をかけてこれを実行しましたF

import java.text.DecimalFormat;
import java.text.NumberFormat;

import android.R.integer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;

public class MainActivity extends Activity {
    EditText mile,diesel;
    Button button1, button2;
    TextView tv, tv2, tv3;
    private double x, y, z, costper, gallon, litres, ophours, ophour, drive, stopdrive;
    CheckBox checkBox1, checkBox2, checkBox3, checkBox4;
    NumberFormat format  = NumberFormat.getCurrencyInstance();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mile= (EditText) findViewById(R.id.mile);
        checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
        checkBox2 = (CheckBox) findViewById(R.id.checkBox2);
        checkBox3 = (CheckBox) findViewById(R.id.checkBox3);
        checkBox4 = (CheckBox) findViewById(R.id.checkBox4);         
        button1 = (Button) findViewById(R.id.button1);
        button2 = (Button) findViewById(R.id.button2);
        button1.setOnClickListener(onClickListener);
        button2.setOnClickListener(onClickListener);
        tv = (TextView) findViewById(R.id.cost);
        tv2 = (TextView) findViewById(R.id.cpm);
        tv3 = (TextView) findViewById(R.id.gallons);
        diesel= (EditText) findViewById(R.id.diesel);
    }

    private OnClickListener onClickListener = new OnClickListener() {
        @Override
        public void onClick(final View v) {
            switch(v.getId()){
                case R.id.button1:
                    x=Double.parseDouble(mile.getText().toString());
                    y=Double.parseDouble(diesel.getText().toString());
                    ophour = x/55;
                    ophours = 0;
                    if (ophour>10){
                        drive = ophour/10;
                        if (drive>1) {
                            stopdrive = drive-1;
                            ophour = ophour + 10;
                            if (stopdrive>1); // This IF statement will ALWAYS execute; remove the ";"
                                ophour = ophour + (10*stopdrive);
                            if (stopdrive<1); // This IF statement will ALWAYS execute; remove the ";"
                                ophour = ophour + 10;
                        }
                    }
                    ophours = ((x/55)*10);
                    } // This should not be here
                    if (checkBox2.isChecked()) {
                        x=x*2;
                    }
                    if (checkBox1.isChecked()) {
                        x=x*0.62137;
                    }
                    gallon = x/5.5;
                    if (checkBox4.isChecked()) {
                        gallon = gallon + (ophour*1.1);
                    }
                    if (checkBox3.isChecked()) {
                        litres = gallon*3.785;
                        tv3.setText(new DecimalFormat("####.##").format(litres)+"L");
                    }
                    z=(gallon*y)+(x*0.655);
                    costper=z/x;
                    tv.setText(format.format(z));
                    tv2.setText(format.format(costper)+"/mile"); 
                    tv3.setText(new DecimalFormat("####.##").format(gallon)+"gal.");
                    break;
                case R.id.button2:
                    Intent browserIntent = 
                    new Intent(Intent.ACTION_VIEW, Url.parse("http://www.google.com"));
                    startActivity(browserIntent);   
                    break;
            // There should be a "}" here.
        };
    };
}

コードの問題を示すために、ここに 4 つのコメントを追加しました (ただし、この問題に関連するのは 2 つだけです)。

  • 62行目あたりに、あっては}ならない があります。
  • 88号線あたりには がなく}、あるはずです。(終わりswitch。)
  • (Unrelated) On about Line 55, you have an if statement which will always execute due to ;.
  • (Unrelated) On about Line 57, you have an if statement which will always execute due to ;.
于 2012-11-05T19:09:18.957 に答える