【问题标题】:How to display each value in turn from a for loop如何从for循环依次显示每个值
【发布时间】:2013-02-28 18:11:23
【问题描述】:

该程序为 i 的每个值提供一个 Htm 值。我想通过反复按下计算按钮来依次显示每个值。我希望它显示 i=0 的值,然后当我按下按钮时,它变为 i=1 并显示由此计算的值。 我知道我的问题有点含糊,但您的帮助将不胜感激.. 问候

我的 main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="Optimum Tilt Angle Calculator"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="Calculate" />

<TextView
    android:id="@+id/beta"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="Beta = "
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/rad"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="H = "
    android:textAppearance="?android:attr/textAppearanceLarge" />


我的 Tilt.java:

    package com.ned.tilt;

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;

    public class Tilt extends Activity implements android.view.View.OnClickListener {
private static final double PI = 3.142;
private static final double PHI = 24.8508 * (PI / 180);
private static final double Gsc = 1367;
private static final double RO = 0.2;
int i, beta, ang;

double delta, ws, wss, Rb, Rb_num, Rb_den, Ra,
        Htm, Ht;
    double Hd[] = new double[12];
double Kt[] = new double[12];
double Gon[] = new double[12];
double Hom[] = new double[12];
int nbar[] = { 17, 47, 75, 105, 135, 162, 198, 228, 258, 288, 318, 344 };
int N[] = { 31, 28, 31, 30, 31, 30, 31, 30, 31, 30, 31, 30 };
double Hm[] = { 4.38, 5.18, 5.93, 6.65, 6.67, 6.40, 5.44, 5.27, 5.62, 5.24,
        4.5, 4.11 };
Button cal;
TextView radiation, angle;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    initialize();
    cal.setOnClickListener(this);

}

private void initialize() {
    // TODO Auto-generated method stub
    cal = (Button) findViewById(R.id.button1);
    radiation = (TextView) findViewById(R.id.rad);
    angle = (TextView) findViewById(R.id.beta);
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    for (i = 0; i <= 11; i++) {
        Htm = 0;
        delta = 23.45 * (Math.sin((0.986301369) * (PI / 180)
                * (284 + (nbar[i]))));

        ws = Math.acos(-(Math.tan(PHI)) * (Math.tan(delta * (PI / 180))));
        ws = ws * (180 / PI);

        Gon[i] = Gsc
                * (1 + (0.033 * Math.cos(0.986301369 * (nbar[i])
                        * (PI / 180))));

        Hom[i] = (86400 / PI)
                * Gon[i]
                * (((Math.cos(PHI)) * (Math.cos(delta * (PI / 180))) * (Math
                        .sin(ws * (PI / 180)))) + ((ws * (PI / 180)) * (Math
                        .sin(PHI) * (Math.sin(delta * (PI / 180))))));
        Hom[i] = Hom[i] / 3600000;

        Kt[i] = Hm[i] / Hom[i];

        Hd[i] = (0.96268) - ((1.45200) * (Kt[i]))
                + ((0.27365) * (Kt[i]) * (Kt[i]))
                + ((0.04279) * (Kt[i]) * (Kt[i]) * (Kt[i]))
                + ((0.000246) * (ws))
                + ((0.001189) * (90 - (PHI * (180 / PI)) + delta));
        Hd[i] = Hd[i] * Hm[i];
        for (beta = 0; beta <= 90; beta++) {
            wss = Math.acos(-(Math.tan((PHI) - (beta * (PI / 180))) * (Math
                    .tan(delta * (PI / 180)))));
            wss = wss * (180 / PI);
            if (wss > ws) {
                wss = ws;
            }

            Rb_num = ((Math.cos((PHI) - (beta * (PI / 180))))
                    * (Math.cos(delta * (PI / 180))) * (Math.sin(wss
                    * (PI / 180))))
                    + (((PI / 180) * wss)
                            * ((Math.sin((PHI) - (beta * (PI / 180))))) * ((Math
                                .sin(delta * (PI / 180)))));
            Rb_den = ((Math.cos(PHI)) * (Math.cos(delta * (PI / 180))) * (Math
                    .sin(ws * (PI / 180))))
                    + (((PI / 180) * (ws) * (Math.sin(PHI)) * (Math
                            .sin(delta * (PI / 180)))));
            Rb = Rb_num / Rb_den;
            Ra = ((1 - (Hd[i] / Hm[i])) * (Rb))
                    + ((Hd[i] / (2 * Hm[i])) * (1 + (Math.cos(beta
                            * (PI / 180)))))
                    + ((RO / 2) * (1 - (Math.cos(beta * (PI / 180)))));

            Ht = Ra * Hm[i];
            if (Ht > Htm) {
                Htm = Ht;
                ang = beta;
            }
        }

        break;
    }
    radiation.setText("H = " + Htm);
    angle.setText("Beta =" + ang);

}
 }

【问题讨论】:

    标签: java android for-loop


    【解决方案1】:

    据我从你的问题中了解到,你不应该使用你的 for 循环

    for (i = 0; i <= 11; i++)
    

    删除这个for循环,声明一个变量,并设置“i”,当退出onClick时增加你的变量。

    private int counter=0;
    
    public void onClick(View v) {
        int i=counter;
        //everything continues, just remove the for loop
        angle.setText("Beta =" + ang);
        counter++;
        if(counter==12) counter=0;//your arrays are fixed, so we are turning back
    }
    

    【讨论】:

    • 再次感谢 Serkan...我是粉丝,真的...:)
    【解决方案2】:

    只需删除您当前的循环,因为无论如何您都会在一次迭代后调用 break: 罢工>

    for (i = 0; i <= 11; i++) {
    

    然后将i设为字段变量,并在每次按下按钮时递增:

    private int i = 0;
    
    @Override
    public void onClick(View v) {
        // Do all your calculations and call setText() like above...
    
        i++;
        if(i == Hm.length)  // Start over 
            i = 0;
    }
    

    (假设每个 Array 都等于或大于 Hm...这是危险的。您应该执行更彻底的检查。)

    【讨论】:

      【解决方案3】:

      您想如何显示它们?只是将它们附加到 TextView 的字符串上?

      textView.setText(someString);
      

      TextView 的 ListView 各自代表一个新的计算?

      private List<String> mCalculations = new LinkedList<String>();
      
      private Adapter mAdapter;
      
      @Override
      public onCreate(Bundle instance) {
          mAdapter = new ArrayAdapter(getContext(), android.R.layout.simple_list_item_1, mCalculations);
          ((ListView) findViewById(R.id.my_list_view)).setAdapter(mAdapter);
      }
      
      @Override
      public void onClick(View v) {
          new AsyncTask<Void, Void, Double>() {
              public Double doInBackground(Void... values) {
                  /* perform calculation */
                  return value;
              }
      
              public void onPostExecute(Double... values) {
                  if(values.length <= 0) return;
                  String text = String.valueOf(values[0]);
                  mCalculations.add(String.valueOf(value));
                  mAdapter.notifyDataSetChanged();
              }
          }.execute();
      }
      

      【讨论】:

      • 适配器不喜欢在 UI 线程上更新它的视图 :)
      • 用更好的方法更新 ;)
      【解决方案4】:

      不要使用 for 循环,而是创建一个全局变量 i 并在每次单击按钮时增加它

      【讨论】:

        【解决方案5】:

        在你的 onClick 下你从一个 for 循环开始

            for (i = 0; i <= 11; i++) {
        

        这是将 i 设置为每次单击按钮时从零开始。然后在循环结束时使用 break 跳出循环。这使得这个循环无用,你不需要这个部分的循环。

        在您的初始化函数中,您应该将 i 设置为零。然后在你的 onClick 函数中取出第一个 for 循环和你有中断的地方,取出它并用 i++ 替换它。

        【讨论】:

          猜你喜欢
          • 2014-04-15
          • 2013-12-30
          • 1970-01-01
          • 1970-01-01
          • 2014-07-12
          • 1970-01-01
          • 2020-07-20
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多