2014-05-16 115 views
0

这是我的代码:Java循环执行错误

import java.io.*; 
import java.util.*; 
class comp 
{ 
int a,b,i,k,l; 
comp() 
{ 

    Scanner s=new Scanner(System.in); 
    System.out.println("Enter the number"); 
    a=s.nextInt(); 
    b=s.nextInt(); 
    divide(a,b); 

} 
public void display(int[] b) 
{ 
    for(i=0;i<b.length;i++) 
     System.out.println(b[i]); 
} 
public void divide(int e,int f) 
{ 
    int[] st=new int[20]; 
    //System.out.println(e); 
    i=0; 

    if(e>0) 
    { 
     l=e%10; 
     e=e/10; 
     st[i]=l; 
     i++; 

    } 
    this.display(st); 

    //return st; 

} 



} 
class km extends comp 
{ 
public static void main(String args[]) 
{ 
    km m=new km();  



} 
} 

的,如果在分频功能回路只得到once.It应完全执行到的number.I的位数我只是想执行将整数转换为数组。

+0

调试器是你的朋友。 –

+0

'divide'中的'if'块(似乎没有正确命名)只会提取'e'的最后一个数字。 – rgettman

+1

“_除法函数中的for循环_”除法函数中没有for循环。 – csmckelvey

回答

1

它不重复,因为你有一个if(e> 0)。

也许它应该是一段时间(e> 0)?

1

如果只评估一次,然后继续执行其余代码。同时将继续处理代码,直到条件为假。

将您的if更改为while

while(e>0) 
{ 
    l=e%10; 
    e=e/10; 
    st[i]=l; 
    i++; 
} 

但是注意,当前的代码会显示您的整数向后