java基础2

一、引用数据类型

使用方法

数据类型  变量名  =  new 数据类型();

变量名.方法名();

1、 Scanner类

引用方法:

①导包:import java.util.Scanner;

②创建对象实例:Scanner sc = new Scanner(System.in);

③调用方法:

int  i = sc.nextInt(); 用来接收控制台录入的数字

String s = sc.next(); 用来接收控制台录入的字符串

2、随机数类Random

引用方法:

导包:import.java.util.Random ; 

创建实例格式:Random 变量名 = new Random();

注:

public int nextInt(int maxValue) 产生[0,maxValue)范围的随机整数,包含0,不包含maxValue;

public double nextDouble()  产生[0,1)范围的随机小数,包含0.0,不包含1.0。

二、控制语句

判断语句:

1、if语句:

if (条件语句){

执行语句;

……

}

2、if…else语句:

if (判断条件){

执行语句1

……

}else{

执行语句2

……

}

3、if…else if…else语句:

if (判断条件1) {

执行语句1

} else if (判断条件2) {

执行语句2

}

...

else if (判断条件n) {

执行语句n

} else {

执行语句n+1

}

引申:

if语句与三元运算转换

三元运算符会得到一个结果,通常用于对某个变量进行赋值,当判断条件成立时,运算结果为表达式1的值,否则结果为表达式2的值。

实例:

求两个数x、y中的较大者,如果用if…else语句来实现,具体代码如下:

复制代码
int x = 0;
int y = 1;
int max=0;
if (x > y) {
    max = x;
} else {
    max = y;
}
复制代码

用三元运算符代替:

int max = x > y ? x : y;

4、switch语句

switch 条件语句也是一种很常用的选择语句,它和if条件语句不同,它只能针对某个表达式的值作出判断,从而决定程序执行哪一段代码。

语法:

switch (表达式){

case 目标值1:

执行语句1

break;

case 目标值2:

执行语句2

break;

......

case 目标值n:

执行语句n

break;

default:

执行语句n+1

break;

}

在上面的格式中,switch语句将表达式的值与每个case中的目标值进行匹配。

如果找到了匹配的值,会执行对应case后的语句,如果没找到任何匹配的值,就会执行default后的语句。

实例:

要判断一周中的某一天是否为工作日

复制代码
public class SwitchDemo02 {
    public static void main(String[] args) {
        int week = 2;
        switch (week) {
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
            // 当 week 满足值 1、2、3、4、5 中任意一个时,处理方式相同
            System.out.println("今天是工作日");
            break;
        case 6:
        case 7:
            // 当 week 满足值 6、7 中任意一个时,处理方式相同
            System.out.println("今天是休息日");
            break;
        }
    }
}
复制代码

循环语句:

1、while循环

while循环语句和选择结构if语句有些相似,都是根据条件判断来决定是否执行大括号内的执行语句。

区别在于,while语句会反复地进行条件判断,只要条件成立,{}内的执行语句就会执行,直到条件不成立,while循环结束。

语法:

while(循环条件){

执行语句

………

}

实例:

打印1~4之间的自然数:

复制代码
public class WhileDemo {
    public static void main(String[] args) {
        int x = 1; // 定义变量x,初始值为1
        while (x <= 4) { // 循环条件
            System.out.println("x = " + x); // 条件成立,打印x的值
            x++; // x进行自增
        }
    }
}
复制代码

2、for循环

语法:

for(初始化表达式; 循环条件; 操作表达式){

执行语句

………

}

流程分析:

分别用①表示初始化表达式、②表示循环条件、③表示操作表达式、④表示循环体,通过序号来具体分析for循环的执行流程。

for(① ; ② ; ③){

}

第一步,执行①

第二步,执行②,如果判断结果为true,执行第三步,如果判断结果为false,执行第五步

第三步,执行④

第四步,执行③,然后重复执行第二步

第五步,退出循环

实例:

打印1~4之间的自然数:

复制代码
public class ForDemo01 {
    public static void main(String[] args) {
        int sum = 0; // 定义变量sum,用于记住累加的和
        for (int i = 1; i <= 4; i++) { // i的值会在1~4之间变化
            sum += i; // 实现sum与i的累加
        }
        System.out.println("sum = " + sum); // 打印累加的和
    }
}
复制代码

3、do…while循环

语法:

do {

执行语句

………

} while(循环条件);

与while区别:

循环体会无条件执行一次,然后再根据循环条件来决定是否继续执行。

实例:

打印1~4之间的自然数:

复制代码
public class DoWhileDemo {
    public static void main(String[] args) {
        int x = 1; // 定义变量x,初始值为1
        do {
            System.out.println("x = " + x); // 打印x的值
            x++; // 将x的值自增
        } while (x <= 4); // 循环条件
    }
}
复制代码

4、无限循环(死循环)

while(true){}

for(;;){}

循环嵌套

嵌套循环是指在一个循环语句的循环体中再定义一个循环语句的语法结构。

例:

复制代码
for(初始化表达式; 循环条件; 操作表达式) {
    ………
    for(初始化表达式; 循环条件; 操作表达式) {
        执行语句
        ………
    }
    ………
}
复制代码

练习:

循环打印

使用“*”打印直角三角形

复制代码
 1public class ForForDemo {
 2    public static void main(String[] args) {
 3        int i, j; // 定义两个循环变量
 4        for (i = 1; i <= 9; i++) { // 外层循环
 5            for (j = 1; j <= i; j++) { // 内层循环
 6                System.out.print("*"); // 打印*
 7            }
 8            System.out.print("
"); // 换行
 9        }
 10    }
 11}
复制代码

三、跳转语句

1、break

在switch条件语句和循环语句中都可以使用break语句。

当它出现在switch条件语句中时,作用是终止某个case并跳出switch结构。

当它出现在循环语句中,作用是跳出循环语句,执行后面的代码。

实例:

复制代码
public class BreakDemo {
    public static void main(String[] args) {
        int x = 1; // 定义变量x,初始值为1
        while (x <= 4) { // 循环条件
            System.out.println("x = " + x); // 条件成立,打印x的值
            if (x == 3) {
                break;
            }
            x++; // x进行自增
        }
    }
}
复制代码

2、标记:

当break语句出现在嵌套循环中的内层循环时,它只能跳出内层循环,如果想使用break语句跳出外层循环则需要对外层循环添加标记。

实例:

控制程序只打印4行“*”

复制代码
public class BreakDemo02 {
    public static void main(String[] args) {
        int i, j; // 定义两个循环变量
        aaa: for (i = 1; i <= 9; i++) { // 外层循环
            for (j = 1; j <= i; j++) { // 内层循环
                if (i > 4) { // 判断i的值是否大于4
                    break aaa; // 跳出外层循环
                }
                System.out.print("*"); // 打印*
            }
            System.out.print("
"); // 换行
        }
    }
}
复制代码

3、continue语句

continue语句用在循环语句中,它的作用是终止本次循环,执行下一次循环。

实例:

对1~100之内的奇数求和

复制代码
public class ContinueDemo {
    public static void main(String[] args) {
        int sum = 0; // 定义变量sum,用于记住和
        for (int i = 1; i <= 100; i++) {
            if (i % 2 == 0) { // i是一个偶数,不累加
                continue; // 结束本次循环
            }
            sum += i; // 实现sum和i的累加
        }
        System.out.println("sum = " + sum);
    }
}
复制代码

例题:

1、后台预先生成一个1-100之间的随机数,用户键盘录入猜数字

2、如果猜对了,打印“恭喜您,答对了”

3、如果猜错了

  猜大了:打印“sorry,您猜大了!”

  猜小了:打印“sorry,您猜小了!”

4、直到数字猜到为止

复制代码
import java.util.Random;
import java.util.Scanner;
class text01 
{
    public static void main(String[] args) 
    {
        Random r=new Random();
        int random = r.nextInt(100)+1;
        while(true){
            Scanner sc=new Scanner(System.in);
            int i=sc.nextInt();
            System.out.println(random);
            if (random == i) {
                System.out.println("恭喜你,答对了!");
                break;
            }else if(i < random){
                System.out.println("对不起,小了");        
            }else if(i > random){
                System.out.println("对不起,大了");
            }
        }
    }
}
复制代码

 

练习:

1、编写程序,生成5个1至10之间的随机整数,并打印结果到控制台。

//第1题
        Random r=new Random();
        for(int i=0;i<5;i++){
            int random = r.nextInt(10)+1;
            System.out.println(random);
        }

2、编写程序,打印1到100之内的整数,但数字中包含7的要跳过

//第2题
        for(int i=0;i<100;i++){
            if(i%10!=7 && (i/10)%10!=7){
                System.out.println(i);
            }
        }

3.我国最高山峰是珠穆朗玛峰,8848米。现在我有一张足够大的纸,它的厚度是0.01米。请问,我折叠多少次,可以折成珠穆朗玛峰的高度。

复制代码
//第3题
        int height1=8848;//珠峰高度
        double height2=0.01;//纸的厚度
        int i=1;//定义次数
        while(true){
            height2 = height2*2;
            if(height2 >= height1){
                System.out.println(i);
                System.out.println(height2);
                break;
            }
            i=i+1;
        }
复制代码

4、需求:
* 键盘录入x的值,计算出y的并输出。
* x>=3 y = 2 * x + 1;
* -1<x<3 y = 2 * x;
* x<=-1 y = 2 * x - 1;

复制代码
//第4题
        Scanner sc=new Scanner(System.in);
        int x=sc.nextInt();
        int y = 0;
        if(x>=3){
            y = 2 * x + 1;
        }else if(x>-1 && x<3){
            y = 2 * x;
        }else if(x<=-1){
            y = 2 * x - 1;
        }
        System.out.println(y);
复制代码

5、键盘录入三个整数,并将三个数据中的最大值打印在控制台。(Math.max)

//第5题
        Scanner zzx=new Scanner(System.in);
        int a=zzx.nextInt();
        int b=zzx.nextInt();
        int c=zzx.nextInt();
        System.out.println(Math.max(Math.max(a,b),c));

6、打印9*9乘法表

for(int i=1;i<=9;i++){
            for(int j=1;j<=i;j++){
                System.out.print("    "+j+"*"+i+"="+j*i);
            }
            System.out.println();
        } 

注意:

以上内容需要放在main方法中,并且注意Random和Scanner方法的调用

原文地址:https://www.cnblogs.com/-dashu/p/9328587.html