java第三天

1.for循环语句
package demo;
//*for 循环
//for(表达式1;表达式2;表达式3){
// 循环条件
//
//
//
//

import java.util.Scanner;

public class demo401 {

	public static void main(String[] args) {
	Scanner input=new Scanner(System.in);
	double sum=0;
	boolean doing=false;
	for (int i = 0; i < 5; i++) {
		System.out.println("请输入第"+(i+1)+"次成绩");
		double score =input.nextDouble();
		if (score<0) {
			doing=true;
			break;
		}
		sum+=score;
		
	   }
	   if (doing) {
		   System.out.println("输入错误!");
	} else {
		double avg =sum/5;
		System.out.println("平均分"+avg);
     }
  }

}

2.for循环的运用:输出加法表
package demo;

import java.util.Scanner;

public class 输出加法表 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.println("请输入一个数字:");
		int num =input.nextInt();
		for (int i = 0,j=num; i < num; i++,j--) {
			System.out.println(i+"+"+j+"="+(i+j));
			
		}

	}

}

3.函数:

package demo;

public class 函数 {

	public static void main(String[] args) {
		int a =10;
		int b =20; 
		int c =30;
//		sum(a, b);
//        int c =sum(a, b);
        System.out.println(sum(a, b));
		System.out.println(sum(a, b,c));
	}
//静态求和函数
//    public static void sum(int a,int b) {
//		System.out.println("和:"+(a+b));
//	}
//方法重载:使用相同方法名,传入不同参数列表,获得对应的执行结果
    public static int sum(int m,int n) {
		return m+n;
	}
    public static int sum(int m,int n,int l) {
 		return m+n+l;
 	}
}

4.数组定义:
package demo;
/*

  • 数组定义
  • 数据类型[] 数组名=new 数据类型[];
  • 数组名[下标]=数值
  • 数据类型[]数组名=new 数据类型[]{元素1,元素2,元素3…元素n};
  • 数据类型[]数组名={元素1,元素2,元素3…元素n};

*/
import java.util.Scanner;

public class 数组 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
//		int [] scores= new int[3] ;
//		scores[0]=60;
//		scores[1]=70;
//		scores[2]=80;
//		int[]scores=new int[]{60,70,80};
		int[]scores={60,70,80};
		for ( int i = 0; i < scores.length; i++) {
			System.out.println(scores[i]);
			
		}
				

	}

}

5.数组逆序:
package demo;

public class 数组逆序 {

	public static void main(String[] args) {
		int[] scores=new int[] {11,22,33,44,55};
	    int[] newscores=new int[5];
	    for (int i = 0; i < newscores.length; i++) {
			newscores[i]=scores[scores.length-i-1];
		}
	    for (int i = 0; i < newscores.length; i++) {
			System.out.println(newscores[i]);
		}
	}

}

6.递归算法:

package demo;


/*递归算法
 * 5的阶乘
 * 
 * 
 */
public class 递归算法 {
/**
 * 
 * @param args
 */
	public static void main(String[] args) {
		int result =jc(5) ;
		System.out.println(result);
   
	}
	/**
	 * 
	 * @param n 整型数值
	 * @return 返回值
	 */
    public static int jc(int n) {
    	if (n==1) {
			return 1;
		}
		return n*jc(n-1);
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值