java基础 | 3.语言基础(2)

 一、数组定义方式

class ArrayDemo 
{
	public static void main(String[] args) 
	{

		//元素类型[] 数组名 = new 元素类型[元素个数或数组长度];
		
		//需求:想定义一个可以存储3个整数的容器。
		int[] x = new int[3];



		//打印数组中角标为0的元素的值。
		System.out.println(x[1]);
	}
}

二、循环结构(for/while)

class ForDemo 
{
	public static void main(String[] args) 
	{

		/*
		for(初始化表达式;循环条件表达式;循环后的操作表达式)
		{
			执行语句;
		}

		*/

		for(int x = 0; x<3 ; x++)
		{
			System.out.println("x="+x);

		}
		//System.out.println("x===="+x);
		
		int y=0;
		while(y<3)
		{
			System.out.println("y="+y);
			y++;
		}
		System.out.println("y===="+y);

		/*
		1,变量有自己的作用域。对于for来讲:如果将用于控制循环的增量定义在for语句中。那么该变量只在for语句内有效。
		for语句执行完毕。该变量在内存中被释放。

		2,for和while可以进行互换。如果需要定义循环增量。用for更为合适。

		
		总结:
		什么时候使用循环结构?
		当要对某些语句执行很多次时,就使用循环结构。


		*/
	}
}
  • do while结构

class WhileDemo 
{
	public static void main(String[] args)
	{
		
		/*
		定义初始化表达式;
		while(条件表达式)
		{
			循环体(执行语句);
		}
		
		int x = 1;
		while(x<3)
		{
			System.out.println("x="+x);
			x++;
		}
		int x = 1;
		do
		{
			System.out.println("do : x="+x);
			x++;
		}
		while (x<3);
		*/
		int y = 1;
		while(y<3)
		{
			System.out.println("y="+y);
			y++;
		}

		
		/*
		while:先判断条件,只有条件满足才执行循环体。
		do while: 先执行循环体,在判断条件,条件满足,再继续执行循环体。
		简单一句话:do while:无论条件是否满足,循环体至少执行一次。

		*/
	}
}
  • for语句的嵌套形式 

//语句嵌套形式。其实就是语句中还有语句。
//循环嵌套。
class ForForDemo 
{
	public static void main(String[] args) 
	{
		for(int x=0; x<3; x++)//
		{
			for(int y=0; y<4; y++)
			{
				System.out.print("*");
			}
			System.out.println();//只有一个功能就是换行。
		}
		System.out.println("-------------------");

		/*
		
		*****
		****
		***
		**
		*
		发现图形有很多行,每一个行有很多列。
		要使用嵌套循环。原理:形象说法:大圈套小圈。
		

		*/
		
		//int z = 5;
		for (int x=0; x<5 ;x++ )//x<5:因为外循环控制行数。一共5行。
		{
			for (int y=x; y<5 ;y++)
			{
				System.out.print("*");
			}
			System.out.println();
			//z++;
		}
	}
}

/*

****
****
****
对于打印长方形:外循环控制的行数。内循环控制的是每一行的列数。也就是一行中元素的个数。

*****
****
***
**
*





*/
  • 练习1 九九乘法表示例(for嵌套规律总结) 

/*

*
**
***
****
*****


1
12
123
1234
12345



九九乘法表
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9


*/

class  ForForTest
{
	public static void main(String[] args) 
	{
		/*
		
		*
		**
		***
		****
		*****
		
		不是规律的规律:
		尖朝上,可以改变条件。让条件随着外循环变化。

		尖朝下,可以初始化值,让初始化随着外循环变化。
		
		*/

		for (int x=0; x<5 ;x++ )
		{
			for (int y=0 ; y<=x ; y++ )
			{
				System.out.print("*");
			}
			System.out.println();
		}

		System.out.println("----------------------");

		/*
		
		1
		12
		123
		1234
		12345

		*/

		for (int x=1; x<=5; x++)
		{ 
			for (int y=1; y<=x;y++ )
			{
				System.out.print(y);
			}
			System.out.println();
		}

		System.out.println("----------------------");


		/*
		
		九九乘法表
		1*1=1
		1*2=2 2*2=4
		1*3=3 2*3=6 3*3=9

		
		*/

		for (int x=1; x<=9 ; x++)
		{
			for (int y=1; y<=x; y++)
			{
				System.out.print(y+"*"+x+"="+y*x+"\t");
			}
			System.out.println();
		}

	}
}


  •  练习2 交替打印

/*

----*
---* *
--* * *
-* * * * 
* * * * * 

* * * * * 
-* * * *
--* * * 
---* *
----* 

*/

class  ForForTest2
{
	public static void main(String[] args) 
	{
		for (int x=0; x<5 ;x++ )
		{
			for(int y=x+1; y<5 ; y++)
			{
				System.out.print(" ");
			}
			for(int z=0; z<=x ; z++)
			{
				System.out.print("* ");
			}

			System.out.println();
		}
	}
}
  • for循环的执行顺序、无限循环 

class ForTest 
{
	public static void main(String[] args) 
	{
		int x = 1;
		for(System.out.println("a"); x<3 ;System.out.println("c"),x++)
		{
			System.out.println("d");
			//x++;
		}
		//adcdc

		for(int y=0; y<3; y++)
		{
			
		}
		/*
		无限循环的最简单表现形式。
		for(;;){}

		while(true){}
		*/
	}
}
  • 练习3 获取1-10的和 

/*
1,获取1~10的和,并打印。

*/

class  ForTest2
{
	public static void main(String[] args) 
	{

		/*
	
		//1,定义变量用于存储不断变化的和。
		int sum = 0;

		//2,定义变量,记录住不断变化的被加的数。
		int x = 1;
		//3,定义循环,重复加法的过程。
		while(x<=10)
		{
			sum = sum + x;
			x++;

		}
		System.out.println("sum="+sum);

		*/
		/*
		循环注意:
		一定要明确哪些语句需要参与循环,哪些不需要。
		*/
		/*
	  0+1
		1+2
		 3+3
		   6+4
		   */
		//用for来体现。
		int sum = 0;

		for(int x=0; x<=10; x++)
		{
			sum += x;
			
		}
		System.out.println("for sum = "+sum);

		/*
		其实这就是累加思想。
		原理:通过变量记录住每次变化的结果。
		通过循环的形式。进行累加动作。

		*/
	}
}
  • 练习4 1-00之间7的倍数的个数 

/*
2,1~100之间 7的倍数的个数。并打印。
思路:
1,先对1~100进行循环(遍历)通过循环的形式。
2,在遍历的过程中,定义条件。只对7的倍数进行操作。
3,因为7的倍数不确定,只要符合条件,就通过一个变量来记录住这个变化的次数。


步骤:
1,定义循环语句,选择for语句。
2,在循环中定义判断。只要是7的倍数即可。使用if语句。条件:7的倍数 x%7==0;
3,定义变量,该变量随着7的倍数的出现而自增。
*/
class  ForTest3
{
	public static void main(String[] args) 
	{
		int count = 0;
		for(int x=1; x<=100; x++)
		{			
			if(x%7==0)
				//System.out.println("x="+x);
				count++;
		}
		System.out.println("count="+count);

		/*
		计数器思想。
		通过一个变量记录住数据的状态变化。
		也许通过循环完成。

		*/
	}
}
  • 练习5 

/*
练习:3000米长的绳子,每天减一半。问多少天这个绳子会小于5米?不考虑小数。

*/
class  ForTest4
{
	public static void main(String[] args) 
	{
		int day = 0;
		for(int x=3000; x>=5; x/=2)
		{
			day++;
		}
		System.out.println("day="+day);

		/*
		for(int x=3000; x>=5; day++)
		{
			x = x/2;
		}
		*/
	}
}

 三、函数

class FunctionDemo 
{
	

	public static void main(String[] args) 
	{
		/*
		int x = 4;
		System.out.println(x*3+5);

		x = 6;

		System.out.println(x*3+5);
		*/
		//int y = 4*3+5;
		//int z = 6*3+5;

		//int x = getResult(4);
		//System.out.println("x="+x);
		//int y = getResult(6);

		getResult(5);

	}

	//发现以上的运算,因为获取不同数据的运算结果,代码出现了重复。
	//为了提高代码的复用性。对代码进行抽取。
	//将这个部分定义成一个独立的功能。方便与日后使用。
	//java中对功能的定义是通过函数的形式来体现的。

	//需要定义功能,完成一个整数的*3+5的运算,

	//1,先明确函数定义的格式。
	/*

	修饰符 返回值类型 函数名(参数类型 形式参数1,参数类型 形式参数2,)
	{
			执行语句;
			return 返回值;
	}
	
	//当函数运算后,没有具体的返回值时,这是返回值类型用一个特殊的关键字来标识。
	//该关键字就是void。void:代表的是函数没有具体返回值的情况。
	//当函数的返回值类型是void时,函数中的return语句可以省略不写。
	*/
	public static void getResult(int num)
	{
		System.out.println(num * 3 + 5);
		return;//可以省略
	}
	
  • 函数的定义 

class FunctionDemo2 
{
	public static void main(String[] args) 
	{
		/*
		int sum = getSum(4,6);

		System.out.println("sum="+sum);
		sum = getSum(2,7);
		System.out.println("sum="+sum);
		*/
		//get(4,5);
		int x = getSum(4,4);
		int y = getSum(7,9);
		int num = getMax(x,y);

	}
	
	/*
	这个功能定义思想有问题,为什么呢?因为只为完成加法运算,至于是否要对和进行打印操作,
	那是调用者的事,不要在该功能中完成。

	public static void get(int a,int b)
	{
		System.out.println(a+b);
		return ;
	}
	*/

	/*
	
	如何定义一个函数呢?
	1,既然函数是一个独立的功能,那么该功能的运算结果是什么先明确
		因为这是在明确函数的返回值类型。
	2,在明确在定义该功能的过程中是否需要未知的内容参与运算。
		因为是在明确函数的参数列表(参数的类型和参数的个数)。

	*/

	//需求:定义一个功能。完成3+4的运算。并将结果返回给调用者。
	/*
	1,明确功能的结果:是一个整数的和。
	2,在实现该功能的过程中是否有未知内容参与运算,没有。
	其实这两个功能就是在明确函数的定义。
	1,是在明确函数的返回值类型。
	2,明确函数的参数列表( 参数的类型和参数的个数)。
	
	
	public static int getSum()
	{
		return 3+4;
	}
	*/
	/*
	以上这个函数的功能,结果是固定的,毫无扩展性而言。
	为了方便用户需求。由用户来指定加数和被加数。这样,功能才有意义。
	思路:
	1,功能结果是一个和。返回值类型是int。
	2,有未知内容参与运算。有两个。这个两个未知内容的类型都是int。
	*/
	public static int getSum(int x,int y)
	{
		return x+y;
	}
	

	/*
	需求:判断两个数是否相同。
	思路:
	1,明确功能的结果:结果是:boolean 。
	2,功能是否有未知内容参与运算。有,两个整数。
	*/
	public static boolean compare(int a,int b)
	{
		/*
		if(a==b)
			return true;
		//else
			return false;
		*/

		//return (a==b)?true:false;

		return a==b;
	}

	/*
	需求:定义功能,对两个数进行比较。获取较大的数。
	*/
	public static int getMax(int a,int b)
	{
		/*
		if(a>b)
			return a;
		else
			return b;
			*/
		return (a>b)?a:b;
	}
}
  • 函数的重载 

/*
什么时候用重载?
当定义的功能相同,但参与运算的未知内容不同。
那么,这时就定义一个函数名称以表示起功能,方便阅读,而通过参数列表的不同来区分多个同名函数。

*/

class FunctionOverload 
{
	public static void main(String[] args) 
	{

//		add(4,5);
//		add(4,5,6);
		print99();

	}
	public static void print99(int num)
	{
		for(int x=1; x<=num; x++)
		{
			for(int y=1; y<=x; y++)
			{
				System.out.print(y+"*"+x+"="+y*x+"\t");
			}
			System.out.println();
		}
	}

	//打印99乘法表
	public static void print99()
	{
		print99(9);
	}

	//定义一个加法运算,获取两个整数的和。
	public static int add(int x,int y)
	{
		return x+y;
	}

	//定义一个加法,获取三个整数的和。
	public static int add(int x,int y,int z)
	{
		return add(x,y)+z;
	}
}


/*

void show(int a,char b,double c){}

a.
void show(int x,char y,double z){}//没有,因为和原函数一样。

b.
int show(int a,double c,char b){}//重载,因为参数类型不同。注意:重载和返回值类型没关系。
c.

void show(int a,double c,char b){}//重载,因为参数类型不同。注意:重载和返回值类型没关系。

d.
boolean show(int c,char b){}//重载了,因为参数个数不同。

e.
void show(double c){}//重载了,因为参数个数不同。

f.
double show(int x,char y,double z){}//没有,这个函数不可以和给定函数同时存在与一个类中。


*/
  • 练习 

/*
1,定义一个功能,用于打印矩形。

2,定义一个打印99乘法表功能的函数。

*/

class  FunctionTest
{
	public static void main(String[] args) 
	{
//		draw(5,6);
//		printHr();
//		draw(7,9);
//		printHr();

		print99();
		

	}

	/*
	定义一个打印99乘法表功能的函数。
	*/
	public static void print99()
	{
		for(int x=1; x<=9; x++)
		{
			for(int y=1; y<=x; y++)
			{
				System.out.print(y+"*"+x+"="+y*x+"\t");
			}
			System.out.println();
		}
	}

	
	/*
	定义一个功能,用于打印矩形。
	思路:
	1,确定结果:没有,因为直接打印。所以返回值类型是void
	2,有未知内容吗?有,两个,因为矩形的行和列不确定。
	*/
	public static void draw(int row,int col)
	{
		for(int x=0; x<row; x++)
		{
			for(int y=0; y<col; y++)
			{
				System.out.print("*");
			}
			System.out.println();
		}
	}

	public static void printHr()
	{
		System.out.println("------------------------------");
	}

}
  •  break和continue

class OtherDemo 
{
	public static void main(String[] args) 
	{
		//break:
		w:for(int x=0; x<3; x++)
		{
			for(int y=0; y<4; y++)
			{
				System.out.println("x="+x);
				break w;
			}				
		}

		//continue:只能作用于循环结构。继续循环。特点:结束本次循环,继续下一次循环。

		for(int x=1; x<=10; x++)
		{
			if(x%2==1)
				continue;
			System.out.println("x="+x);
			
		}

		w:for(int x=0; x<3; x++)
		{
			for(int y=0; y<4; y++)
			{
				System.out.println("x="+x);
				continue w;
			}				
		}

		/*
		记住:
		1,break和continue语句作用的范围。
		2,break和continue单独存在时,下面可以有任何语句。因为都执行不到。
		*/

//		break;
//		continue;
	}
}

 四、练习

1.
已知学生成绩以100分为满分,共分5个等级:A,B,C,D,E。
   90~100为等级A,80~89为等级B,70~79为等级C,
   60~69为等级D,0~59为等级E。
   要求定义一个成绩变量,当成绩变化时,可直接知道该成绩对应的等级。
   例如:当成绩为100时,该学生的等级时A。

class Demo
{
	//定义一功能,通过给定分数,获取该分数对应的等级。
	/*
	1,明确该功能的结果:等级 char
	2,有没有未知内容。分数。int
	*/
	public static String getLevel(int num)
	{
		char level;
		if(num>=90 && num<=100)
			level = 'A';
		else if(num>=80 && num<=89)
			level = 'B';
		else if(num>=70 && num<=79)
			level = 'C';
		else if(num>=60 && num<=69)
			level = 'D';
		else
			level = 'E';

		return level;
	}
	public static void main(String[] args)
	{
		
		char ch = getLevel(35);
		System.out.println("level="+ch);
	}
	
}






2.
写出输出结果。
class Demo
{
	public static void main(String[] args)
	{
		show(0);//15
		show(1);//14
	}	
	public static void show(int i)
	{
		switch(i)
		{	
			default:
				i+=2;
			case 1:
				i+=1;
			case 4:
				i+=8;			
			case 2:
				i+=4;
		}
		System.out.println("i="+i);
	}	
}

3.写出输出的结果.
class Demo
{
	public static void main(String[] args)
	{
		int x=0,y=1;
		if(++x==y--&x++==1||--y==0)
			System.out.println("x="+x+",y="+y);//x=2,y=0
		else
			System.out.println("y="+y+",x="+x);
	}
}


4.
求出1~100之间,即使3又是7的倍数出现的次数?



5.
用程序的方式显示出下列结果。

1*1=1
1*2=2   2*2=4
1*3=3   2*3=6   3*3=9
1*4=4   2*4=8   3*4=12  4*4=16
1*5=5   2*5=10  3*5=15  4*5=20  5*5=25


6.写出程序结果。

class Demo
{
	public static void main(String[] args)
	{
		int x = 1;
		for(show('a'); show('b') && x<3; show('c'))
		{
			show('d'); 
			x++;
		}
	}
	public static boolean show(char ch)
	{
		System.out.println(ch);
		return true;
	}
}

//a b d c b d c b
第一题

int x = 1,y=1;

if(x++==2 & ++y==2)
{
	x =7;
}
System.out.println("x="+x+",y="+y);//x=2,y=2


---------------------------------------------------
第二题
int x = 1,y = 1;

if(x++==2 && ++y==2)
{
	x =7;
}
System.out.println("x="+x+",y="+y);x=2,y=1

---------------------------------------------------

第三题
int x = 1,y = 1;

if(x++==1 | ++y==1)
{
	x =7;
}
System.out.println("x="+x+",y="+y);x=7,y=2


---------------------------------------------------

第四题
int x = 1,y = 1;

if(x++==1 || ++y==1)
{
	x =7;
}
System.out.println("x="+x+",y="+y);//x=7,y=1



---------------------------------------------------
第五题
boolean b = true;

if(b=false)  //如果写成if(b=false)有结果吗?如果有,结果是?
	System.out.println("a");
else if(b)
	System.out.println("b");
else if(!b)
	System.out.println("c");
else
	System.out.println("d");
	
//b
if(b=false)
//c
---------------------------------------------------

第六题
int x = 2,y=3;

switch(x)
{
	default:
		y++;
	case 3:
		y++;
	case 4:
		y++;
}

System.out.println("y="+y);//y=6

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值