2010-06-06 76 views
1

在我深入研究之前,我對Android非常陌生,上個月剛剛開始學習Java。我試圖開發我的第一個簡單的應用程序時遇到了顛簸。在線隨機教程讓大多數障礙都跳了起來。我的代碼非常精彩。任何提示都表示讚賞。如何更改不同類別的變量?

上面的問題相當廣泛,但這是我想要做的:它基本上是一個血液酒精含量計算器/飲料守護者跟蹤器。基本佈局:http://i.imgur.com/JGuh7.jpg

沿着底部的按鈕都只是普通的鈕釦,不ImageButtons(曾與該問題),這裏有一個的一些示例代碼:

<Button android:id="@+id/Button01" 
        android:layout_width="wrap_content" 
        android:layout_marginRight="5dp" 
        android:background="@drawable/addbeer"/> 

的按鈕和TextView的都是main.xml中。

我有一個名爲Global.java類中定義的變量:

package com.dantoth.drinkingbuddy; 

進口android.app.Activity;

公共類全球擴展活動{

public static double StandardDrinks = 0; 
public static double BeerOunces = 12; 
public static double BeerPercentAlcohol = .05; 
public static double BeerDrink = BeerOunces * BeerPercentAlcohol; 
public static double BeerDrinkFinal = BeerDrink * 1.6666666; 
public static double ShotOunces = 1.5; 
public static double ShotPercentAlcohol = .4; 
public static double ShotDrink = ShotOunces * ShotPercentAlcohol; 
public static double ShotDrinkFinal = ShotDrink * 1.6666666; 
public static double WineOunces = 5; 
public static double WinePercentAlcohol = .12; 
public static double WineDrink = WineOunces * WinePercentAlcohol; 
public static double WineDrinkFinal = WineDrink * 1.6666666; 
public static double OtherOunces; 
public static double OtherPercentAlcohol; 
public static double OtherDrink = OtherOunces * (OtherPercentAlcohol * .01); 
public static double OtherDrinkFinal = OtherDrink * 1.6666666; 
public static double GenderConstant = 7.5; //9 for female 
public static double Weight = 180; 
public static double TimeDrinking = 60; 
public static double Hours = TimeDrinking/60; 
public static double Bac = ((StandardDrinks/2) * (GenderConstant/Weight)) - (0.017 * Hours); 

}

最後一個變量是重要的組成部分。它根據涉及的因素計算您的BAC。

當我按下添加啤酒按鈕(Button01)時,我將它添加到StandardDrinks,模擬喝一杯啤酒。 Bac公式中的其他變量在Global.java中分配給它們的值。

,使啤酒的按鈕做的東西是我的正常類的代碼,drinkingbuddy.java:

public class DrinkingBuddy extends Activity { 
/** Called when the activity is first created. */ 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    Button button = (Button) findViewById(R.id.Button01); 
    button.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Global.StandardDrinks = Global.StandardDrinks + Global.BeerDrinkFinal; 
      Toast.makeText(DrinkingBuddy.this, "Mmmm... Beer", Toast.LENGTH_SHORT).show(); 

     } 
     }); 

通過我的感覺,StandardDrinks現在應該有1的值。然而,當我點擊計算BAC按鈕(Button05)它只輸出變量Bac,就好像StandardDrinks仍然設置爲0.下面是Calculate BAC按鈕(Button05)的代碼:

Button button4 =(Button)findViewById(R.id.Button05 ); button4.setOnClickListener(新OnClickListener(){@Override 公共 無效的onClick(視圖v){

  TextView texty; 

      texty = (TextView) findViewById(R.id.texty1); 

      texty.setText("Your BAC is " + Global.Bac); 

     } 
     }); 

它輸出下面的文本視圖: 「你BAC爲-0.017」 這是北如果StandardDrinks的值仍然爲0,那麼很明顯,這些類之間存在一些問題。任何人都可以幫助我嗎?

公式的其他元素(重量,花費的時間以及酒精%等等)是變量,因爲我最終會允許用戶在設置中更改這些值。

我聽說過水冷卻器,全局變量不是很好的編程風格,但這是我最接近實現它的工作。任何其他的做法都非常受歡迎!

回答

1

程序中有一些邏輯錯誤。

public static double Bac = ((StandardDrinks/2) * (GenderConstant/Weight)) - (0.017 * Hours); 

該變量Bac將用公式的值進行初始化。除非您明確這麼做,否則公式中用於計算BAC的變量的其他變化不會反映到該變量中。我建議有一個功能來更新BAC,如下所示

您可以在單個活動中完成以上所有操作。

 public class DrinkingBuddy extends Activity { 
/** Called when the activity is first created. */ 

double StandardDrinks = 0; 
double BeerOunces = 12; 
double BeerPercentAlcohol = .05; 
double BeerDrink = BeerOunces * BeerPercentAlcohol; 
double BeerDrinkFinal = BeerDrink * 1.6666666; 
double ShotOunces = 1.5; 
double ShotPercentAlcohol = .4; 
double ShotDrink = ShotOunces * ShotPercentAlcohol; 
double ShotDrinkFinal = ShotDrink * 1.6666666; 
double WineOunces = 5; 
double WinePercentAlcohol = .12; 
double WineDrink = WineOunces * WinePercentAlcohol; 
double WineDrinkFinal = WineDrink * 1.6666666; 
double OtherOunces; 
double OtherPercentAlcohol; 
double OtherDrink = OtherOunces * (OtherPercentAlcohol * .01); 
double OtherDrinkFinal = OtherDrink * 1.6666666; 
double GenderConstant = 7.5; //9 for female 
double Weight = 180; 
double TimeDrinking = 60; 
double Hours = TimeDrinking/60; 
double Bac; 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    Button button = (Button) findViewById(R.id.Button01); 
    button.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      StandardDrinks = StandardDrinks + BeerDrinkFinal; 
      Toast.makeText(DrinkingBuddy.this, "Mmmm... Beer", Toast.LENGTH_SHORT).show(); 

     } 
     }); 

Button button4 = (Button) findViewById(R.id.Button05); 
button4.setOnClickListener(new OnClickListener() 
{ 
@Override 
public void onClick(View v) { 

      TextView texty; 
      Bac = ((StandardDrinks/2) * (GenderConstant/Weight)) - (0.017 * Hours); 
      texty = (TextView) findViewById(R.id.texty1); 
      texty.setText("Your BAC is " + Bac); 

     } 
     }); 

我的代碼很凌亂。

的確很麻煩。我很難清除所有不必要的公共和靜態聲明。在編寫java代碼時,這個document可能有助於遵循約定。

按照從文檔,其中 他們宣稱,他們應該是在最小的範圍內 可能宣佈

變量應該初始化引用。

希望它在正確的方向上有所幫助。

1

在編碼風格和結構方面可能會有很長的迴應,但我會跳過這一點並保持前期的問題。

僅僅因爲您更新變量的值並不意味着更新了該變量發生的任何計算。在這種情況下,更改StandardDrinks的值不會導致Bac自動重新計算。具體來說,分配類靜態成員變量值的代碼在類設置期間運行一次,但我實際上並不知道dalvik VM何時執行此操作。要點是你每次需要StandardDrinks更改值才能重新計算Bac

+1

點1錯了。我想你的意思是說,定義爲「最終」的變量不能改變。聲明'靜態'意味着類有一個變量,而不是實例。 點2是正確的。 – 2010-06-06 03:28:21