2012-11-05 26 views
-1

我有更早工作的代碼,現在似乎正在拋出一些關於我有一個按鈕事件的case語句的錯誤。案例結構會查看哪個按鈕被點擊並執行某些操作,這是第二種加重我的情況。 Eclipse中的錯誤說,案例R.id.button2:告訴我「令牌語法錯誤」情況下,「預期」和它下面的線(意圖),它說「語法錯誤,插入」 ;「完成斷言陳述」。但是,似乎錯誤消息不斷變化。早些時候,Eclipse正在嘮叨我把一個「;」而不是一個@,我不知道爲什麼自代碼以前罰款。我試過把「;」在第二個案例行中的冒號後仍然會引發錯誤。我不知道爲什麼它突然發生,或者爲什麼錯誤信息不斷變化,儘管輸入了它所需的東西(通常在Eclipse中修復了這些東西)。我錯過了我的病例結構中的某些東西嗎?謝謝。在Java中的語法錯誤

這裏是我的代碼(完全發佈):

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

{} s爲根本不匹配。這是爲什麼正確格式化代碼的一個很好的例子,包括縮進。

我所花費的時間爲你做這一點,雖然你可以在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結束。)
  • (無關)對於第55行,您有一個if語句,它將始終執行,因爲;
  • (無關)對於第57行,您有一個if語句,它將始終執行,因爲;
+0

我明白我做錯了什麼。說實話,我在想,但是放了;那些如果陳述大聲笑。啊。沒有咖啡無法解決的問題。我會記住格式化快捷方式,因爲,老實說,當Eclipse開始要求時}和;在一條線上完成它,那是當事情開始看起來粗糙的大聲笑。感謝您向我展示如何管理這一點,Eric。 – Shawner

+0

沒問題。祝你好運! :) – Eric