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

JAVA基础语法练习

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

JAVA基础语法练习

1、输出三个数中的最大值和最小值
public class task8 {
   public static void main(String[] args) {
	int k1=65;
	int k2=6;
	int k3=15;
	int max=Math.max(k1, k2);
	max=Math.max(max, k3);
	int min=Math.min(k1, k2);
	min=Math.min(k3, min);
	System.out.println(max+","+min);
}
}

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

public class test1 {
public static void main(String[] args) {
	int c=0;
	for (int i=1;i<101;i++) {
		if(i%2!=0) {
			System.out.print(i+"t");
	        c++;
	        if(c%6==0) {
	        System.out.println();
	        }
		}
	}
}
}

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

public class test5 {
public static void main(String[] args) {
	int c=0;
	//for (int i=1;i<=100;i++)
		//if (i%3==0&&i%5!=0) {
			//System.out.print(i+"t");
	       // c++;
	       // if (c%6==0) {
	        //	System.out.println();
	      // }
		//}
	int i=1;
	//while (i++<100)
		//if (i%3==0&&i%5!=0) {
			//System.out.print(i+"t");
			//c++;
			//if(c%6==0)
			//System.out.println();
		//}
	do 
		if (i%3==0&&i%5!=0) {
			System.out.print(i+"t");
		    c++;
		    if (c%6==0)
		    	System.out.println();
		}
	while (i++<100);
}
}

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

public class test6 {
public static void main(String[] args) {
	int a,b,c;
	for循环:
     //int k=0;
	   //for (int n=100;n<1000;n++) {
		//a=n/100;
		//b=n%100/10;
		//c=n%10;
		//if (a*a*a+b*b*b+c*c*c==n) {
			//System.out.print(n+"t");
		    //k++;
		   // if (k%2==0)
		    	//System.out.println();
		//}
	//}
	while循环 :
    //while (n++<999) {
		//a=n/100;
		//b=n%100/10;
		//c=n%10;
		//if (a*a*a+b*b*b+c*c*c==n) {
			//System.out.print(n+"t");
		//}
	//}
	
	int n=100;
do while循环:
	 do {
		a=n/100;
        b=n%100/10;
        c=n%10;
	    if (a*a*a+b*b*b+c*c*c==n) {
	    	System.out.print(n+"t");
	    }
	}
	while (n++<999);
}
}

5、判断一个数是否质数

import java.util.Scanner;

public class test7 {
public static void main(String[] args) {
	int n;
 Scanner sc=new Scanner(System.in);
 System.out.println("请输入一个大于1的整数");
 while(true) {
	 n=sc.nextInt();
     if(n<=1)
    	 System.out.println("输入错误");
     else
    	 break;
 }
     boolean a=true;
     for (int i=2;i 

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

public class test9 {
	public static void main(String[] args) {
		int c=0;
		for (int n=101;n<=205;n++) {
		   for (int i=2;i<=n;i++) {
			  if (n%i==0&&i 

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

import java.util.Scanner;

public class test10 {
public static void main(String[] args) {
	Scanner sc=new Scanner(System.in);
	System.out.println("请输入整数m:");
	int m=sc.nextInt();
	while (true) {
		if (m<=0) {
			System.out.println("输入错误,请重新输入");
		}
		else break;
	}
	System.out.println("请输入整数n:");
	int n=sc.nextInt();
	while (true) {
		if (n<=0) {
			System.out.println("输入错误,请重新输入");
		}
		else break;
	}
	int y=-1;
	int min=Math.min(m, n);
	for (int k=min;k>1;k--) {
		if (m%k==0&&n%k==0) {
			y=k;}
		break;
	}
	int b=-1;
	int max=Math.max(m, n);
		for (int k=max;k 

8.100 ~ 50000之间有多少整数,其各位数字之和为5,分别是哪些数(例如整数1324的各位数字之和为 1+3+2+4 等于10(不为5)),并统计满足条件的整数有多少个

public class test11 {
public static void main(String[] args) {
	int w,q,b,s,g;
	int c=0;
	for (int i=100;i<=50000;i++) {
		if(i>=100&&i<1000) {
			b=i/100;
			s=i%100/10;
			g=i%10;
			while (b+s+g==5) {
				System.out.print(i+"t");
				c++;
				 if (c%6==0) {
					 System.out.println();}
				break;
			}
		}
		 if(i>=1000&&i<10000) {
			q=i/1000;
			b=i%1000/100;
			s=i%100/10;
			g=i%10;
			while (q+b+s+g==5) {
				System.out.print(i+"t");
				c++;
				 if (c%6==0) {
					 System.out.println();}
			break;
		}
			
		 }
	   if(i>=10000&&i<=50000) {
		   w=i/10000;
		   q=i%10000/1000;
		   b=i%1000/100;
		   s=i%100/10;
		   g=i%10;
		   while (w+q+b+s+g==5) {
			   System.out.print(i+"t");
			   c++;
				 if (c%6==0) {
					 System.out.println();}
		   break;
		   }
	   }
	          
		 }
	    
}
}

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

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

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