栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java学习第四天----习题练习

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java学习第四天----习题练习

今日进行了习题练习

1、输出三个数中的最大值和最小值

import java.util.Scanner;

public class Homework6 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入第一个数:");
		int num1 =sc.nextInt();
		System.out.println("请输入第二个数");
		int num2 = sc.nextInt();
		System.out.println("请输入第三个数");
		int num3 = sc.nextInt();
//		int max1=(num1 > num2 ? num1 : num2);
//		int max2=(max1 > num3 ? max1 : num3);
//		int min1=(num1 < num2 ? num1 : num2);
//		int min2=(min1 < num3 ? min1 : num3);
//		System.out.println("(" +num1+ "," +num2+ "," +num3+ ")的最大值为" +max2+ ",最小值//      为" +min2);
		
		//Math数学库
	    int max=Math.max(num1, num2);
	    max=Math.max(max, num3);
	    
	    int min=Math.min(num1, num2);
	    min=Math.min(min, num3);
	    
	    System.out.println("最大值为:"+max+",最小值为"+min);
	  }
}

2、输出1-100的奇数(每行输出6个)

public class Homework7 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int a = 0;
		for (int i = 1; i <= 100; i++) {
			if (i%2 == 1) {
				a++;
				System.out.print(i+"t");
			}
			if (a%6 == 0) {
				System.out.print("n");
			}			
		}
	}
}

3、1-100求和(for while以及do/while的写法)

public class Homework8 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int res = 0;
		for(int i = 1;i <101;i++) {
			res +=i;
		}
		System.out.println("1+2+3+...+100=" +res);
	}

}
public class Homework81 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int a = 1;
		int b = 0;
		while(a < 101) {
			b += a++;
		}
		System.out.println("1+2+3+...+100=" +b);
	}
}
public class Homework82 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int a = 1;
		int b = 0;
		do {
			b += a++;
		}while(a < 101);
		System.out.println("1+2+3+...+100=" +b);
	}
}

4、1-100奇数求和

public class Homework9 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int res = 0;
		for(int i = 1;i <101;i++) {
			if(i%2 != 0) {
				res +=i;
			}			
		}
		System.out.println("1+3+5+...+99=" +res);
	}

}

5、1~100可以被3整除的数

public class Homework10 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		for(int con = 1;con <= 100;con++) {
			if(con%3 == 0) {
				System.out.println(con);
			}
		}
	}

}

6、求100 以内所有能被3 整除但不能被5 整除的个数

public class Homework11 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int i = 0;
		for(int con = 1;con <= 100;con++) {
			if(con%3 == 0 && con%5 != 0) {
				i++;
			}
		} 
			System.out.println("100以内所有能被3整除但不能被5整除的个数为" +i+ "个");
	}
}

7、打印出所有的水仙花数

public class Shuixian {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		for(int n = 100;n < 1000;n++) {
		       int a=n%10;   //个位
		       int b=n/10%10;//十位
		       int c=n/100;  //百位
		       if(Math.pow(a, 3)+Math.pow(b, 3)+Math.pow(c, 3) == n) {
		          System.out.println(n);
		       }
		}		       
	}

}
public class Test18 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int n = 100;
		do {
			int a=n%10;   //个位
		    int b=n/10%10;//十位
		    int c=n/100;  //百位
		    if(Math.pow(a, 3)+Math.pow(b, 3)+Math.pow(c, 3) == n) {
		          System.out.println(n);
		    }
		}while(n++ < 999);

	}
}

8、判断一个数是否质数

import java.util.Scanner;

public class Zhishu {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入一个大于1的整数");
		int n = sc.nextInt();
		boolean sushu  = true;
		if(n == 1 || n % 2 == 0 && n != 2) {
			sushu  =  false;
		}
		else {
			for(int i = 2; i < n; i++ ) {
				if(n % i == 0) {
					sushu = false;
					break;
				}
			}
		}
		
		if(sushu) {
			System.out.println("是素数");
		}
		else {
			System.out.println("不是素数");
		}
	}
}

9、编程求出自然数101-205中的所有质数

public class Test19 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		  int cc=0;
		    for (int i = 101; i <= 205; i++) {
		      boolean bb=true;
		      for(int k=2;k 

12、输入两个正整数m和n,求其最大公约数和最小公倍数

import java.util.Scanner;

public class Test1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int k1 = 0;
		System.out.println("请输入第一个正整数:");
		while(true) {
			k1 = sc.nextInt();
			if(k1 <=0) {
				System.out.println("输入数据不合法,请重新输入!");
			}else {
				break;
			}
		}
		int k2 = 0;
		System.out.println("请输入第二个正整数:");
		while(true) {
			k2 = sc.nextInt();
			if(k2 <=0) {
				System.out.println("输入数据不合法,请重新输入!");
			}else {
				break;
			}
		}
		int yueshu = -1;
		int min = Math.min(k1, k2);
		for(int k = min;k > 1;k--) {
			if(k1%k == 0 && k2%k == 0) {
				yueshu = k;
			break;
			}			
		}
		int beishu = -1;
		int max = Math.max(k1, k2);
		for(int k = max;k < k1*k2;k++) {
			if(k%k1 == 0 && k%k2 == 0) {
				beishu = k;
				break;
			}
		}
		if(yueshu==-1)
		      System.out.println(k1+"和"+k2+"互质");
		    else
		      System.out.println(k1+"和"+k2+"的最大公约数"+yueshu);
		    if(beishu!=-1)
		      System.out.println(k1+"和"+k2+"的最小公倍数"+beishu);
	}

}
import java.util.Scanner;

public class Homework12 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		System.out.println("输入第一个正整数");
		int m = sc.nextInt();
		System.out.println("输入第二个正整数");
		int n = sc.nextInt();
        int c = 1;
        for (int i = 2; i <= Math.min(m, n); i++) {
            if (m % i == 0 && n % i == 0) {
                c *= i;
                m = m / i;
                n = n / i;
                i--;
            }
        }
        System.out.println("最大公约数:" + c);
        System.out.println("最小公倍数:" + (c * m * n));
	}

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1008112.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号