第一次Coding Exercise

Coding Time

1.Calculate the

目录

Coding Time

1.Calculate the factorial of 10 and print out result.计算10的阶乘和其结果。

2.Determine whether a number is even or odd,print out the result.

3.Try using "+" operation between Strings,Strings and numbers.

Judge statement判断语句

 在条件判断语句中,经常会放一些运算符Relational operators,

 Logical operators逻辑操作符(Only apply to boolean)

 De Morgan's Law 德摩根定律        This law can be expressed as ( A ∪ B) ‘ = A ‘ ∩ B ‘ 

Precedence 优先级    

Branch Statement

Loop Statement: the powerful tool of computer

Scope of the variable in programs  变量的域

第二次 CodingTime

1.Calculate the factorial of 100.

2.Print out the multiplication table.


factorial of 10 and print out result.计算10的阶乘和其结果。

class Test{
	//程序入口
	public static void main(String args[]){
		int result=1*2*3*4*5*6*7*8*9*10;
		System.out.println(result);		
	}
}

2.Determine whether a number is even or odd,print out the result.

3.Try using "+" operation between Strings,Strings and numbers.

class Test{
	//程序入口
	public static void main(String args[]){
		int result=1*2*3*4*5*6*7*8*9*10;
		//System.out.println(result);		
		
		int a=(int)(10*Math.random());
		System.out.println(a);
		System.out.println(a%2);
		
		String introduce="My name is Java!";
		String address="I live in computer";

		System.out.println(introduce+address);
		System.out.println(introduce+result);
		System.out.println(a+result);
		
	}
	
}

注意:如果出现了中文字符,则编译会产生编译失败,乱码,意思是出现了中文字符,不能编译成功。

 mac在terminal运行方式,相同的运行方式,使用cd 命令进入某个目录,用javac命令进行编译,再用java命令对生成的类文件运行出结果

Judge statement判断语句

计算机指令的执行顺序,就是我们执行的顺序,这个顺序不会变的,而且所有的指令都会被执行一遍。但是对于解决问题而言,这从从上到下的顺序是不合理的,一旦遇到需要选择的情况,则需要条件判断语句。

The statement form:

if(boolean)

{

statements

}

//statements will be executed if Boolean expression is true.

例如,判断数字是奇数还是偶数,则需要使用if 判断语句,判断其实现的效果。

class Test{
	//程序入口
	public static void main(String args[]){
		
		int a=(int)(10*Math.random());
		System.out.println(a);
		if(a%2==1){
			System.out.println("The number"+a+"is an odd number!");
		}
		if(a%2==0){
			System.out.println("The number "+a+"is an even number");	
		}
	}
	
}

 在条件判断语句中,经常会放一些运算符Relational operators,

 Notes:

The result is a Boolean (either true or false)

Except"==" ;all the relational operators only apply to type int,float and double.综上表格中这些关系操作符都是用来比较数和数之间的。

For"==";apply to type int ,float ,double and String.判断是否相等,既可以判断数,也可以判断字符类型,都可以。举个例子:

        String name1="David";
        String name2="david";
        System.out.println(name1==name2);

结果:

 Logical operators逻辑操作符(Only apply to boolean)

 De Morgan's Law 德摩根定律        This law can be expressed as ( A ∪ B) ‘ = A ‘ ∩ B ‘ 

Precedence 优先级    

   不需要记忆,先要运算的,就用括号给他括在一起,后运算的放在括号外面。

Branch Statement

class Test{
	//程序入口
	public static void main(String args[]){
		
		int a=(int)(10*Math.random());
		//System.out.println(a);
		if(a%2==1){
			System.out.println("The number"+a+"is an odd number!");
		}
		else{
			System.out.println("The number "+a+"is an even number");	
		}

	}

 ----More about judge statement --If else statement分支语句

QUE:  Determine the grade according to scores

class Test{
	//程序入口
	public static void main(String args[]){
		int score =67;
		if(score>=90 && score <100){
			System.out.println("A");
		}
		if(score>=80 && score <90){
			System.out.println("B");
		}
		if(score>=70 && score <80){
			System.out.println("C");
		}
		if(score>=60 && score <70){
			System.out.println("D");
		}
		if(score<60){
			System.out.println("Fail");
		}
	}
	
}

 这种写法,不免非常的繁琐,所以修改成if ---else if,等语句

class Test{
	//程序入口
	public static void main(String args[]){
		int score =100;
		if(score>=90 && score <=100){
			System.out.println("A");
		}else if(score>=80){
			System.out.println("B");
		}else if(score>=70){
			System.out.println("C");
		}else if(score>=60){
			System.out.println("D");
		}else {
			System.out.println("Failed");
		}
		
	}
	
}

每一个else if就是一个分支,一旦满足,则执行语句,一旦前面的语句没有被满足,则流换到下一个分支(这里可以强调一下,100这个数字,如果不在条件中添加,运行的结果,会自动流入下一个分支语句,从而输出B)。

而两者的区别是什么?

一旦abc三个条件都满足。

前者,是执行的每一条if语句,后者,并没有执行每一条语句,而是只会执行第一条语句。并且相当于、等价于

Loop Statement: the powerful tool of computer

for (int i = 0; i <100; i++){

}

int i = 0,表示给i赋一个初始值;the start of the loop

i<100 ,判断条件,当i满足这个条件时,循环继续  the loop condition; the loop ends when condition is not satisfied

i++ , 循环进行一次后,会发生什么样的变化  the loop body--what to loop

{} 中的内容为循环体。 the loop gap

//循环输出1-10
class Test{
	public static void main(String args[]){
		for(int i=1;i<=10;i+=2){
			System.out.println(i);
		}
	}
}

在循环语句的语句体中,还是一个循环,这种语句叫做嵌套 nested,

循环体的里面,为内层循环,循环体的外面的循环,为外层循环。

Scope of the variable in programs  变量的域

首先,变量不可以重名,否则程序会报错。

在Java中一个大括号,就是一个域。编译器通过大括号来判断从属关系,通过缩进格式,来增加代码的可读性。

第二次 CodingTime

1.Calculate the factorial of 100.

首先需要一个变量,用来放计算的结果。

//循环输出1-10
class Test{
	public static void main(String args[]){
		int result=1;
		for(int i=1;i<=10;i++){
			result*=i;
		}
		System.out.println(result);
	}
}

首先,先用循环语句,计算10的阶乘。注意:reslut的初始值,要设置为1,而不是0,因为0乘任何数都是0.

但是,当你把10换成100,会发现这个结果为0,因为发生了溢出,int的精度难以承担这个结果,所以结果就成这了。 所以,把int的这个数据类型换成精度更大的,例如double,(double表示的范围是双精度浮点,要比float(单精度)浮点数范围更大,精度更高,但是也没有占据更多的资源,所以,使用double需要更多一些。

class Test{
    public static void main(String args[]){
        double result=1;
        for (int i =1;i<=100;i++){
            result*=i;
        }
        System.out.print(result);
    }
}

然后运行一下,会发现结果为

 其中E为exponential指数的缩写,指的是,结果是前面的小数乘10的157次方。(可以验证一下,把100换成99,进行循环,就会少100倍,也是10的2次方,所以指数就会少2)

2.Print out the multiplication table.

 思路:三角形的阵列,第一行一个乘号,第二行两个乘号,第三行三个乘号。

先打出九个乘号,再用一个循环再套一个循环(即循环体本身还是一个循环)nested loop 循环的嵌套。

直接嵌套会发现打印出来了81个乘号,是因为没有进行换行,所以添加一个换行语句。

 class Test{
    public static void main(String args[]){
        for (int i =1;i<=9;i++){
            for(int j=1;j<=9;j++){
                System.out.print("X ");
            }
            System.out.println();
        }     
    }
}

要想运行出来一个三角阵,则需要把j<=9换成j<=i即可。


class Test{
	public static void main(String args[]){
		
		for (int i =1;i<=9;i++){
			
			for(int j=1;j<=i;j++){
				int result=i*j;
				System.out.print(j+" X "+i+" = "+result+"  ");
			}
			System.out.println();
		}
		
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值