Java入门总结第五天
1. 方法
//import Test1;
public class FirstDemo{
/**
1.什么是方法?
方法是执行一串代码的集合;
2.为什么要使用方法?
1.封装 2.方便简洁;
3.方法定义的语法:
修饰符 返回值类型 方法名([参数列表]){
//方法体
[return 返回值;]
}
4.使用方法
方法定义是不会执行的;
通过调用才会执行
*/
//使用方法求任意三个整数中最大的数
public static int getMax(int a,int b,int c){
//业务的处理
System.out.println("a="+a+"\t"+"b="+b+"\t"+"c="+c);
if(a>b&&a>c){
return a;//return语句以下代码将不会执行
}else if(b>c){
return b;
}else{
return c;
}
}
//没有返回值
public static void sysHero(String hero){
//业务处理
System.out.println(hero);
}
//方法入口
public static void main(String[]args){
//1.调用方法 方法名(参数);
int Max=getMax(12,33,2);
System.out.println("最大数为:"+Max);
//2.调用方法 无返回值
sysHero("李白");
//3.实现一个登录操作 今天方法使用可以通过 类名.方法();
String str=Login.login("jie","123456");
System.out.println(str);
}
}
public class Login{
//该类只定义方法
//1.用户登录操作
public static String login(String username,String pwd){
//判断用户名密码是否正确
if(username.equals("jie")&&pwd.equals("123456")){
return "欢迎"+username+"登录";
}
else{
return "登录失败";
}
}
}
输出结果
练习题
1. 输出等腰三角形
2. 杨辉三角
3. 输出平行四边形
4. 计算面积
//主程序
import java.util.Scanner;
public class Test{
//方法入口
public static void main(String []args){
System.out.println("1.输出等腰三角形");
System.out.println("2.杨辉三角");
System.out.println("3.输出平行四边形");
System.out.println("4.计算面积");
//初始化一个键盘录入对象
System.out.print("请选择:");
Scanner input=new Scanner(System.in);
int a=input.nextInt();
if(a==1){
//1.输出等腰三角形
Test1.getTriangle();
}else if(a==2){
//2.杨辉三角
Test1.out();
}else if(a==3){
//输出平行四边形
Test1.outPara();
}else if(a==4){
//4.计算面积
Test2.getArea();
}
}
}
//1.等腰三角形
/**
*
***
*****
*******
*********
*/
import java.util.Scanner;
public class Test1{
public static void getTriangle(){
//初始化一个键盘录入对象
System.out.print("请输入几行:");
Scanner input=new Scanner(System.in);
int a=input.nextInt();
for(int row=0;row<a;row++){
for(int col=0;col<a-row;col++){
System.out.print(" ");
if(col+1==a-row){
for(int coll=0;coll<2*row+1;coll++){
System.out.print("*");
}
}
}
System.out.println();
}
}
public static void out(){
System.out.print("请输入几行:");
Scanner input=new Scanner(System.in);
int b=input.nextInt();
int num[][]=new int[b][b];
//循环输出杨辉三角
//外层循环控制行数
for(int row=0;row<b;row++){
//内层循环控制列数
for(int col=0;col<=row;col++){
if(row>1&&col>0&&col<row){//提取不为值1的行数和列数 num[row][col]=num[row-1][col-1]+num[row-1][col];//计算数值
}
else{
num[row][col]=1;
}
System.out.print(num[row][col]+"\t");
}
System.out.println();
}
}
public static void outPara(){
System.out.print("请输入几行:");
Scanner input=new Scanner(System.in);
int c=input.nextInt();
for(int row=0;row<c;row++){
for(int col=0;col<c-row;col++){
System.out.print(" ");
if(col-(c-1-row)==0){
System.out.print("*****");
}
}
System.out.println();
}
}
}
import java.util.Scanner;
public class Test2{
public static void getArea(){
System.out.println("1.计算圆形面积");
System.out.println("2.计算矩形面积");
System.out.println("3.计算三角形面积");
//初始化一个键盘录入对象
System.out.print("请选择:");
Scanner input=new Scanner(System.in);
int a=input.nextInt();
if(a==1){
System.out.print("请输入半径");
double r=input.nextDouble();
double PI=3.14;
double area=PI*r*r;
System.out.println("圆的面积为:"+area);
}else if(a==2){
System.out.print("请输入长:");
int l=input.nextInt();
System.out.print("请输入宽:");
int w=input.nextInt();
int area=l*w;
System.out.println("矩形的面积为:"+area);
}else if(a==3){
System.out.print("请输入底:");
double d=input.nextInt();
System.out.print("请输入高:");
double h=input.nextInt();
double area=d*h/2;
System.out.println("三角形的面积为:"+area);
}
}
}
输出结果
1.等腰三角形
2.杨辉三角
3.平行四边形
4.计算面积
//圆形
//矩形
//三角形
2. 集合
import java.util.*;
public class SecondDemo{
/**
集合: 有序
//1. 增、删、查、改
*/
//方法入口
public static void operationList(ArrayList<String>list){
//2.添加数据
list.add("98k");//在最后添加一条数据
list.add("awm");
list.add("999");
list.add("666");
list.add("98k");
//在指定位置添加 下标从0开始
list.add(4,"平底锅");
//修改数据 按下标修改
list.set(0,"吃鸡");
//通过元素找下标
System.out.println(list.indexOf("999"));
System.out.println(list.lastIndexOf("666"));
//通过下标找元素
System.out.println(list.get(1));
//删除元素 通过下标删除 通过元素删除 全部删除
list.remove(1);
list.remove("98k");//删除的是相同元素的第一个元素
//判断当前list中是否包含某一个元素
System.out.println(list.contains("666"));
}
//方法入口
public static void main(String[]args){
//1.初始化一个集合ArrayList对象 必须指定集合中存放的数据类型
ArrayList<String>list=new ArrayList<>();
//调用list操作方法
operationList(list);
//3.查看所有元素
//将列表中的元素转化为数组
Object[]arr=list.toArray();
//查看数组中的所有元素
for(int i=0;i<arr.length;i++){
System.out.println((String)arr[i]);
}
}
}
输出结果
3. HashMap
import java.util.*;
public class ThirdDemo{
//方法入口
public static void main(String []args){
/**
HashMap
数据存储的特点是key-value键值对
*/
//1.初始化一个HashMap对象
HashMap<String,String>map=new HashMap<>();
//2.存放数据 key value
map.put("id","123");
map.put("name","jie");
map.put("age","19");
//修改某一个key对应的值
map.put("age","20");
//删除某一个key
map.remove("age");
//3.取值 通过key获取value值
System.out.println(map.get("id"));
System.out.println(map.get("name"));
System.out.println(map.get("age"));
}
}
输出结果
4. 知识框架