2014-10-28 53 views
-1

你能告訴我怎樣才能修復這個程序,使「sumSecTolastD」操作符執行以下操作:
1-將第二個數字與一個整數中的最後一位數字相加(例如:324 + 564 + 9876 = (2 + 4)+(6 + 4)+(8 + 7 + 6)= 29)。
2-如果我只插入一個數字,它仍然能夠給我總和(例如:1 + 3 + 4 = 8)。如何將第二位數字加到java中整數的最後一位數字上?

import java.util.*; 
 
public class Pr66{ 
 
     public static void main(String[] args){ 
 
     Scanner scan = new Scanner (System.in); 
 
     int num1; 
 
     int num2; 
 
     int num3; 
 
     int sumLastD; 
 
     int sumSecTolastD; 
 

 
     System.out.print("Please write an integer: "); 
 
     num1 = scan.nextInt(); 
 

 
     System.out.print("Please write an integer: "); 
 
     num2 = scan.nextInt(); 
 

 
     System.out.print("Please write an integer: "); 
 
     num3 = scan.nextInt(); 
 

 
     sumLastD = num1 % 10 + num2 % 10 + num3 % 10; 
 
     System.out.println(); 
 
     System.out.println("- LastDigitSum: " + num1 % 10 + " + " + num2 % 10 + " + " + num3 % 10 + " = " + sumLastD); 
 
     
 
     if (sumLastD % 2 == 0) 
 
     System.out.println("- LastDigitSum: is an even integer"); 
 
     else 
 
     System.out.println("- LastDigitSum: is an odd integer"); 
 

 
     System.out.println(); 
 

 
     sumSecTolastD = (num1/10) % 10 + (num2/10) % 10 + (num3/10) % 10; 
 
     System.out.println("- SecToLastSum: " + (num1/10) % 10 + " + " + (num2/10) % 10 + " + " + (num3/10) % 10 + " = " + sumSecTolastD); 
 
       
 
     if (sumSecTolastD % 2 == 0) 
 
     System.out.println("- SecToLastSum: is an even integer"); 
 
     else 
 
     \t System.out.println("- SecToLastSum: is an odd integer"); 
 
     
 
     }//main 
 
}//Pr66

+0

的可能重複[總結第二到最後關於java](http://stackoverflow.com/questions/26599609/sum-the-second-to-在-最後上的Java) – 2014-10-28 05:31:25

回答

0

抱歉不便。我的問題被誤解了。我的意思是我想編寫一個代碼來查找三個不同整數的第二位數字和最後一位數字的總和。例如:如果用戶輸入15,34和941,在這種情況下,第二位到最後一位將是1,3和4.因此,它們的小計將是1 + 3 + 4 = 8.

我找到了答案,我想與大家分享,並且我想感謝所有嘗試提供幫助的人。謝謝你..

import java.util.*; 
 
public class Pr6{ 
 
    public static void main(String[] args){ 
 
    Scanner scan = new Scanner (System.in); 
 
    int num1; 
 
    int num2; 
 
    int num3; 
 
    int sumSecToLast; 
 
    
 
    System.out.print("Please write an integer: "); 
 
    num1 = scan.nextInt(); 
 
    System.out.print("Please write an integer: "); 
 
    num2 = scan.nextInt(); 
 
    System.out.print("Please write an integer: "); 
 
    num3 = scan.nextInt(); 
 
    
 
    sumSecToLast = ((num1/10) % 10) + ((num2/10) % 10) + ((num3/10) % 10); 
 
    System.out.println("The subtotal of the 2nd to the last digit = " + sumSecToLast); 
 
    
 
    if (sumSecToLast % 2 == 0) 
 
     System.out.println(sumSecToLast + " is an even number."); 
 
    else 
 
     System.out.println(sumSecToLast + " is an odd number."); 
 
    
 
    }//main 
 
}//Pr6

0

此總結只有倒數第二位:

sumSecTolastD = (num1/10) % 10 + (num2/10) % 10 + (num3/10) % 10; 

爲了資金都倒數第二個和最後一個數字,將其更改爲:

sumSecTolastD = num1%10 + (num1/10) % 10 + num2%10 + (num2/10) % 10 + num3%10 + (num3/10) % 10; 

即使任何/所有的輸入數字都有歌曲,相同的代碼也可以工作因爲在那種情況下,(numi/10) % 10將是0,並且不會影響總和。

0

這總結過去的數字:

sumLastD = num1 % 10 + num2 % 10 + num3 % 10; 

得到最後和第二的總和持續,上述的結果添加到您的計算倒數第二位:

sumSecTolastD = (num1/10) % 10 + (num2/10) % 10 + (num3/10) % 10 + sumLastD; 

,使其與只有個位數的工作,以及將需要額外的檢查,並讓你的代碼要大得多:

sumLastD = (String.valueOf(num1).length() > 1 ? num1 % 10 : num1) + 
(String.valueOf(num2).length() > 1 ? num2 % 10 : num2) + 
(String.valueOf(num3).length() > 1 ? num3 % 10 : num3); 

是的,你可以把它放在多行上,編譯器會忽略不必要的空格。

對於sumSecTolastD修改上面到:

sumSecTolastD = (String.valueOf(num1).length() > 2 ? (num1/10) % 10 : num1) + 
(String.valueOf(num2).length() > 2 ? (num2/10) % 10 : num2) + 
(String.valueOf(num3).length() > 2 ? (num3/10) % 10 : num3) + sumLastD; 
相關問題