您的位置:首页 > 编程语言 > Java开发

JAVA基础3-程序结构与控制、函数与数组入门

2015-11-01 12:11 741 查看
一、程序结构

 

1、顺序结构
2、选择结构
3、循环结构
二、顺序结构
程序至上而下逐行执行,一条语句执行完之后继续执行下一条语句,一直到程序的末尾

 

三、条件选择结构

 
选择结构是根据条件的成立与否,再决定要执行哪些语句的一种结构
 


3.1 、IF语句--单分支结构

if (判断条件){
语句1 ;
 

</pre><div class="cnblogs_code" style="margin: 5px 0px; padding: 5px; border: 1px solid rgb(204, 204, 204); overflow: auto; font-family: 'Courier New' !important; font-size: 12px !important; background-color: rgb(245, 245, 245);"><div class="cnblogs_code_toolbar" style="margin: 5px 0px 0px; padding: 0px;"></div><pre style="margin-top: 0px; margin-bottom: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: 'Courier New' !important;"><span style="margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 0, 255);">package</span><span style="margin: 0px; padding: 0px; line-height: 1.5;"> com.day03;
</span><span style="margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 128, 0);">/**</span><span style="margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 128, 0);">
* 比较2个数的大小
* </span><span style="margin: 0px; padding: 0px; line-height: 1.5; color: rgb(128, 128, 128);">@author</span><span style="margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 128, 0);"> Administrator
*
</span><span style="margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 128, 0);">*/</span>
<span style="margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 0, 255);">public</span> <span style="margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 0, 255);">class</span><span style="margin: 0px; padding: 0px; line-height: 1.5;"> IFTest1 {

</span><span style="margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 0, 255);">public</span> <span style="margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 0, 255);">static</span> <span style="margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 0, 255);">void</span><span style="margin: 0px; padding: 0px; line-height: 1.5;"> main(String[] args) {
</span><span style="margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 0, 255);">int</span> x=3<span style="margin: 0px; padding: 0px; line-height: 1.5;">;
</span><span style="margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 0, 255);">int</span> y=10<span style="margin: 0px; padding: 0px; line-height: 1.5;">;
System.out.println(</span>"====开始比较====="<span style="margin: 0px; padding: 0px; line-height: 1.5;">);
</span><span style="margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 0, 255);">if</span>(x><span style="margin: 0px; padding: 0px; line-height: 1.5;">y){
System.out.println(</span>"x比y大!"<span style="margin: 0px; padding: 0px; line-height: 1.5;">);
}
</span><span style="margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 0, 255);">if</span>(x<<span style="margin: 0px; padding: 0px; line-height: 1.5;">y){
System.out.println(</span>"x比y小!"<span style="margin: 0px; padding: 0px; line-height: 1.5;">);
}
</span><span style="margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 0, 255);">if</span>(x==<span style="margin: 0px; padding: 0px; line-height: 1.5;">y){
System.out.println(</span>"x等于y!"<span style="margin: 0px; padding: 0px; line-height: 1.5;">);
}
System.out.println(</span>"====比较完成!====="<span style="margin: 0px; padding: 0px; line-height: 1.5;">);
}

}</span>


</pre><p style="margin: 10px auto; padding-top: 0px; padding-bottom: 0px; line-height: 1.5;"> </p><pre name="code" class="java" style="margin-top: 0px; margin-bottom: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word;">


3.2、if...else语句--双分支结构

if (判断条件){
语句主体1 ;
}else{
语句主体2;
}
 
</pre><div class="cnblogs_code" style="margin: 5px 0px; padding: 5px; border: 1px solid rgb(204, 204, 204); overflow: auto; font-family: 'Courier New' !important; font-size: 12px !important; background-color: rgb(245, 245, 245);"><div class="cnblogs_code_toolbar" style="margin: 5px 0px 0px; padding: 0px;"></div><pre style="margin-top: 0px; margin-bottom: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: 'Courier New' !important;">
<span style="margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 0, 255);">package</span><span style="margin: 0px; padding: 0px; line-height: 1.5;"> com.day03;

</span><span style="margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 128, 0);">/**</span><span style="margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 128, 0);">
* 判断一个数是奇数还是偶数
*
* </span><span style="margin: 0px; padding: 0px; line-height: 1.5; color: rgb(128, 128, 128);">@author</span><span style="margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 128, 0);"> Administrator
*
</span><span style="margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 128, 0);">*/</span>
<span style="margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 0, 255);">public</span> <span style="margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 0, 255);">class</span><span style="margin: 0px; padding: 0px; line-height: 1.5;"> IFELSETest {

</span><span style="margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 0, 255);">public</span> <span style="margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 0, 255);">static</span> <span style="margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 0, 255);">void</span><span style="margin: 0px; padding: 0px; line-height: 1.5;"> main(String[] args) {

</span><span style="margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 0, 255);">int</span> x = 3<span style="margin: 0px; padding: 0px; line-height: 1.5;">;
</span><span style="margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 0, 255);">if</span> (x % 2 == 0<span style="margin: 0px; padding: 0px; line-height: 1.5;">) {
System.out.println(x </span>+ "是偶数!"<span style="margin: 0px; padding: 0px; line-height: 1.5;">);
} </span><span style="margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 0, 255);">else</span><span style="margin: 0px; padding: 0px; line-height: 1.5;"> {
System.out.println(x </span>+ "是奇数!"<span style="margin: 0px; padding: 0px; line-height: 1.5;">);
}

}

}</span>


</pre><p style="margin: 10px auto; padding-top: 0px; padding-bottom: 0px; line-height: 1.5;"> </p><pre name="code" class="java" style="margin-top: 0px; margin-bottom: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word;">


3.3、if…else if…else语句 --多分支结构

如果需要在if..else里判断多个条件时,就需要if..else if … else语句了,其格式如下:
if (条件判断1){
语句主体1 ;
}else if (条件判断2){
语句主体 2 ;
}
… // 多个else if()语句
else{
语句主体3 ;
}

package pb.test;

public class test6 {

public static void main(String[] args) {
int x=3;
if(x==1){
System.out.println("x的值是1!");
}else if(x==2){
System.out.println("x的值是2!");
}else if(x==3){
System.out.println("x的值是3!");
}else{
System.out.println("x的值不是1,2,3 中的一个!");
}

}

}


 

四、Switch结构
 

要在许多的选择条件中找到并执行其中一个符合判断条件的语句时,除了可以使用if..else不断地判断之外,也可以使用另一种更方便的方式即多重选择——switch语句,语法格式:

switch (表达式){
case 选择值1 : 语句主体 1 ;        break ;
case 选择值2 : 语句主体 2 ;                 break ;
…….
case 选择值n : 语句主体 n ;                break ; default:  语句主体 ;
}

 
4.1、正常运行的Switch

package com;

import java.util.Scanner;

/**
* 根据用户指定的朋份,打印该月份所属的季节
*                 12,1,2冬
* 2,3,4春
* 5,6,7夏季
* 9,10,11秋季
*/
public class SwitchTest {

public static void main(String[] args) {
//定义扫描器变量
Scanner scanner=new Scanner(System.in);
//定义变量接收
int num=scanner.nextInt();
/**
* 数值类型的结果,并不是很多时用switch
*/
switch(num){  //byte short int char只接收4种类型  JDK7以上switch可以判断字符串
case 3:
case        4:
case 5:
System.out.println(num+"春季");
break;
case 6:
case        7:
case 8:
System.out.println(num+"夏季");
break;
case 9:
case        10:
case 11:
System.out.println(num+"秋季");
break;
case 12:
case        1:
case 2:
System.out.println(num+"冬季");
break;
default:
System.out.println("输入 错误,请输入1--12之间的数字");
}

}

}


</pre><p style="margin: 10px auto; padding-top: 0px; padding-bottom: 0px; line-height: 1.5;"> </p><pre name="code" class="java" style="margin-top: 0px; margin-bottom: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word;">

4.2、没有Break的Switch

package com;

/**
* 根据操作符做相应的操作
*
* @author Denny
*
*/
public class SwitchTest2 {

public static void main(String[] args) {
int a = 10;
int b = 3;
char c = '+';

switch (c) {
default:
System.out.println("未知操作符");
case '+':
System.out.println("a+b=" + (a + b));
case '-':
System.out.println("a-b=" + (a - b));
case '*':
System.out.println("a*b=" + (a * b));
case '/':
System.out.println("a/b=" + (a / b));

}

}

}


</pre><p style="margin: 10px auto; padding-top: 0px; padding-bottom: 0px; line-height: 1.5;"> </p><pre name="code" class="java" style="margin-top: 0px; margin-bottom: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word;">

五、循环while和do..while
 
5.1、While循环
 

package com.day03;
/**
* 使用While进行100以内累加操作
* @author Administrator
*
*/
public class WhileTest {

public static void main(String[] args) {

int i=1;  //变量
int sum=0; //和

while(i<=100){//先判断条件
sum+=i;
i++;
}
System.out.println("100以内整数之和:"+sum);

}

}


</pre><p style="margin: 10px auto; padding-top: 0px; padding-bottom: 0px; line-height: 1.5;"> </p><pre name="code" class="java" style="margin-top: 0px; margin-bottom: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word;">

5.2 do...while
 

package com.day03;

/**
* 使用do...While进行100以内累加操作
*
* @author Administrator
*
*/
public class DoWhileTest {

public static void main(String[] args) {
int i = 1; // 变量
int sum = 0; // 和

do {
sum += i;
i++;

} while (i <= 100);

System.out.println("100以内整数之和:" + sum);
}

}


 

5.3、while和do..while区别
 

package com.db1;

public class WhileDemo {

public static void main(String[] args) {
//while 判断条件可能一次也不执行
int x=1;

while(x<3);
{
System.out.println("x="+x);
x++;
}

//do while 最少执行一次
int y=1;

do{
System.out.println("do:y="+y);
y++;
}while(y<3);

}

}


</pre><p style="margin: 10px auto; padding-top: 0px; padding-bottom: 0px; line-height: 1.5;"> </p><pre name="code" class="java" style="margin-top: 0px; margin-bottom: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word;">

六、for循环
 

6.1、for循环基本结构
 
for (赋值初值;判断条件;赋值增减量){
语句1 ;
….
语句n ;
}
 
6.2、使用for进行100以内累加操作

package com.day03;
/**
* 使用for进行100以内累加操作
* @author Administrator
*
*/
public class ForTest {

public static void main(String[] args) {
int sum=0;
for (int i = 0; i <=100; i++) {
sum+=i;
}
System.out.println("100以内整数之和:" + sum);
}

}


 

6.3、打印99乖法表

双循环打印
package com.db1;

/**
* 打印99乖法表
*
* @author denny
*
*/
public class Demo7 {

public static void main(String[] args) {
//外层循环控制行数
for(int i=1;i<=9;i++){
//内层循环控制列
for (int j = 1; j <=i; j++) {
System.out.print(j+"*"+i+"="+(j*j)+"  ");
}
//换行
System.out.println();
}

}

}


 

一个循环打印

package com.db1;

/**
* 打印99乖法表
*
* @author denny 一个循环打印
*/
public class Demo7_1 {

public static void main(String[] args) {
//一个循环
for (int i = 1, j=1;i <=9; i++) {
//输出
System.out.print(i+"*"+j+"="+i*j+"  ");
if(i==j){
i=0;
j++;
System.out.println();
}
//当j>9是终止
if(j>9){
break;
}

}

}

}


</pre><p style="margin: 10px auto; padding-top: 0px; padding-bottom: 0px; line-height: 1.5;"> </p><pre name="code" class="java" style="margin-top: 0px; margin-bottom: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word;">

另类写法

package com.day03;

public class ForTest2 {

public static void main(String[] args) {

int x=1;
for(System.out.println("a");x<3;System.out.println("c")){
System.out.println("d");
x++;
}
//结果adcdc
}

}


</pre><p style="margin: 10px auto; padding-top: 0px; padding-bottom: 0px; line-height: 1.5;"> </p><pre name="code" class="java" style="margin-top: 0px; margin-bottom: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word;">
打印菱形

package com.day03;
/**
* 打印菱形
* @author Denny
* 思路:使用for和循环
* 1.先打印正三角,
* 2.再打印倒三角
*
*/
public class ForTest7 {

public static void main(String[] args) {
/*
* 打印三角形
*/
for(int x=0;x<5;x++){

//打印空格
for(int y=x+1;y<5;y++){
System.out.print(" ");
}
//打印三角形
for(int z=0;z<=x;z++){
System.out.print("* ");
}
//换行
System.out.println();

}
//倒三角
for(int x=4;x>0;x--){

//打印空格
for(int y=x;y<=4;y++){
System.out.print(" ");
}
//打印三角形
for(int z=0;z<x;z++){
System.out.print("* ");
}
//换行
System.out.println();

}

}

}


</pre><p style="margin: 10px auto; padding-top: 0px; padding-bottom: 0px; line-height: 1.5;"> </p><pre name="code" class="java" style="margin-top: 0px; margin-bottom: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word;">


七、break、continue、return
 
7.1、break
结果最近的和循环体,执行和循环体后的代码

package com.day03;
/**
* 使用for和循环验证break
* @author Administrator
*
*/
public class BreakDemo {

public static void main(String[] args) {

for(int i=0;i<10;i++){
if(i==5){
break;//结束最近的和循环体 ,执行这个和循环体后面的代码
}
System.out.print(i+" ");

}

System.out.println("程序结束");
}

}


 
结果:
<span style="margin: 0px; padding: 0px; line-height: 1.5; font-family: 'Microsoft YaHei'; font-size: 14px;">0 1 2 3 4 程序结束</span>


 
7.2、continue
跳过本次和循环,进行下次和循环

package com.day03;
/**
* 使用for和循环验证continue
* @author Administrator
*
*/
public class ContinueDemo {

public static void main(String[] args) {

for(int i=0;i<10;i++){
if(i==5){
continue;  //跳过本次和循环执行下次和循环
}
System.out.print(i+" ");

}

System.out.println("程序结束");

}

}


</pre><p style="margin: 10px auto; padding-top: 0px; padding-bottom: 0px; line-height: 1.5;"> </p><pre name="code" class="java" style="margin-top: 0px; margin-bottom: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word;">

结果:
<span style="margin: 0px; padding: 0px; line-height: 1.5; font-family: 'Microsoft YaHei'; font-size: 14px;">0 1 2 3 4 6 7 8 9 程序结束</span>


7.3、return
结果程序

/**
* 使用for和循环验证return
* @author Administrator
*
*/
public class ReturnDemo {

public static void main(String[] args) {
for(int i=0;i<10;i++){
if(i==5){
return;    //程序结束
}
System.out.print(i+" ");

}

System.out.println("程序结束");  //这里没有执行

}

}


</pre><p style="margin: 10px auto; padding-top: 0px; padding-bottom: 0px; line-height: 1.5;"> </p><pre name="code" class="java" style="margin-top: 0px; margin-bottom: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><span style="margin: 0px; padding: 0px; line-height: 1.5; font-family: 'Microsoft YaHei'; font-size: 14px;"> </span>
结果:
<span style="margin: 0px; padding: 0px; line-height: 1.5; font-family: 'Microsoft YaHei'; font-size: 14px;">0 1 2 3 4 </span>

八、函数(方法)
 
8.1 函数定义
函数就是定义在类中的具有特定功能的一段独立小程序,函数也称为方法。

    函数的格式:
    修饰符 返回值类型 函数名(参数类型 形式参数1,参数类型 形式参数2,...)
    {
          执行语句;
          return 返回值;
    }

    返回值类型:函数运行后的结果的数据类型。
    参数类型:是形式参数的数据类型。
    形式参数:是一个变量,用于存储调用函数时传递给函数的实际参数。
    实际参数:传递给形式参数的具体数值。
    return:用于结束函数。
    返回值:该函数运算后的结果,该结果会返回给调用者。
8.2、函数特点
1、定义函数可以将功能代码进行封装。
    2、便于对该功能进行复用。
    3、函数只有被调用才会被执行。
    4、函数的出现提高了代码的复用性。

    P.S.
    1、对于函数没有具体返回值的情况,返回值类型用关键字void表示,那么该函数中的return语句如果在最后一行可以省略不写,或者写上return;。
8.3、函数的应用
 

package com.day03;
/**
* 函数
* @author denny
*
*/
public class FunctionDemo {

public static void main(String[] args) {

String str="Hello";
getString(str);
}
public static void getString(String str){
System.out.println(str);
}

}


</pre><p style="margin: 10px auto; padding-top: 0px; padding-bottom: 0px; line-height: 1.5;"> </p><pre name="code" class="java" style="margin-top: 0px; margin-bottom: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word;">

8.4、函数的重载
重载的概念:
    在同一个类中,允许存在一个以上的同名函数,只要它们的参数个数或者参数类型不同即可和返回值类型无关
    重载的好处:
    代码复用提高,提高阅读性

package com.day03;
/**
* 函数
* @author denny
*
*/
public class FunctionDemo {

public static void main(String[] args) {
System.out.println(add(4,5));
System.out.println(add(4,5,6));
System.out.println(add(2.0,3.5));
}
//2个整数参数
public static int  add(int x, int y){
return x+y;
}
//3个整数参数
public static int  add(int x, int y,int z){
return x+y+z;
}
//2个double参数
public static double  add(double x, double y){
return x+y;
}

}


 



九、数组

9.1、什么数组
 
数组是一组相关数据的集合,一个数组实际上就是一连串的变量,
数组按照使用可以分为一维数组、二维数组、多维数组。同一数组中的变量数据类型相同
9.2、数组的优点
 

不使用数组定义100个整型变量:int i1;int i2;…int i100
使用数组定义:int i[100]
同一类型的多个变量可以使用数组来完成
9.3、数组的定义和格式
格式一:
元素类型[] 数组名=new 元素类型[元素个数或者数组长度];
示例:int [] arr=new int[5];
格式2:

元素类型[] 数组名=new 元素类型{元素1,元素2,...元素n};
示例:int [] arr=new int[5,3,6,8,9];

数组声明后实际上是在栈内存中保存了此数组的名称,接下来便是要在堆内存中配置数组所需的内存,其中“长度”是告诉编译器,所声明的数组要存放多少个元素,
而“new”则是命令编译器根据括号里的长度开辟空间。

9.4、内存分配和特点
int [] arr=new int[4];


 
9.5、常识
数组的下标是从0开始的,
最后的元素下标是[长度-1】
可以通过下标赋值:arr[0]=2,arr[3]=5..
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java基础