java基础
1、
class xiao { public static void main(String[] args) { System.out.println("Hello World!"); } }
注意:编译之后不要加后缀名class
2、
1 2 3 4 5 6 7 8 9 10 | class xiao { public static void main(String[] args) { byte b1=4; byte b2=8; byte b3=( byte )(b1 + b2) ; System. out .println(b3); } } |
3、
4、
1 2 3 4 5 6 7 8 9 10 11 12 | class Xiao { public static void main(String[] args) { int a=10; int b=11; System. out .println(a/b); System. out .println(b/a); System. out .println(a%b); System. out .println(b%a); } } |
5、
1 2 3 4 5 6 7 8 9 10 11 | class Xiao { public static void main(String[] args) { int a=10; int b=11; int c=++a; int d=b++; System. out .println(c+ "," +d+ ";" +a+ "," +b); } } |
++a a=a+1,c=a 先自加,再赋值给c;b++ 先赋值给d,再自加
6、
1&1=1
0|0=0
1 2 3 4 5 6 7 8 9 10 11 12 13 | class Xiao { public static void main(String[] args) { int a=10; int b=11; if ((a++>10) && (b-->10)) //当第一个条件能够确定这个表达式值的时候,不会再去判断第二个条件 { //a++为10 a为11 ,b--为10,b为11 System. out .println(a+ "," +b); } System. out .println(a+ "," +b); } } |
当将条件改为:f ((a++>10) & (b-->10)):少一个&时
7、条件表达式if
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | class Xiao { public static void main(String[] args) { String y; int x=88; if (x>=90) { y= "A" ; } else if (x>=80){ y= "B" ; } else if (x>=70){ y= "C" ; } else { y= "False" ; } System. out .println(y); } } |
8、switch
switch(n)
{
case 1:
执行代码块 1
break;
case 2:
执行代码块 2
break;
default:
n 与 case 1 和 case 2 不同时执行的代码
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | class Xiao { public static void main(String[] args) { switch (2) { case 1: System. out .println(1); break ; case 2: System. out .println(2); break ; default : System. out .println( "default" ); } } } |
工作原理:首先设置表达式 n(通常是一个变量)。随后表达式的值会与结构中的每个 case 的值做比较。如果存在匹配,则与该 case 关联的代码块会被执行。请使用 break 来阻止代码自动地向下一个 case 运行。
9、判断是否是质数(设置isPrinme旗帜,有一个满足条件的值就将重新设置isPrime的值,并对他进行判断输出不同结果)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | class Xiao { public static void main(String[] args) { int num = 123; boolean isPrime = true ; for ( int i=2;i<num ;i++ ) { if (num%i==0){ isPrime = false ; break ; } if (isPrime) { System. out .println(num+ "是质数" ); } else { System. out .println(num+ "不是质数" ); } } } } |
10、while循环
1 2 3 4 5 6 7 8 9 10 11 12 | class mcw { public static void main(String[] args) { int i = 0; while (i<100) { System. out .println(i); i++; } } } |
11、for 循环
1 2 3 4 5 6 7 8 9 10 11 | class mcw { public static void main(String[] args) { for ( int i=0;i<100 ;i++ ) { System. out .println(i); } } } |
12、0-100之间的偶数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | class mcw { public static void main(String[] args) { int j = 0; while (j<100) { if (j%2==0) { System. out .println(j); } j++; //while 循环中不要忘了写自加,不然死循环了 } } } |
13、打印1-100间除了能被10整除的所有数字:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | class mcw { public static void main(String[] args) { for ( int i=1;i<100 ;i++ ) { if (i%10==0) { continue ; //跳过满足的,break跳出循环。 } System. out .println(i); } } } |
14、输出1--100之间前五个偶数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | class mcw { public static void main(String[] args) { int num = 0; for ( int i=0;i<=100 ;i++ ) { if (num>=5) { break ; } if (i%2==0) { num++; //就像计数器,计数前五个 System. out .println(i); } } } } |
15、
16、
函数封装,黑匣子,只需要知道怎么调用就好,
求长方形面积和周长:
一个类三个函数:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | class mcw //类名 { public static void main(String[] args){ int w = 50; int h = 30; int p = calPerimeter(w,h); int a = calArea(w,h); } public static int calArea( int width, int height){ //计算面积 return width*height; } public static int calPerimeter( int width, int height){ //计算周长 return 2*(width+height); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | class mcw { public static void main(String[] args){ int w = 50; int h = 30; int p = calPerimeter(w,h); int a = calArea(w,h); System. out .println( "周长" +p); System. out .println( "面积" +a); } public static int calArea( int width, int height){ return width*height; } public static int calPerimeter( int width, int height){ return 2*(width+height); } } |
17、求圆面积和周长
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | class mcw { public static double calArea( double r){ double PI = 3.14; //定义变量 double area = PI*r*r; //求圆的面积 return area; } public static double calPer( double r){ double PI = 3.14; //定义变量 double per = 2*PI*r; //求圆的面积 return per; } public static void main(String[] args){ double area = calArea(1.01); double per = calPer(2.03); System. out .println( "面积:" +area); System. out .println( "周长:" +per); } } |
18、输出小旗子和小树的形状。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | class mcw { public static void main(String[] args){ graphicTree(7); //传给下面这个函数的参数为7 } public static void graphicTree( int layer){ //layer 是7 String spaceStr = "" ; for ( int i=1;i<=layer ;i++ ) //从上到下7层 { System. out .print( " " ); if (i==1) { spaceStr+= " " ; } int stars = 2*i-1; //每层星星个数 for ( int j=0;j<stars ;j++ ) { System. out .print( "*" ); } System. out .println(); } for ( int j=0;j<layer;j++){ System. out .println(spaceStr+ "*" ); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | class mcw { public static void main(String[] args){ graphicTree(7); } public static void graphicTree( int layer){ String spaceStr = "" ; for ( int i=1;i<=layer ;i++ ) //从上到下n层 { int spaces =layer-i; for ( int j=0;j<spaces;j++){ System. out .print( " " ); if (i==1) { spaceStr+= " " ; } } int stars = 2*i-1; for ( int j=0;j<stars ;j++ ) { System. out .print( "*" ); } System. out .println(); } for ( int j=0;j<layer;j++){ System. out .println(spaceStr+ "*" ); } } } |
19、
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public class mcw { public static void main(String[] args){ printNum(19,799,7,17); //主函数中调用自定义函数并传参 } public static void printNum( int start, int end, int num1, int num2){ for ( int i=start;i<=end ;i++ ) //定义i在两个定值间自增 { if (i%num1==0&&i%num2==0) //一个区间内的数是否能同时被给出的值7,17整除 { System. out .println(i); } } } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏
· Manus爆火,是硬核还是营销?