函数
2016-07-13 00:00
190 查看
摘要: 函数的特点:
1. 函数是把一个功能的代码 封装起来以达到提高功能 代码的复用性。
2. 函数定义好之后,需要调用才能执行的。
3. 如果一个函数没有返回值类型,那么就使用void关键字表示。
需求: 定义一个函数判断分数的等级,然后返回分数对应的等级。
/*
函数:
函数的作用: 为了提高功能代码的复用性。
函数的定义格式:
修饰符 返回值类型 变量名(形式参数){
函数体
}
函数的特点:
1. 函数是把一个功能的代码 封装起来以达到提高功能 代码的复用性。
2. 函数定义好之后,需要调用才能执行的。
3. 如果一个函数没有返回值类型,那么就使用void关键字表示。
需求: 定义一个函数判断分数的等级,然后返回分数对应的等级。
"a等级"
*/
class Demo1
{
public static void main(String[] args)
{
String result = getGrade(90); //调用函数
System.out.println("对应的等级是:"+ result );
}
public static String getGrade(int score){ //未知的参数定义在形参中, 由函数的调用者确定。
if(score>=90&&score<=100){
return "A等级";
}else if(score>=80&&score<=89){
return "B等级";
}else if(score>=70&&score<=79){
return "C等级";
}else if(score>=60&&score<=69){
return "D等级";
}else if(score>=0&&score<=59){
return "E等级";
}
}
/*
public static String getGrade(int score){ //未知的参数定义在形参中, 由函数的调用者确定。
String grade = ""; //用于存储对应的等级
if(score>=90&&score<=100){
grade = "A等级";
}else if(score>=80&&score<=89){
grade = "B等级";
}else if(score>=70&&score<=79){
grade = "C等级";
}else if(score>=60&&score<=69){
grade = "D等级";
}else if(score>=0&&score<=59){
grade = "E等级";
}
return grade; // 把分数的等级返回给调用者。
}
*/
&nbs
3ff0
p;
}
1. 函数是把一个功能的代码 封装起来以达到提高功能 代码的复用性。
2. 函数定义好之后,需要调用才能执行的。
3. 如果一个函数没有返回值类型,那么就使用void关键字表示。
需求: 定义一个函数判断分数的等级,然后返回分数对应的等级。
/*
函数:
函数的作用: 为了提高功能代码的复用性。
函数的定义格式:
修饰符 返回值类型 变量名(形式参数){
函数体
}
函数的特点:
1. 函数是把一个功能的代码 封装起来以达到提高功能 代码的复用性。
2. 函数定义好之后,需要调用才能执行的。
3. 如果一个函数没有返回值类型,那么就使用void关键字表示。
需求: 定义一个函数判断分数的等级,然后返回分数对应的等级。
"a等级"
*/
class Demo1
{
public static void main(String[] args)
{
String result = getGrade(90); //调用函数
System.out.println("对应的等级是:"+ result );
}
public static String getGrade(int score){ //未知的参数定义在形参中, 由函数的调用者确定。
if(score>=90&&score<=100){
return "A等级";
}else if(score>=80&&score<=89){
return "B等级";
}else if(score>=70&&score<=79){
return "C等级";
}else if(score>=60&&score<=69){
return "D等级";
}else if(score>=0&&score<=59){
return "E等级";
}
}
/*
public static String getGrade(int score){ //未知的参数定义在形参中, 由函数的调用者确定。
String grade = ""; //用于存储对应的等级
if(score>=90&&score<=100){
grade = "A等级";
}else if(score>=80&&score<=89){
grade = "B等级";
}else if(score>=70&&score<=79){
grade = "C等级";
}else if(score>=60&&score<=69){
grade = "D等级";
}else if(score>=0&&score<=59){
grade = "E等级";
}
return grade; // 把分数的等级返回给调用者。
}
*/
&nbs
3ff0
p;
}
相关文章推荐
- return 关键字的作用
- 函数的重载
- 数组的冒泡排序
- ubuntu 基本软件安装命令
- ios 时间以unix时间戳现显示
- 【JavaScript高级程序设计】--第2章 在HTML中使用JavaScript
- 我写的GO的基础语法书,希望对喜欢Go的有帮助
- Linux上安装gitlab所遇到的问题及解决方法
- mongoDB 常用操作
- 【Java】serialVersionUID的作用
- linux zip/unzip命令
- Android Hack进行时: 取得了"system"用户,还需继续努力升格为root用户
- UIImageView特性的详细尝试
- Button 特性
- Spring--Quartz定时器调度
- static、const和static const
- mysql之DATE_FORMAT
- 关于Android的内存泄露的笔记
- go语言学习-变量的问题
- toString方法和java.lang.StackOverflowError