出现了些问题,以我现在的能力实在找不到原因。
问题如下:for循环当条件部位true时仍继续循环,一直循环到他心情好。
具体情况例for(;x < y;){}当x>y时仍然在循环,这是什么原因呢,换了while情况依然一样。
具体代码如下,如果没时间就不用看了。请求各位大神告诉小弟原因,还有解决的思路。拜谢了package bnu.ep;import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;public class ReadView extends View { //屏幕宽度
int screenWidth ;
//屏幕高度
int screenHeight ;
//文字大小
int word_Size = 20;
//行距
int Leading = 2;

//当前行数
int Now_LineNumber = 1;
//文字
String str = "jiyuhang 中hangm yujia =lljyhjyhjyjjjmmmqssssmmmmmmskkkjdjh,hdjj,jiyuhang 中hangm ";
//当前文字
int k = 0;
//当前页数
int now_page = 0;
//每页字数
int a[]  = new int[10]; //字符总长度
int word_long = str.length();
// 实例化绘制方法
Paint m_paint = new Paint(Paint.ANTI_ALIAS_FLAG);



public ReadView(Context context) {
super(context);
// TODO Auto-generated constructor stub
setFocusable(true);
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//Now_LineNumber = 1;
//每页行数
int line_number = (screenHeight/word_Size) - 1;

//文字大小
m_paint.setTextSize(word_Size);
canvas.drawARGB(255,255,255,255);   
//字符绘制Y坐标
int y = word_Size;
Log.i("screenHeight",""+screenHeight);
Log.i("screenWidth",""+screenWidth); while(this.Now_LineNumber < line_number)
{
String str2 = "";
if(this.k >= word_long||y >= screenHeight)
{  
break;
}
for(;;k++)
{
//Log.i("k",""+k);
if(k < word_long)
{
str2 += str.substring(k,k+1);
}
else
{
break;
}
float textWidth = m_paint.measureText(str2);
if(textWidth >= screenWidth)
{
if(textWidth > screenWidth)
{
int beyond_long = str2.length();
str2 = str2.substring(0,beyond_long - 1);
break;
}
break;
}
}
//绘制文字
canvas.drawText(str2, 0, y, m_paint);
//y坐标每次增加字符高度(即字体大小+行距)
y =  y + word_Size + Leading;
Now_LineNumber++;
}
}}用的是全局变量,现在那个for改成while。

解决方案 »

  1.   

    把代码放在[/code] 之前,放在[code=Java]之后
      

  2.   

    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.util.Log;
    import android.view.KeyEvent;
    import android.view.View;public class ReadView extends View {//屏幕宽度
    int screenWidth ;
    //屏幕高度
    int screenHeight ;
    //文字大小
    int word_Size = 20;
    //行距
    int Leading = 2;//当前行数
    int Now_LineNumber = 1; 
    //文字
    String str = "jiyuhang 中hangm yujia =lljyhjyhjyjjjmmmqssssmmmmmmskkkjdjh,hdjj,jiyuhang 中hangm ";
    //当前文字
    int k = 0;
    //当前页数
    int now_page = 0;
    //每页字数
    int a[] = new int[10];//字符总长度
    int word_long = str.length();
    // 实例化绘制方法
    Paint m_paint = new Paint(Paint.ANTI_ALIAS_FLAG);public ReadView(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
    setFocusable(true);
    }
    protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    //Now_LineNumber = 1;
    //每页行数
    int line_number = (screenHeight/word_Size) - 1;//文字大小
    m_paint.setTextSize(word_Size);
    canvas.drawARGB(255,255,255,255);   
    //字符绘制Y坐标
    int y = word_Size;
    Log.i("screenHeight",""+screenHeight);
    Log.i("screenWidth",""+screenWidth);while(this.Now_LineNumber < line_number)
    {
    String str2 = "";
    if(this.k >= word_long||y >= screenHeight)
    {   
    break;
    }
    for(;;k++)
    {
    //Log.i("k",""+k);
    if(k < word_long)
    {
    str2 += str.substring(k,k+1);
    }
    else
    {
    break;
    }
    float textWidth = m_paint.measureText(str2);
    if(textWidth >= screenWidth)
    {
    if(textWidth > screenWidth)
    {
    int beyond_long = str2.length();
    str2 = str2.substring(0,beyond_long - 1);
    break;
    }
    break;
    }
    }
    //绘制文字
    canvas.drawText(str2, 0, y, m_paint);
    //y坐标每次增加字符高度(即字体大小+行距)
    y = y + word_Size + Leading; 
    Now_LineNumber++;
    }
    }}
      

  3.   

    楼主你这些代码貌似画不出东西来,暂不说for循环的问题
      

  4.   

    我改了下你的代码
    public class ReadActivity extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            DisplayMetrics dm = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(dm);
            int screenWidth = dm.widthPixels;
            int screenHeight = dm.heightPixels;
            ReadView readView = new ReadView(this, screenWidth,  screenHeight);
            setContentView(readView);
        }
    }
    public class ReadView extends View { // 屏幕宽度
    int screenWidth;
    // 屏幕高度
    int screenHeight;
    // 文字大小
    int word_Size = 20;
    // 行距
    int Leading = 2;
    // 当前行数
    int Now_LineNumber = 1;
    // 文字
    String str = "jiyuhang 中hangm yujia =lljyhjyhjyjjjmmmqssssmmmmmmskkkjdjh,hdjj,jiyuhang 中hangm ";
    // 当前文字
    int k = 0;
    // 当前页数
    int now_page = 0;
    // 每页字数
    int a[] = new int[10];
    // 字符总长度
    int word_long = str.length();
    // 实例化绘制方法
    Paint m_paint; public ReadView(Context context, int width, int height) {
    super(context);
    // TODO Auto-generated constructor stub
    screenWidth = width;
    screenHeight = height;
    setFocusable(true);
    m_paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    } protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    // 每页行数
    int line_number = (screenHeight / word_Size) - 1; // 文字大小
    m_paint.setTextSize(word_Size);
    canvas.drawARGB(255, 255, 255, 255);

    // 字符绘制Y坐标
    int y = 0;
    Log.i("screenHeight", "" + screenHeight);
    Log.i("screenWidth", "" + screenWidth);
            
    while (this.Now_LineNumber < line_number) {
    Log.i(">>>>>>>>>>>>>>>>while<<<<<<<<<<<<<<<<", ">>>>>>>>>>>>>>>>>>>loop == true????");
    String str2 = "";
    if (this.k >= word_long || y >= screenHeight) {
    break;
    }
    for (;; k++) {
     Log.i("KKKKKKKKKK============",""+k);
    if (k < word_long) {
    str2 += str.substring(k, k + 1);
    } else {
    break;
    }
    float textWidth = m_paint.measureText(str2);
    if (textWidth >= screenWidth) {
    if (textWidth > screenWidth) {
    int beyond_long = str2.length();
    str2 = str2.substring(0, beyond_long - 1);
    break;
    }
    break;
    }
    }

    // y坐标每次增加字符高度(即字体大小+行距)
    y +=  word_Size + Leading;
    // 绘制文字
    canvas.drawText(str2, 0, y, m_paint);
    Now_LineNumber++;
    } }}下面是运行的Log,没有出现像楼主说的那样非条件下还循环的问题,Log为证05-28 00:38:03.761: INFO/screenHeight(729): 480
    05-28 00:38:03.761: INFO/screenWidth(729): 320
    05-28 00:38:03.770: INFO/>>>>>>>>>>>>>>>>while<<<<<<<<<<<<<<<<(729): >>>>>>>>>>>>>>>>>>>loop == true????
    05-28 00:38:03.770: INFO/KKKKKKKKKK============(729): 0
    05-28 00:38:03.770: INFO/KKKKKKKKKK============(729): 1
    05-28 00:38:03.780: INFO/KKKKKKKKKK============(729): 2
    05-28 00:38:03.780: INFO/KKKKKKKKKK============(729): 3
    05-28 00:38:03.780: INFO/KKKKKKKKKK============(729): 4
    05-28 00:38:03.780: INFO/KKKKKKKKKK============(729): 5
    05-28 00:38:03.780: INFO/KKKKKKKKKK============(729): 6
    05-28 00:38:03.780: INFO/KKKKKKKKKK============(729): 7
    05-28 00:38:03.801: INFO/KKKKKKKKKK============(729): 8
    05-28 00:38:03.801: INFO/KKKKKKKKKK============(729): 9
    05-28 00:38:03.850: INFO/KKKKKKKKKK============(729): 10
    05-28 00:38:03.850: INFO/KKKKKKKKKK============(729): 11
    05-28 00:38:03.861: INFO/KKKKKKKKKK============(729): 12
    05-28 00:38:03.861: INFO/KKKKKKKKKK============(729): 13
    05-28 00:38:03.880: INFO/KKKKKKKKKK============(729): 14
    05-28 00:38:03.880: INFO/KKKKKKKKKK============(729): 15
    05-28 00:38:03.880: INFO/KKKKKKKKKK============(729): 16
    05-28 00:38:03.891: INFO/KKKKKKKKKK============(729): 17
    05-28 00:38:03.901: INFO/KKKKKKKKKK============(729): 18
    05-28 00:38:03.901: INFO/KKKKKKKKKK============(729): 19
    05-28 00:38:03.910: INFO/KKKKKKKKKK============(729): 20
    05-28 00:38:03.920: INFO/KKKKKKKKKK============(729): 21
    05-28 00:38:03.920: INFO/KKKKKKKKKK============(729): 22
    05-28 00:38:03.931: INFO/KKKKKKKKKK============(729): 23
    05-28 00:38:03.931: INFO/KKKKKKKKKK============(729): 24
    05-28 00:38:03.941: INFO/KKKKKKKKKK============(729): 25
    05-28 00:38:03.941: INFO/KKKKKKKKKK============(729): 26
    05-28 00:38:03.951: INFO/KKKKKKKKKK============(729): 27
    05-28 00:38:03.951: INFO/KKKKKKKKKK============(729): 28
    05-28 00:38:03.960: INFO/KKKKKKKKKK============(729): 29
    05-28 00:38:03.960: INFO/KKKKKKKKKK============(729): 30
    05-28 00:38:03.971: INFO/KKKKKKKKKK============(729): 31
    05-28 00:38:03.971: INFO/KKKKKKKKKK============(729): 32
    05-28 00:38:03.971: INFO/KKKKKKKKKK============(729): 33
    05-28 00:38:03.981: INFO/KKKKKKKKKK============(729): 34
    05-28 00:38:03.981: INFO/KKKKKKKKKK============(729): 35
    05-28 00:38:04.001: INFO/>>>>>>>>>>>>>>>>while<<<<<<<<<<<<<<<<(729): >>>>>>>>>>>>>>>>>>>loop == true????
    05-28 00:38:04.001: INFO/KKKKKKKKKK============(729): 35
    05-28 00:38:04.020: INFO/KKKKKKKKKK============(729): 36
    05-28 00:38:04.020: INFO/KKKKKKKKKK============(729): 37
    05-28 00:38:04.031: INFO/KKKKKKKKKK============(729): 38
    05-28 00:38:04.031: INFO/KKKKKKKKKK============(729): 39
    05-28 00:38:04.031: INFO/KKKKKKKKKK============(729): 40
    05-28 00:38:04.041: INFO/KKKKKKKKKK============(729): 41
    05-28 00:38:04.051: INFO/KKKKKKKKKK============(729): 42
    05-28 00:38:04.051: INFO/KKKKKKKKKK============(729): 43
    05-28 00:38:04.060: INFO/KKKKKKKKKK============(729): 44
    05-28 00:38:04.060: INFO/KKKKKKKKKK============(729): 45
    05-28 00:38:04.071: INFO/KKKKKKKKKK============(729): 46
    05-28 00:38:04.071: INFO/KKKKKKKKKK============(729): 47
    05-28 00:38:04.080: INFO/KKKKKKKKKK============(729): 48
    05-28 00:38:04.080: INFO/KKKKKKKKKK============(729): 49
    05-28 00:38:04.091: INFO/KKKKKKKKKK============(729): 50
    05-28 00:38:04.091: INFO/KKKKKKKKKK============(729): 51
    05-28 00:38:04.091: INFO/KKKKKKKKKK============(729): 52
    05-28 00:38:04.101: INFO/KKKKKKKKKK============(729): 53
    05-28 00:38:04.101: INFO/KKKKKKKKKK============(729): 54
    05-28 00:38:04.131: INFO/KKKKKKKKKK============(729): 55
    05-28 00:38:04.131: INFO/KKKKKKKKKK============(729): 56
    05-28 00:38:04.131: INFO/KKKKKKKKKK============(729): 57
    05-28 00:38:04.131: INFO/KKKKKKKKKK============(729): 58
    05-28 00:38:04.161: INFO/KKKKKKKKKK============(729): 59
    05-28 00:38:04.161: INFO/KKKKKKKKKK============(729): 60
    05-28 00:38:04.170: INFO/KKKKKKKKKK============(729): 61
    05-28 00:38:04.180: INFO/>>>>>>>>>>>>>>>>while<<<<<<<<<<<<<<<<(729):>>>>>>>>>>>>>>>>>>>loop == true????
    05-28 00:38:04.180: INFO/KKKKKKKKKK============(729): 61
    05-28 00:38:04.191: INFO/KKKKKKKKKK============(729): 62
    05-28 00:38:04.201: INFO/KKKKKKKKKK============(729): 63
    05-28 00:38:04.201: INFO/KKKKKKKKKK============(729): 64
    05-28 00:38:04.211: INFO/KKKKKKKKKK============(729): 65
    05-28 00:38:04.221: INFO/KKKKKKKKKK============(729): 66
    05-28 00:38:04.221: INFO/KKKKKKKKKK============(729): 67
    05-28 00:38:04.231: INFO/KKKKKKKKKK============(729): 68
    05-28 00:38:04.231: INFO/KKKKKKKKKK============(729): 69
    05-28 00:38:04.241: INFO/KKKKKKKKKK============(729): 70
    05-28 00:38:04.241: INFO/KKKKKKKKKK============(729): 71
    05-28 00:38:04.261: INFO/KKKKKKKKKK============(729): 72
    05-28 00:38:04.261: INFO/KKKKKKKKKK============(729): 73
    05-28 00:38:04.261: INFO/KKKKKKKKKK============(729): 74
    05-28 00:38:04.270: INFO/KKKKKKKKKK============(729): 75
    05-28 00:38:04.270: INFO/KKKKKKKKKK============(729): 76
    05-28 00:38:04.270: INFO/KKKKKKKKKK============(729): 77
    05-28 00:38:04.281: INFO/KKKKKKKKKK============(729): 78
    05-28 00:38:04.291: INFO/KKKKKKKKKK============(729): 79
    05-28 00:38:04.291: INFO/KKKKKKKKKK============(729): 80
    05-28 00:38:04.301: INFO/>>>>>>>>>>>>>>>>while<<<<<<<<<<<<<<<<(729): >>>>>>>>>>>>>>>>>>>loop == true????
    到此就不循环了