4.6 在键盘上输入数n,利用循环编程计算sum=(请看图片)
4.7 水仙花数是指其个位、十位和百位三个数字的立方和等于其自身的三位数,求出所有的水仙花数。
4.8 从键盘输入一个整数,判断该数是否是完全数。完全数是指其所有因数(包括 1但不包括其自身)的和等于该数自身的数。例如 28=1+2+4+7+14 就是一个完全数。
4.9 计算并输出一个整数各位数字之和。如 5423 的各位数字之和为 5+4+2+3=14.
Java程序设计基础课后习题 4.6至4.9
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- 醉酒的戈多 2023-10-10 14:06关注
public static void main(String[] args) throws InterruptedException { topic46(); topic47(); topic48(); Thread.sleep(500); topic49(); } public static void topic49() { Scanner scanner = new Scanner(System.in); System.out.print("4.9、请输入一个整数:"); int number = scanner.nextInt(); if (number <= 0) { System.out.println("输入的数必须是正整数!"); return; } String str = number+""; int sumnumber = 0; for(int i=0; i<str.length(); i++){ String substr = str.substring(i, i+1); sumnumber += Integer.parseInt(substr); } System.err.println(number+"各位数字之和为:"+sumnumber); } public static void topic48() { Scanner scanner = new Scanner(System.in); System.out.print("4.8、请输入一个整数:"); int number = scanner.nextInt(); if (number <= 0) { System.out.println("输入的数必须是正整数!"); return; } int sum = 0; for (int i = 1; i < number; i++) { if (number % i == 0) { sum += i; } } if(sum == number){ System.err.println(number+"是完全数"); }else{ System.err.println(number+"不是完全数"); } } public static void topic47(){ for (int i = 100; i <1000 ; i++) { //求出百位数 int a = i/100; //求出十位数 int b = i%100/10; //求出个位数 int c = i%10; if(a*a*a + b*b*b + c*c*c == i){ System.out.println(i); } } } public static void topic46(){ Scanner scanner = new Scanner(System.in); System.out.print("4.6、请输入一个整数:"); int n = scanner.nextInt(); double value = 1; for(int i = 2;i <= n;i++){ value += Math.pow(-1,i-1)*1/factorial(i); } System.out.println(n+"的计算结果为:"+value); } public static int factorial(int n){ int factorial = 1; for(int i = 1;i <= n;i++){ factorial *= i; } return factorial; }
运行结果如下:
解决 无用评论 打赏 举报
悬赏问题
- ¥15 SPSS分类模型实训题步骤
- ¥15 求解决扩散模型代码问题
- ¥15 工创大赛太阳能电动车项目零基础要学什么
- ¥20 limma多组间分析最终p值只有一个
- ¥15 nopCommerce开发问题
- ¥15 torch.multiprocessing.spawn.ProcessExitedException: process 1 terminated with signal SIGKILL
- ¥15 QuartusⅡ15.0编译项目后,output_files中的.jdi、.sld、.sof不更新怎么解决
- ¥15 pycharm输出和导师的一样,但是标红
- ¥15 想问问富文本拿到的html怎么转成docx的
- ¥15 我看了您的文章,遇到了个问题。