2014-10-28 27 views
0

我怎樣才能打印每個整數的第二位到最後一位數字的總和?總結第二個到最後一個在java

(因此,8將被印刷,因爲1 + 3 + 4爲8個,和35將被印刷,因爲3453 + 65324 + 354)中的以下程序:*不使用if語句*

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.print((num1/10) % 10 + " + " + (num2/10) % 10 + " + " + (num3/10) % 10 + " = " + sumSecToLast); 


     }//main 
}//Pr6 
+1

* 「35將是自3453 + 65324 + 354印有」 * - 爲什麼?還有什麼是你的代碼工作? – 2014-10-28 01:45:28

+0

導致3453的第2位爲4,第2位到第4位的總和爲4 + 5 + 3 = 12。因此,如果你以相同的方式將其餘整數相加,它將是35. – Bader 2014-10-28 01:58:10

+0

(可能是錯誤的),但我認爲當他的意思是(3453 + 65324 + 354)=(4 + 5 + 3)+(5+ 3 + 2 + 4)+(5 + 4)= 35 – StephenButtolph 2014-10-28 01:58:32

回答

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); 
 
    System.out.println(); 
 
    
 
    }//main 
 
}//Pr6

0

一旦你掃描所有的整數:

//In main method: 
int secLast1 = Pr6.getSecLastDigit(num1); 
int secLast2 = Pr6.getSecLastDigit(num2); 
int secLast3 = Pr6.getSecLastDigit(num3); 

int sum = secLast1 + secLast2 + secLast3; 
System.out.println(secLast1 + " + " + secLast2 + " + " + secLast3 + " = " + sum); 

您還需要創建額外的方法:

private static int getSecLastDigit(int num) { 
    return (num/10) % 10; 
} 
0

這是我該怎麼做的。根據您對if聲明的定義,這可能不適用於您(擾流板)。

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

     System.out.print("Please write an integer: "); 
     num1 = scan.nextLine(); // rather than taking an integer this takes a String because it is easier to extract a single element. 

     ... // get the other numbers 

     for (int i = 1; i < num1.length(); i++){ 
      total += Character.getNumericValue(num1.charAt(i)); // adds each number to the total 
     } 

     ... // do this for the other Strings (or use another loop for with a String[]) 

     System.out.println(total); 
    }//main 
}//Pr6 

爲了使這更簡潔我會強烈建議使用String[]而不是3個不同的變量。此外,我假設for循環不算作if語句。然而我意識到,由於boolean檢查他們可能被認爲太相似,因爲你目前的情況。我希望這有幫助! :)

相關問題