Java基础知识1

Java小结1

scanner

使用方法1:

public class demo01 {
    public static void main(String[] args) {

        Scanner s = new Scanner(System.in);

        System.out.println("输入数据:");

        String str = s.nextLine();

        System.out.println("输出的内容为:"+str);

        //一定要关闭,避免浪费资源
        s.close();

    }
}

这里一共有两种 scanner.nextLine() 和 scanner.next()两种办法接收

  • scanner.next()当遇到空格时,不接收空格后面的内容

  • scanner.nextLine()可以接收空格,遇到回车时停止接收内容

当scanner来判断输入的是整数还是小数时:

public class demo02 {
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        int i = 0;
        float f = 0.0f;

        System.out.println("请输入整数:");
        //判断输入是否为整数
        if (scanner.hasNextInt()){

            i = scanner.nextInt();
            System.out.println("整数数据:"+i);
        }else{
            System.out.println("输入的不是整数!");
        }

        System.out.println("请输入小数:");
        //判断输入的是否为小数
        if (scanner.hasNextFloat()){

            f = scanner.nextFloat();
            System.out.println("小数数据:"+f);
        }else{
            System.out.println("输入的不是小数!");
        }

        scanner.close();

    }
}

Java流程控制

if选择结构

使用方法如下:

public class ifDemo01 {
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);


        System.out.println("请输入成绩:");
        int score = scanner.nextInt();
         
		//判断:当分数大于60,及格,分数小于60,不及格
        if(score>60){
            System.out.println("成绩及格");
        }else {
            System.out.println("成绩不及格");
        }

        scanner.close() ;
    }

switch多选择结构

使用方法如下:

public class switchDemo01 {
    public static void main(String[] args) {

        System.out.println("请输入等级:");

        String Grade = new Scanner(System.in).next();
		
        //这里有四种选择,分别对应不同的等级,若所输入的等级均不在这四种等级,则输入错误(default)
        switch (Grade){

            case "A":
                System.out.println("优秀");
                break;
            case "B":
                System.out.println("良好");
                break;
            case "C":
                System.out.println("一般");
                break;
            case "D":
                System.out.println("很差");
                break;
            default:
                System.out.println("输入错误");
                break;

        }
    }
}

循环结构

while循环

使用方法如下:

public class whileDemo01 {
    //求1~100的和
    public static void main(String[] args) {

        int i = 1;

        int sum = 0;

        //当i小于等于100时执行循环里面的内容,即当i大于100时跳出循环
        while(i <= 100){
            sum = sum+i;
            i++;
        }
        System.out.println("1~100的和为:"+sum);

    }
}
do while循环

使用方法如下:

public class doWhileDemo01 {
	//求1~100的和
    public static void main(String[] args) {

        int i = 1;

        int sum = 0;

        //当i小于等于100时执行循环里面的内容,即当i大于100时跳出循环
        do{
            sum = sum+i;
            i++;
        }while(i <= 100);
        
        System.out.println("1~100的和为:"+sum);

    }
}

注意:while 和do while的区别在于do while 的语句至少执行一次,而while不一定执行!

for循环

使用方法如下:

public class forDemo01 {
    //求1~100的和
    public static void main(String[] args) {

        int sum = 0;

        //利用for循环来求1~100的和
        for (int i = 0; i <= 100; i++) {
            sum = sum+i;
        }

        System.out.println("1~100的和为:"+sum);

    }
}

使用for循环输出九九乘法表

public class forDemo02 {
    //输出九九乘法表
    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*i+"\t");
            }
            System.out.println();
        }
    }

}

结果如下:

在这里插入图片描述

使用for循环打印三角形(5行)

public class forDemo03 {
    //打印三角形(五行)
    public static void main(String[] args) {
        for (int i = 1; i <= 5; i++) {
            for (int j = 5; j > i; j--) {
                System.out.print(" ");
            }
            for (int j = 1; j <= i; j++) {
                System.out.print("*");
            }
            for (int j = 1; j < i; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

结果如下
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值