-1

我有早期工作的代码,现在似乎在我对按钮事件的 case 语句方面抛出了一些错误。案例结构查看单击了哪个按钮并执行了某些操作,这是使我感到恼火的第二种情况。Eclipse 中的 case R.id.button2: 行上的错误告诉我“令牌“case”上的语法错误,@预期”,在它下面的行(Intent)上,它说“语法错误,插入” ;" 完成断言语句"。但是,错误消息似乎不断变化。早些时候,Eclipse 一直在唠叨我要放一个“;” 而不是@,我不知道为什么,因为代码之前很好。我试过把“;” 在第二个案例行中的冒号之后,仍然会引发错误。我不知道为什么' s 突然发生,或者为什么错误消息不断变化,尽管输入了它想要的内容(通常在 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

{}的 s 根本不匹配。这是一个很好的例子,说明为什么正确格式化代码很重要,包括缩进。

尽管您可以在 Eclipse 中使用Control++来执行此操作,但我已经花时间为您执行此操作ShiftF

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。)
  • (不相关)在大约第 55 行,您有一个if将始终执行的语句,原因是;.
  • (无关)大约在第 57 行,您有一个if将始终执行的语句,原因是;.
于 2012-11-05T19:09:18.957 回答