用while语句求1~100之和 public class Ex3_5 { public static void main(String[] args){ int n=1,sum=0; while(n<=100) { sum+=n; n++; } System.out.println("sum="+sum); }}…
经典问题了,三个变量分别表示起始.结尾以及和,for循环从起始到结尾,和不断累积.代码如下: public class ForLoop { public static void main(String[] args){ int limit=100; int sum=0; for(int i=1;i<=limit;i++) { sum=sum+i; } System.out.println("sum="+sum); } } 效果如图:…
/* * 用java求一个整数各位数字之和 */ public class Test02 { public static void main(String[] args) { System.out.println(Test02.sumDig(23865)); System.out.println(Test02.sumDig2(23965)); } public static int sumDig(int n) { int sum = 0; if (n >= 10) { sum += n % 10…
29 [程序 29 求矩阵对角线之和] 题目:求一个 3*3 矩阵对角线元素之和 程序分析:利用双重 for 循环控制输入二维数组,再将 a[i][i]累加后输出. package cskaoyan; public class cskaoyan29 { @org.junit.Test public void diagonal() { java.util.Scanner in = new java.util.Scanner(System.in); int[][] arr = new int[3][…
package com.swift; public class String_To_Integer_Test { public static void main(String[] args) { /* * 编程求字符串“100”和“150”按十进制数值做差后的结果以字符串形式输出. */ String str1="100"; String str2="150"; int i1=Integer.valueOf(str1); int i2=Integer.valueOf…
Java求素数时出现错误 1.具体错误如下 No enclosing instance of type Prime is accessible. Must qualify the allocation with an enclosing instance of type Prime (e.g. x.new A() where x is an instance of Prime). 2.错误原因 class PrimNumber { public boolean isPrim(int a) { f…