学习总结2(java基础)

来源:互联网 发布:手机小说编辑软件 编辑:程序博客网 时间:2024/05/01 23:04
1:for循环(掌握)
(1)格式:
for(初始化表达式; 控制条件表达式; 增量表达式) {
循环体语句;
}


执行流程:
A:先执行初始化表达式
B:执行控制条件表达式
C:看B的返回值:
true:执行循环体语句和增量表达式,回到B继续。
false:就结束for循环。
(2)for循环的注意事项:
A:注意格式问题。
B:分号和大括号的问题。
C:大括号建议不要省略。
(3)案例:

A:重复内容输出多次。

class ForDemo{
public static void main(String[]args){
for (int x=0;x<5;x++ ){
System.out.println("HelloWorld");
}
}
}

B:输出1-10或者10-1的数据。

class ForDemo
{
public static void main(String[]args)
{
for (int x=1;x<=10;x++)

{
System.out.println(x);
}
System.out.println("-----------------");
for (int x=10;x>0;x--)

{
System.out.println(x);
}
}

C:求1-10的和。

class ForDemo

{
public static void main(String[]args)

{
int x ;
int sum=0;
for (x=1; x<=10;x++ )

{
//x--1.2.3.4,,,,
//sum=x+sum
sum+=x;
}
System.out.println(sum);
}
}

D:求5的阶乘。

class ForDemo

{
public static void main(String[]args)

{
int result=1;
for (int x=1;x<=5 ; x++)

{
result*=x;
}
System.out.println("1-5的阶乘="+result);
}
}

E:求1-100的偶数或者奇数和。

class ForDemo

{
public static void main(String[]args)

{
//方式1
int sum=0;
for (int x=1;x<=100;x++ ){
if (x%2==0){
sum+=x;
}
}
System.out.println(sum);
// 方式2
int sum2=0;
for (int x=0;x<=100 ;x+=2 ){
sum2+=x ;
}
System.out.println("1-100的偶数和="+sum2);
//奇数的求和
int sum3=0;
for (int x=1;x<=100 ;x+=2 ){
sum3+=x ;
}
System.out.println("1-100的奇数和="+sum3);
}
}

F:水仙花数。

class ForDemo

{
public static void main (String[]args){
for (int x=100;x<1000 ;x++ ){
int ge=x%10;
int shi=x/10%10;
int bai =x/100%10;
if (x==bai*bai*bai+shi*shi*shi+ge*ge*ge){

System.out.println(x);
}

}
}

G:统计水仙花数有多少个。

class ForDemo

{
public static void main(String[]args){
//定义统计数据
int count=0;
//通过for循环获取范围的数据
for (int x=100;x<1000 ;x++ ){
int ge=x%10;
int shi=x/10%10;
int bai =x/100%10;
//进行判断
if (x==bai*bai*bai+shi*shi*shi+ge*ge*ge){

count++;
}

System.out.println(count);
}
}


2:while和do...while循环(掌握)
(1)while:
基本格式:
while(条件表达式) {
语句;
}


扩展格式:
初始化表达式;
while(控制条件表达式) {
循环体语句;
增量表达式
}


执行流程:
A:先执行初始化表达式
B:执行控制条件表达式
C:看B的返回值:
true:执行循环体语句和增量表达式,回到B继续。
false:就结束while循环。


注意:
while循环和for循环是可以等价转换的。


区别:
增量如果还要继续使用,就用while循环。否则就用for循环。


使用场景:
A:是一个明确的范围,用for。
B:次数不明确,用while循环。
案例:

珠穆朗玛峰的案例。

class WhileDemo{
public static void main(String[]args){
//定义统计变量
int count =0;
//定义初始化值
double start =0.01;
double end =8848;
//判断
while (start<end){
//统计次数
count++;
start*=2;
}
System.out.println(count);


// for循环改进
int count2=0;
for (double x=0.01,y=8848;x<y ;x*=2 )
{
count2++;
}
System.out.println(count2);
}
}

(2)do...while:
基本格式:
do {
语句;
}while(条件表达式);


扩展格式:
初始化表达式;
do {
循环体语句;
增量表达式
}while(控制条件表达式);

执行流程:
A:先执行初始化表达式
B:执行循环体语句和增量表达式
C:执行控制条件表达式
D:看C的返回值:
true:回到B继续。
false:就结束do...while循环循环。


和其他两种循环的区别:
do...while循环至少执行一次循环体。


3:循环嵌套(重要)
(1)这两天作业的格式:
if语句
switch语句
for语句
while语句


for里面嵌套if判断
for里面嵌套for循环
(2)案例:
A:一个4行5列的矩形*。
*****
*****
*****
*****
B:一个正三角形
*
**
***
****
*****
C:九九乘法表(与正三角形案例相仿
for(int x=1; x<=9; x++) {
for(int y=1; y<=x; y++) {
System.out.print(y+"*"+x+"="+x*y+"\t");
}
System.out.println();
}


4:continue和break(掌握)
(1)作用:
用于控制循环语句跳转的。
(2)在哪里使用呢?
不能单独使用。
continue必须在循环中。
break可以在循环和switch语句中。
(3)区别:
continue:结束本次循环,进入下一次循环。
break:结束当前所有循环。
(4)带标号的跳转
可以退出多层循环。


5:函数( 重中之重)
(1)函数:是完成特定功能的代码块。
(2)函数的格式:
修饰符  返回值类型  函数名(数据类型  变量1,数据类型  变量2,... ...)
{
函数体;
return 返回值;     
}
(3)函数的特点:
A:函数只有被调用才执行。
B:函数间是平级的关系,不能嵌套定义。
(4)写一个函数:两个明确
A:返回值类型
B:参数列表
(5)函数调用
A:非void类型的函数
a:单独调用,没有意义。
b:输出调用,不够好。
c:赋值调用,推荐方案。
B:void类型的函数
单独调用。
(6)案例:
A:获取三个数中的最大值。(这个案例很经典)

import java.util.Scanner;
class FunctionDemo
{
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();
System.out.println("请输入第三个数据");
int c =sc.nextInt();

int max =getMax (a,b,c);
System.out.println(max);
}
/*
明确:
返回值类型:int类型
参数列表:int a,int b ,int c 


*/
public static int getMax (int a,int b ,int c)
{
/*if (a>b)
{
if (a>c )
{
return a;
}else 
{
return c;
}
}
else 
{
if (b>c)
{
return b;
}
else 
{
return c;
}
}*/
//改进
/*if (a>b)
{
return (a>c?a:c);
}
else
{
return (b>c?b:c);
}
*/
//最终改进三元运算符的嵌套使用


//return (a>b)?(a>c?a:c):(b>c?b:c);

int temp=(a>b)?a:b;
int max =(temp>c)?temp:c;
return max;
}
}


B:判断两个数是否相等。

public static boolean compare(int a, int b)
{
//boolean flag=(a==b)?true:false;
//return flag;


//改进型
//boolean flag=(a==b);
//return flag;

//最终型
return a==b; 


C:求和案例。

public static int sum (int a,int b )
{
int result =a+b;
return result;
}
D:nn乘法表。

public static void printNN(int n)
{
for (int x=1;x<=n ;x++ )
{
for (int y=1;y<=x ;y++)
{
System.out.print(y+"*"+x+"="+y*x+"\t");
}
System.out.println();
}
}


(7)函数重载
函数名相同,参数列表不同。(个数和类型)
与返回值类型无关。


6:数组(一维数组 重中之重)
(1)数组:存储同一种类型的多个元素的容器。
(2)特点:
每一个元素都有编号,(索引)。方便我们获取数组中的元素的。
从0开始编号。数组的最大索引是长度-1。
(3)定义格式:
A:int[] arr = new int[3];
B:int arr[] = new int[3];
C:int[] arr = new int[]{1,2,3};
D:int[] arr = {1,2,3};
(4)常见操作:
数组的长度:数组名.length


A:遍历数组
public static void printArray(int[] arr) {
for(int x=0; x<arr.length; x++) {
System.out.println(arr[x]);
}
}


10
20
30


//改进版(为了让输出格式更叫好看
public static void printArray(int[] arr) {
for(int x=0; x<arr.length; x++) {
if(x==arr.length-1) {
System.out.println(arr[x]);
}else {
System.out.print(arr[x]+", ");
}
}
}


10, 20, 30


B:获取最值
最大值


public static int getMax(int[] arr) {
int max = arr[0];
for(int x=1; x<arr.length; x++) {
if(arr[x] > max) {
max = arr[x];
}
}
return max;
}


最小值


public static int getMin(int[] arr) {
int min = arr[0];
for(int x=1; x<arr.length; x++) {
if(arr[x] < min) {
min = arr[x];
}
}
return min;
}



C:数组的逆序(将元素对调)
//方式1
public static void change(int[] arr) {
for(int start=0,end=arr.length-1; start<=end; start++,end--) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
}
}


//方式2
public static void change(int[] arr) {
for(int x=0; x<arr.length/2; x++) {
int temp = arr[x];
arr[x] = arr[arr.length-1-x];
arr[arr.length-1-x] = temp;
}
}


D:查表法
根据给定的数据,输出对应的星期。


String[] strArray = {"星期一","星期二",...};


Scanner sc = new Scanner(System.in);
int week = sc.nextInt();


System.out.println(strArray[week-1]);


E:普通查找
//方式1
public static int getIndex(int[] arr,int value) {
for(int x=0; x<arr.length; x++) {
if(arr[x] == value) {
return x;
}
}
return -1;
}


//方式2
public static int getIndex(int[] arr,int value) {
int index = - 1;
for(int x=0; x<arr.length; x++) {
if(arr[x] == value) {
index = x;
break;
}
}
return index;
}


0 0
原创粉丝点击