/写一个函数 add,接受两个整数作为参数,
返回这两个整数的和;在 main 函数中调用并打印
结果。/
import java.util.Scanner;
class Test2{
public static void main(String[] args){
Scanner m = new Scanner(System.in); //调用输入方法
Scanner n = new Scanner(System.in);
System.out.println("请输入两个整数,计算和");
int a = m.nextInt(); //输入值进行赋值
int b = n.nextInt();
System.out.println(he(a,b)); //调用求和函数并打印
}
//求和函数
public static int he(int a,int b){ //求和函数名叫 he
int c = 0;
c = a+b;
return c; //返回c
}
}
/写一个函数,
接受一个整数 n,输出这个整数的所有因子;
并在 main 函数中调用此函数/
import java.util.Scanner;
class Test3{
public static void main(String[] args){
Scanner s =new Scanner(System.in);
System.out.println("请输入一个整数");
int m = s.nextInt();
//等待调用函数
yin(m);
System.out.println();
}
//求因子函数
public static void yin(int a){
for(int n=1;n<=a;n++){
if(a%n==0)
System.out.println(n);
}
}
}
/*. 编程:写一个函数,接受一个整数,输出这个整数是几位数。 */
import java.util.Scanner;
class Test4{
public static void main(String[] args){
Scanner s = new Scanner(System.in);
System.out.println("请输入一个整数");
int m = s.nextInt();
System.out.println("这个数是"+ count(m) +"位数。");
}
public static int count(int a){
int cc=1;
for(int i=0;i<=10;i++){
if(a/10!=0){
a=a/10;
cc++;
}
}
return cc;
}
}
/.编程:求一个三位数,该三位数等与其每位数字的阶乘之和。/
import java.util.Scanner;
class Test5{
public static void main(String[] args){
for(int n=100;n<1000;n++){
int a=0,b=0,c=0;
a = n/100;
b = (n/10)%10;
c = n%10;
if(jieCheng(a)+jieCheng(b)+jieCheng(c)==n){
System.out.println(n);
}
}
}
public static int jieCheng(int i){
int n=1;
for(int m=1;m<=i;m++){
n *= m;
}
return n;
}
}
/编程:如果整数 A 的全部因子(包括 1,不包括 A 本身)之和等于 B,且整数 B 的全部因子包
括 1,不包括 B 本身)之和等于 A,则称整数 A\B 是一对亲密数;求 3000 以内的全部亲密数。/
import java.util.Scanner;
class Test6{
public static void main(String[] args){
int m=0;
for(int i = 1;i<=3000;i++){
m=yin(i);
if(iyin(m)&&i!=m){
System.out.println(i+" "+m);
}
}
}
//求因子函数
public static int yin(int a){
int count=0;
for(int n=1;n<a;n++){
if(a%n0)
count += n;
}
return count;
}
}
/13. 验证哥德巴赫猜想:任何一个大于 6 的偶数,
都能分解成两个质数的和。
这个数能被分解成哪两个质数的和。
例如:输入一个整数 14
输出结果:
14=3+11
14=7+7/
import java.util.Scanner;
class Test7{
public static void main(String[] args){
Scanner s =new Scanner(System.in);
System.out.println("请输入一个大于6的偶数。");
int m = s.nextInt();
for(int i=2;i<=m/2;i++){
int a = i;
int b = m-i;
if(panDuan(a)&&panDuan(b)){
System.out.println(m+"="+a+"+"+b);
}
}
}
//判断函数
public static boolean panDuan(int a){
for(int i=2;i<=a-1;i++){
if(a%i ==0)
return false;
}
return true;
}
}