在深入研究它之前,我对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。但是,当我单击Calculate BAC按钮(Button05)时,它仅输出变量Bac,就像StandardDrinks仍设置为0一样。这是Calculate BAC按钮的代码( Button05):

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

            TextView texty;

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

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

        }
        });


它将以下内容输出到文本视图:“您的BAC为-0.017”。如果StandardDrinks仍为0,则这是Bac值,因此显然在类之间进行通信存在一些问题。谁能帮我??

公式中的其他元素(重量,喝酒时间以及酒精含量等)是变量,因为我最终将允许用户在设置中更改这些值。

我在水冷却器周围听说,全局变量不是好的编程风格,但这是我使其工作起来最接近的方法。非常欢迎使用其他任何方法!

最佳答案

您的程序中存在一些逻辑错误。

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 );

        }
        });



  我的代码很乱。


确实很乱。我很难删除所有不必要的公共和静态声明。该document可能有助于编写Java代码时遵循的约定。

根据该文件的引用


  变量应在以下位置初始化
  他们被宣布,他们应该
  在最小范围内声明
  可能。


希望它对正确的方向有所帮助。

08-17 15:24