资源描述
Java基本语句
IF语句
第一种:
class if
{
public static void main (String[] args)
{
int x=8;
if(x<9)
{
System.out.println("haha");
}
}
}
第二种:
class textif
{
public static void main (String[] args)
{
int x=5;
if(x<4)
{
System.out.println("haha");
}
else
{
System.out.println("gege");
}
System.out.println("bbbbbbbb");
}
}
第三种:
class ccc
{
public static void main (String[]args)
{
int x=6;
if(x<1)
{
System.out.println("haha");
}
else if(x<8)
{
System.out.println("hehe");
}
else
{
System.out.println("heihei");
}
}
}
Switch语句:
class ddd
{
public static void main (String[]args)
{
int x=8;
switch(x)
{
case 4:
System.out.println("haha");
break;
case 8:
System.out.println("hehe");
break;
case 6:
System.out.println("hihi");
break;
default:
System.out.println("gaga");
break;
}
}
}
循环语句
1:while语句
class fff
{
public static void main (String[]args)
{
int x=2;
while(x<4)
{
System.out.println("x="+x);
x++;//ctrl+c 停止当前DOS循环。
}
}
}
2:do while语句
class ggg
{
public static void main(String[]args)
{
int x=3;
do
{
System.out.println("x="+x);
x++;
}
while(x<8);
}
}
3:for语句
1:关键词 break
class hhh
{
public static void main(String[]args)
{
for(int x=0;x<5;x++)
{
System.out.println("x="+x);
}
System.out.println("---------------");
for(int x=7;x<14;x++)
{
System.out.println("x="+x);
break;//break作用:第一次循环到此结束,不往下继续循环。
}
}
}
2:关键词 continue
class jjj
{
public static void main(String[]args)
{
for(int x=1;x<=10;x++)
{
if(x%2==1)
continue;
System.out.println("x="+x);
}
}
}
For嵌套循环
class ppp
{
public static void main(String[]args)
{
int z=1;
for(int x=0;x<5;x++)
{
for(int y=0;y<z;y++)
{
System.out.print("*");
}
z++;
System.out.println();
}
}
}
打印结果
*
**
***
****
*****
展开阅读全文