import java.util.Scanner; public class NKTest { public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.print("Enter a number between 0 and 1000:"); int number = s.nextInt(); s.close(); int total = 0; if (number > 0 && number < 1000) { int nuits = number % 10;//个位 int tens = number / 10 % 10;//十位,当number小于10时,tens=0不影响结果。 int hundreds = number / 100;//百位,当number小于100时,hundreds=0不影响结果。 if (hundreds + tens + nuits == 14) { total++; } }else{ System.out.println("数值不合法"); } System.out.println(total); } }
import java.util.Scanner ; public class CalcuTest { public static void main(String args[]) { System .out .println("Enter a number between 0 and 1000:") ; Scanner scanner = new Scanner(System.in); int num = scanner.nextInt(); while ((num < 0) ||( num > 1000)) { System.out.println("请输入0-1000范围的整数:"); num = scanner.nextInt(); }//判断输入数字是否合乎要求 int sum=0; int a=num%10;//获得个位数字 int b=num/10; if(b>9&&b<=99){//num为三位数 int c=b/10;//获得百位数字 int d=b%10;//获得十位数字 sum=a+c+d; } else if (b<=9&&b>=0){//num为一位或两位数 sum=a+b; } else sum=1;//num=1000 System .out .println("The sum of the digits is"+sum) ; } }