2013-02-05 49 views
2

Targer - 需要算多少數與6位有左,右3位如何使用帶變量的方法?

什麼做的相同SUMM - 準備3種方法:

1日 - 計數SUMM從數字左3位 - 工程確定

private static int counterLeft3(int i) { 
    int digitCounter=0; 
    int summLeft3=0; 
    while (digitCounter!=3){ 
     summLeft3=summLeft3+(i%10); 
     i=i/10; 
     digitCounter++; 
    } 
    return summLeft3; 
} 

2擋 - 從數右3位的計數SUMM - 工程確定

private static int counterRight3(int i) { 
    int summRight3=0; 
    int buffer; 
    int counter=0; 
    while (counter!=3){ 
     summRight3=summRight3+(i%10); 
     i=i/10; 
     counter++; 
    } 
    buffer =i; 
    summRight3=0; 
    while (buffer!=0){ 
     summRight3=summRight3+(buffer%10); 
     buffer=buffer/10; 
    } 
    return summRight3; 
} 

3-DR - 週期數的計數Q-TY - 八方通返回0 thnink - 我的錯誤thomthing這裏:

private static void summCounter() { 
    int counter=0; 
    for (int i=111110; i<1000000; i++){ 
     if (counterRight3(i)==counterLeft3(i)){ 
     } 
     counter = counter++; 
    } 
    System.out.println("Q-ty is " + counter); 
} 

調試 - 例如

enter image description here

第一種方法的結果

enter image description here

結果2擋的方法

enter image description here

問題 - 有什麼錯3擋的方法,爲什麼永諾只返回0和計數器從不增加?

回答

2

在你方法分配: -

counter = counter++; 

counter價值沒有影響。在此作業之後,counter僅保留0。您需要刪除的任務的一部分,只是有部分increment -

counter++; 

而你需要做的if block內增量只有當你的左,右之和等於。

參見: -

+0

改變 - 它的工作=)...感謝 – gbk

+0

@Bki。不客氣:) –

+0

此代碼需要放在'if'塊中。你有'if(...){} counter ++;'。需要成爲'if(...){counter ++; }'。 – jlordo

相關問題