一.方法

1.方法是完成特定功能的代码块。

修饰符  返回值类型  方法类型(参数类型  参数名1,参数类型  参数名2,...){

方法体语句;

return返回值;

}

修饰符:目前就用public static。

返回值类型:就是功能结果的数据类型。

方法名:符合命名规则即可。方便调用。

参数:

*形式参数:就是方法定义的,用于接收实际参数的。

*实际参数:就是实际参与运算的。

参数类型:就是参数的数据类型

参数名:就是变量名

方法体语句:就是完成功能的代码

return:结束方法的

返回值:就是功能的结果,由return带给调用者;返回值是什么类型,返回值类型就是该类型

2.有具体返回值

a.单独调用,一般来说没有意义,不推荐。

b.输出调用,但是不够好。因为我们可能需要针对结果进行进一步的操作。

c.赋值调用,推荐方案。

无具体返回值类型

a.单独调用

b.输出调用(错误)

c.赋值调用(错误)

3.

import java.util.Scanner;
class FunctionSum {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个数");
int a = sc.nextInt();
System.out.println("请输入第二个数");
int b = sc.nextInt();
int s = add(a,b);
System.out.println(s);
}
public static int add(int a, int b){
return a + b;
}
}
import java.util.Scanner;
class FunctionCompare {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个数:");
int x = sc.nextInt();
System.out.println("请输入第二个数:");
int y = sc.nextInt();
boolean b = isEquals(x,y);
System.out.println(b);
}
public static boolean isEquals(int a, int b){
return a == b;
}
}
import java.util.Scanner;

class Test1_Method {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个整数:");
int x = sc.nextInt();
System.out.println("请输入第二个整数:");
int y = sc.nextInt();
int m = getMax(x,y);
System.out.println("max = " + m);
boolean b = isEquals(x,y);
System.out.println(b);
}
public static int getMax(int a,int b){
return(a > b)? a : b;
}
}
import java.util.Scanner;

class Test2_Method {
public static void main(String[] args) { Scanner sc = new Scanner(System.in);
System.out.println("请输入行数:");
int m = sc.nextInt();
System.out.println("请输入列数:");
int n = sc.nextInt();
printRectangle(m,n); }
public static void printRectangle(int a,int b){ for (int i = 1;i <= a ;i++ ) {
for (int j = 1;j <= b ;j++ ) {
System.out.print("* ");
}
System.out.println();
}
return;
} }
import java.util.Scanner;

class FunctionMultiplication {

    public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入n:");
int n = sc.nextInt();
multiplication(n); } public static void multiplication(int n){
for (int i = 1;i <= n ;i++ ) {
for (int j = 1;j <= i ;j++ ) {
System.out.print(j + "*" + i + "=" +i*j + "\t");
}
System.out.println();
}
}
}

4.在同一个类中,方法名相同,参数列表不同。与返回值类型无关。

import java.util.Scanner;

class Test_Overload {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个整数:");
int x = sc.nextInt();
System.out.println("请输入第二个整数:");
int y = sc.nextInt();
boolean b1 = isEquals(x,y);
System.out.println(b1);
System.out.println("请输入第一个小数:");
double m = sc.nextDouble();
System.out.println("请输入第二个小数:");
double n = sc.nextDouble();
boolean b2 = isEquals(m,n);
System.out.println(b2);
}
public static boolean isEquals(int a,int b){
return a == b;
}
public static boolean isEquals(double a,double b){
return a == b;
}
}

三.数组

1.数组是存储同一种数据类型多个元素的集合。也可以看成一个容器。

数组既可以存储基本数据类型,也可以存储引用数据类型。

2.数据类型[] 数组名 = new 数据类型[数组的长度];

数据类型[] 数组名 = {元素1,元素2,...}

3.ArrayIndexOutOfBoundsException:数组索引越界异常

NullPointerException:空指针异常

4.

class Test4_Array {
public static void main(String[] args) {
int[] arr = {1,2,3,4,5};
print(arr);
}
public static void print(int[] arr){
for (int i = 0;i <= arr.length ;i++ ) {
System.out.print(arr[i] + " ");
}
}
}
class Test5_Array {
public static void main(String[] args) {
int[] arr = {1,2,3,4,5};
int m1 = getMax(arr);
System.out.println(m1);
int m2 = getMin(arr);
System.out.println(m2);
}
public static int getMax(int[] arr){
int max = arr[0];
for (int i = 1;i < arr.length ;i++ ) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}
public static int getMin(int[] arr){
int min = arr[0];
for (int i = 1;i < arr.length ;i++ ) {
if (arr[i] < min) {
min = arr[i];
}
}
return min;
}
}
class Test6_Array {
public static void main(String[] args) {
int[] arr = {1,2,3,4,5};
reverseArray(arr);
print(arr);
}
public static void reverseArray(int[] arr){
for (int i = 0;i < arr.length/2 ;i++ ) {
int temp = arr[i];
arr[i] = arr[arr.length-1-i];
arr[arr.length-1-i] = temp;
}
}
public static void print(int[] arr){
for (int i = 0 ;i < arr.length ;i++ ) {
System.out.print(arr[i] + " ");
}
}
}
import java.util.Scanner;

class Test7_Array {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个1~7之间的整数:");
int i = sc.nextInt();
char week = getWeek(i);
System.out.println("星期" + week); }
public static char getWeek(int m){
char[] arr = {' ','一','二','三','四','五','六','日'};
return arr[m];
}
}
class Test2_Array {
public static void main(String[] args) {
int[] arr = {11,22,33,44,55};
int index = getIndex(arr,11);
System.out.println(index);
}
public static int getIndex(int[] arr,int value){
for (int i = 0;i < arr.length ;i++ ) {
if (arr[i] == value) {
return i;
}
}
return -1;
}
}
class Test8_Array {
public static void main(String[] args) {
int[][] arr = {{1,2,3},{4,5,6},{7,8,9}};
print(arr);
}
public static void print(int[] arr){
for (int i = 0;i < arr.length ;i++ ) {
for (int j = 0;j < arr[i].length ;j++ ) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
}
class Test9_Array {
public static void main(String[] args) {
int[][] arr = {{22,66,44},{77,33,88},{25,45,65},{11,66,99}};
int sum = 0;
for (int i = 0;i < arr.length ;i++ ) {
for (int j = 0;j < arr[i].length ;j++ ) {
sum = sum + arr[i][j];
}
}
System.out.println(sum);
}
}

7.基本数据类型的值传递,不改变原值,因为方法调用后就会弹栈,而局部变量随之消失

引用数据类型的值传递,改变原值,因为即使方法调用后弹栈,但是堆内存中的数组对象还在,可以通过地址继续访问。

day06作业的更多相关文章

  1. python day06 作业答案

    1. count=1 while count<11: fen=input('请第{}个评委打分' .format( count)) if int(fen) >5 and int(fen) ...

  2. day06作业---字典循环

    '''1.1使⽤循环打印以下效果: ***************''' for a in range(1,6): print(a*'*') '''1.2: ***** **** *** ** * ' ...

  3. day06 作业

    猜年龄游戏 ''' 1. 给定年龄,用户可以猜三次年龄 2. 年龄猜对,让用户选择两次奖励 3. 用户选择两次奖励后可以退出 ''' import random age = random.randin ...

  4. python 作业

    Linux day01 计算机硬件知识整理 作业要求:整理博客,内容如下 编程语言的作用及与操作系统和硬件的关系 应用程序->操作系统->硬件 cpu->内存->磁盘 cpu与 ...

  5. DSB

    Linux day01 计算机硬件知识整理 作业要求:整理博客,内容如下 编程语言的作用及与操作系统和硬件的关系 应用程序->操作系统->硬件 cpu->内存->磁盘 cpu与 ...

  6. 老男孩Python全栈第2期+课件笔记【高清完整92天整套视频教程】

    点击了解更多Python课程>>> 老男孩Python全栈第2期+课件笔记[高清完整92天整套视频教程] 课程目录 ├─day01-python 全栈开发-基础篇 │ 01 pyth ...

  7. day05对象和类

    day06作业: 第一题:分析以下需求,并用代码实现 手机类Phone 属性: 品牌brand 价格price 行为: 打电话call() 发短信sendMessage() 玩游戏playGame() ...

  8. day06 再谈编码 and 作业讲解

    1. 小数据池,(其他语言又叫常量池) id() 查看变量的内存地址 is和== is 判断内存地址是否一致 == 判断内容是否一致 小数据池的作用: 为了快速的创建字符串对象, 可以减少内存的浪费 ...

  9. python开发学习-day06(模块拾忆、面向对象)

    s12-20160130-day06 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: ...

随机推荐

  1. 安装logstash5.4.1,并使用grok表达式收集nginx日志

    关于收集日志的方式,最简单性能最好的应该是修改nginx的日志存储格式为json,然后直接采集就可以了. 但是实际上会有一个问题,就是如果你之前有很多旧的日志需要全部导入elk上查看,这时就有两个问题 ...

  2. .NET MVC 获取 当前请求的 控制器/视图/区域 的名字

    .NET MVC 在action中或过滤器中或视图中,分别如何获取  当前请求的  控制器/视图/区域  的名字 1)过滤器中的: public class CMSAttribute : Filter ...

  3. 音视频处理之FFmpeg+SDL视频播放器20180409

    一.FFmpeg视频解码器 1.视频解码知识 1).纯净的视频解码流程 压缩编码数据->像素数据. 例如解码H.264,就是“H.264码流->YUV”. 2).一般的视频解码流程 视频码 ...

  4. git<git rebase 修改以前提交过的内容>

      git rebase 使用总结: 使用git rebase 修改以前已经提交的内容 比如要修改之前的commit的 hashcode为:187f869c9d54c9297d6b0b1b4ff47d ...

  5. 支付宝当面付功能demo运行解读

    下载java版本的sdk的demo: 然后拷入idea中: 准备工作: (1)验签工具下载:蚂蚁金服上面下载: https://openclub.alipay.com/read.php?tid=955 ...

  6. 一个最简单的使用Entity Framework 查询SQL 数据库的例子

    1.ADO.NET 3.5 Entity Framework是随着.net framework 3.5一起发布的,确认开发环境版本是大于等于3.5版本 2.确认已经安装了ADO.NET 3.5 Ent ...

  7. Docker Swarm 服务编排之命令

    一.简介 Docker有个编排工具docker-compose,可以将组成某个应该的多个docker容器编排在一起,同时管理.同样在Swarm集群中,可以使用docker stack 将一组相关联的服 ...

  8. Ubuntu 18.04设置dns

    最近使用了最新版的ubuntu 18.04运行一些服务,然后发现服务器经常出现网络不通的情况,主要是一些域名无法解析. 检查/etc/resolv.conf,发现之前修改的nameserver总是会被 ...

  9. P3155 [CQOI2009]叶子的染色

    P3155 [CQOI2009]叶子的染色 题目描述 给一棵m个结点的无根树,你可以选择一个度数大于1的结点作为根,然后给一些结点(根.内部结点和叶子均可)着以黑色或白色.你的着色方案应该保证根结点到 ...

  10. 科学计算三维可视化---Mayavi入门(Mayavi介绍和安装)

    Mayavi介绍 是基于VTK开发的可视化软件(更加高效),Mayavi完全由python编写,方便使用,而且可以使用python编写扩展,嵌入到用户程序中 安装要求 VTK >pip3 ins ...