如何使用函数递增和递减值

时间:2013-11-21 09:23:42

标签: java

大家好我是Java新手。我创建了第一个程序。但我无法获得所需的输出..

我希望j值为2,然后我想增加值,直到循环结束。但我没有得到值

    private double i=30;
    private double j=20; 

    private double l=5;



if (l=5)
{

for (; j<=50  ; j+=2)
{
     System.out.printf("value of j is %d\n",j);
     increamenti_Value ();

}
for (; j>=4; j-=2) // i want here the value j to be 20 ... 

{
     System.out.printf("value of decrement is %d\n",j);
      increamenti_Value ();
}

}


private voide increamenti_Value ()

for (; i<=50  ; i+=2)
{
     System.out.printf("value of i is %d\n",i);


}
for (; i>=4; i-=2) // i want here the value j to be 20 ... 

{
     System.out.printf("value of decrement is %d\n",i);
}

2 个答案:

答案 0 :(得分:1)

请提供一个最小的工作示例。你的代码甚至没有编译(方法之外的代码,“voide”而不是void)。无论如何,我把一些适合你的东西放在一起:

package test;

public class Test
{
    static private int i=30;
    static private int  j=20; 
    static private int l=5;

    public static void main(String[] args)
    {
        if (l==5)
        {

            for (; j<=50  ; j+=2)
            {
                System.out.printf("value of j is %d\n",j);
                increamenti_Value ();
            }
            for (; j>=4; j-=2) // i want here the value j to be 20 ... 

            {
                System.out.printf("value of decrement is %d\n",j);
                increamenti_Value ();
            }
        }
    }

    private static void increamenti_Value ()
    {
        for (; i<=50  ; i+=2) {System.out.printf("value of i is %d\n",i);}  
    }

}

输出如下。那是你的期望吗?

value of j is 20
value of i is 30
value of i is 32
value of i is 34
value of i is 36
value of i is 38
value of i is 40
value of i is 42
value of i is 44
value of i is 46
value of i is 48
value of i is 50
value of j is 22
value of j is 24
value of j is 26
value of j is 28
value of j is 30
value of j is 32
value of j is 34
value of j is 36
value of j is 38
value of j is 40
value of j is 42
value of j is 44
value of j is 46
value of j is 48
value of j is 50
value of decrement is 52
value of decrement is 50
value of decrement is 48
value of decrement is 46
value of decrement is 44
value of decrement is 42
value of decrement is 40
value of decrement is 38
value of decrement is 36
value of decrement is 34
value of decrement is 32
value of decrement is 30
value of decrement is 28
value of decrement is 26
value of decrement is 24
value of decrement is 22
value of decrement is 20
value of decrement is 18
value of decrement is 16
value of decrement is 14
value of decrement is 12
value of decrement is 10
value of decrement is 8
value of decrement is 6
value of decrement is 4

答案 1 :(得分:0)

使用j和i作为静态int变量,如static int i