Java学习
第六天:
写了一下网课上的代码
1)求素数
public class SuNum {
public static void main(String[] args) { if(getResult(4)){ System.out.println("素数"); }else{ System.out.println("非素数"); } } public static boolean getResult(int num) { boolean isPrime = true; // 定义布尔变量判断是否素数.是:true;否:false int k = (int) Math.sqrt(num); for (int j = 2; j <= k; j++) { if (num % j == 0) { isPrime = false;
} } if (isPrime) { isPrime = true; } return isPrime; }}求最大公约数
1.public static int consecutiveIntegersTest(int m, int n) {
int t;
if (m > n)
t = n;
else
t = m;
while(true) {
if (m%t == 0 && n%t == 0)
break;
else
t = t - 1;
}
return t;
}
2.
public static int consecutiveIntegersTest(int m, int n) {
int t;
if (m > n)
t = n;
else
t = m;
while(true) {
if (m%t == 0 && n%t == 0)
break;
else
t = t - 1;
}
return t;
}
3.求和
public class CommandParameter { //CommandParameter类
public static void main(String args[]){ //main方法
int sum=0;
System.out.println("参数个数:"+args.length);
for(String arg:args){
System.out.println(arg);
for(int i=0;i<args.length;i++)
{
sum=sum+Integer.parseInt(args[i]);
}
System.out.println(sum);
}
}
}
浙公网安备 33010602011771号