java学习第05天--程序

循环结构:do-while
do while语句格式:
  [ 初始化部分 ]
do {
  循环体语句;
  [ 循环变量控制语句 ]
}while(条件表达式) ;
执行流程
1. 执行初始化部分(如果有的话)
2. 执行循环体语句
3. 执行循环变量控制语句(如果有的话)
4. 判断条件表达式,如果为真,返回第 2 步;如果假,结束循环
特点:循环体至少会被执行一次

循环嵌套:
案例:
1、循环嵌套打印正三角形
public class ForForDemo{
public static void main(String[] args){
for(int i = 1; i <= 5; i++){ //打印行数
for(int j = 0; j < i ; j ++){ //打印列数
System.out.print("$");
}
System.out.println();
}
}
}
2、循环嵌套打印倒三角形
public class ForForDemo{
public static void main(String[] args){
for(int i = 1; i <= 5; i++){ //打印行数
for(int j = 0; j < 5 - i ; j ++){ //打印列数
System.out.print("$");
}
System.out.println();
}
}
}

3、99乘法表的打印
while-while表示:
public class TableDemo{
public static void main(String[] args){
//乘法表的显示 while-while表示
int i = 1;
while(i < 10){
int j = 1;
while(j < i + 1){
System.out.print(i + "x" + j + "=" + i*j + "\t");
j++;
}
i ++;
System.out.println();
}
}
}

for-for表示:
public class TableDemo{
public static void main(String[] args){
for(int i = 1; i < 10; i ++){
for(int j = 1; j < i + 1; j ++){
System.out.print(i + "x" + j + "=" + i*j + "\t");
}
System.out.println();
}
}
}

方法Method
什么是方法?
方法就是定义在类中的,具有特定功能的一段小程序
方法也称为函数(Function)
方法可以接收输入,根据需要输出内容

方法的定义格式:
修饰符 返回值类型 方法名( 参数类型 形式参数1,参数类型 形式参数2,... ){
  方法体语句;
  return [ 返回值 ];
}
注解:
修饰符:暂时是固定写法 public static
返回值类型:方法执行后返回结果的数据类型
形式参数列表:
    参数类型:是形式参数的数据类型。
    形式参数:是一个变量,用于接收方法被调用时传给方法的实际参数的值
实际参数:方法调用时传递给形式参数的具体数值
return:关键字,用于标示方法结束
返回值:该方法运算后需要返回给调用者的结果,其类型要和返回值类型匹配,
    或者可以自动转型

定义方法的格式重点:两个明确
返回值类型:方法执行完,需要返回东西给调用者么?
  形参列表:方法需要什么 原材料
方法的返回值如何定义?
方法是否需要返回值,返回值是什么类型,完全取决于业务逻辑,没有固定模式
方法的形参列表如何定义?
形参列表相当于这个方法的“原材料”,同样是根据业务逻辑来决定
返回值为 void 类型的方法,是否可以加 return 语句?
return 语句只是用来表示一个方法的结束
当有返回值的时候,负责把返回值带回
若是没有返回值 ( 即返回值类型为 void), 那就在 return 后什么都不接 , 这种情况下 , 也可以将 return 省略

方法的调用:
1. 有返回值方法的调用
  可以使用其返回值 ( 赋值给其它变量 )
  也可以不理会(单独调用)
2. 没有明确返回值方法的调用
  即返回值类型为 void 的方法,只能单独调用

方法的优点:
定义方法可以将功能代码进行封装
提高了代码的复用性
方法只有被调用才会被执行
注意:
方法之间是调用关系,不可以在方法内部再定义方法(方法内不能再包含方法定义)
暂时将方法的修饰符固定写成 public static

方法练习案例:
1、方法小程序的练习
/*
修饰符 返回值类型 方法名(形参类型 形参数){
方法体语句;
return [返回值];
}
方法定义在类中,main方法之外
方法不能嵌套定义
*/

public class MethodDemo{
public static void main(String[] args){
int i = 24;
int j = 26;
//调用add方法,并将计算值赋值给res
int res = add(i,j);
System.out.println(res);
}
//自定义方法
public static int add(int i, int j){
int tmp = i + j;
return tmp;
}
}

2、计算两个double型数的和
/*
计算两个double型数的和
*/

public class MethodDemo2{
public static void main(String[] args){
double res = add(24.0,26.0);
System.out.println(res);
//直接赋值
//System.out.println(add(24.0,26.0));
}
//add方法
public static double add(double a,double b){
double temp = a + b;
return temp;
}
}

3、使用调用方法实现99乘法表的打印
/*
使用调用方法实现99乘法表的打印
*/

public class MethodDemo3{
public static void main(String[] args){
//调用Table方法
Table();
}
//自定义方法
public static void Table(){
for(int i = 1; i < 10; i ++){
for(int j = 1; j < i + 1; j ++){
System.out.print(j + "x" + i + "=" + i*j + "\t");
}
System.out.println();
}
}
}

4、使用调用方法实现99乘法表的打印
/*
使用调用方法实现99乘法表的打印
*/
import java.util.Scanner;

public class MethodDemo4{
public static void main(String[] args){
//通过键盘输入随机数进行乘法表的打印
Scanner sc = new Scanner(System.in);
System.out.print("请输入打印几几乘法表:");
int line = sc.nextInt();
//调用Table方法
if(line <= 0){
System.out.print("对不起,您输入的数是负数");
}else{
Table(line);
}
}
//自定义方法
public static void Table(int line){
for(int i = 1; i <=line; i ++){
for(int j = 1; j < i+1; j ++){
System.out.print(j + "x" + i + "=" + i*j + "\t");
}
System.out.println();
}
}
}

5、定义方法,用于打印任意行数和列数的“+”号
/*
定义方法,用于打印任意行数和列数的“+”号
*/

import java.util.Scanner;

public class MethodDemo5{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.print("请输入行数:");
int line = sc.nextInt();
System.out.print("请输入列数:");
int col = sc.nextInt();
showPlus(line,col);
}
public static void showPlus(int line, int col){
for(int i = 1; i <= line; i++){
for(int j = 1; j < col+ 1; j ++){
System.out.print("$");
}
System.out.println();
}
}
}

6、定义方法,用于判断一个int数是否为偶数
/*
定义方法,用于判断一个int数是否为偶数
*/
import java.util.Scanner;

public class MethodDemo6{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.print("请输入一个数:");
int num = sc.nextInt();
judge(num);
}
public static void judge(int num){
if(num % 2 == 0){
System.out.print(num + "是偶数");
}else{
System.out.print(num + "不是偶数");
}
}
}


//方法二
/*
定义方法,用于判断一个int数是否为偶数
*/

/*
import java.util.Scanner;

public class MethodDemo6{
public static void main(String[] args){
Scanner s = new Scanner(System.in);
while(true){
System.out.println("输入一个int值:(-8退出)");
int n = s.nextInt();
if(n == -8){
break;
}else{
boolean res = isEven(n);
System.out.println("你输入的数: "+ n + "是否是偶数? "+ res);
}
}
}
//boolean
//int a
public static boolean isEven(int a){
/*
boolean f;
if(a % 2 == 0){
f = true;
}else{
f = false;
}
return f;
*/
// return (a % 2 == 0)?true:false;
/*
return a % 2 == 0;
}
}
*/

7、定义方法,接收一个int值,返回该值对应是星期几,要求使用switch结构实现
/*
定义方法,接收一个int值,返回该值对应是星期几,要求使用switch结构实现
*/

import java.util.Scanner;

public class MethodDemo7{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.print("请输入一个数:(0-7)");
int week = sc.nextInt();
judgeWeek(week);
}
public static void judgeWeek(int week){
switch(week){
case 1:{
System.out.println("星期一");
break;
}
case 2:{
System.out.println("星期二");
break;
}
case 3:{
System.out.println("星期三");
break;
}
case 4:{
System.out.println("星期四");
break;
}
case 5:{
System.out.println("星期五");
break;
}
case 6:{
System.out.println("星期六");
break;
}
case 7:{
System.out.println("星期天");
break;
}
default:{
System.out.println("对不起,您输入的数错误");
}
}
}
}


//方法二:
/*
import java.util.Scanner;

public class MethodDemo7{
public static void main(String[] args){
Scanner s = new Scanner(System.in);
while(true){
System.out.println("输入一个int值(1-7,-8退出)");
int i = s.nextInt();
if(i == -8){
break;
}else{
String week = getWeek(i); //将返回的字符串变量赋值
System.out.println(i + " 对应的星期是: " + week);
}
}
}
//String
//int a
public static String getWeek(int a){
String week; //定义week字符串变量
switch(a){
case 1:{
week = "星期一";
break;
}
case 2:{
week = "星期二";
break;
}
case 3:{
week = "星期三";
break;
}
case 4:{
week = "星期四";
break;
}
case 5:{
week = "星期五";
break;
}
case 6:{
week = "星期六";
break;
}
case 7:{
week = "星期日";
break;
}
default :{
week = "没有对应的星期";
break;
}
}
return week; //返回字符串变量
}
}
*/

8、用于获取三个数中的是最大值
/*
用于获取三个数中的是最大值
*/
import java.util.Scanner;

public class MethodDemo8{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.print("请输入第一个数:");
int i = sc.nextInt();
System.out.print("请输入第二个数:");
int j = sc.nextInt();
System.out.print("请输入第三个数:");
int k = sc.nextInt();
int max = getMax(i,j,k); //方法中有return返回值,所以要创建个变量进行接收
System.out.println("三个数中最大的值是:" + max);
}
public static int getMax(int i, int j, int k){
int max = (i>j)?i:j;
max = (max > k)?max:k;
return max;
//System.out.println("三个数中的最大值是:" + max);
}
}

9、定义方法,实现打印两个double型值的四则运算结果
/*
定义方法,实现打印两个double型值的四则运算结果
*/
import java.util.Scanner;

public class MethodDemo9{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.print("请输入第一个数:");
double i = sc.nextInt();
System.out.print("请输入第二个数:");
double j = sc.nextInt();
// double i = 20;
// double j = 10;
double add = add(i,j);
System.out.println("和为:"+add );
double sub= sub(i,j);
System.out.println("差为:"+sub );
double multiply = multiply(i,j);
System.out.println("乘为:"+multiply );
double divide= divide(i,j);
System.out.println("商为:"+divide);
}
public static double add(double a, double b){
return a + b;
}
public static double sub(double a, double b){
return a - b;
}
public static double multiply(double a, double b){
return a * b;
}
public static double divide(double a, double b){
return a / b;
}
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值