程序笔记   发布时间:2022-07-21  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了java-day02大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

自增 自减

 //自增 自减
        int a= 3;
        int b =a++;//先打印 再增加
        System.out.println(b);
        int c=++a;//先增加 再打印
        System.out.println(c);

输出 b=3 c=5

幂运算

 //幂运算
        double d = Math.pow(2,3);
        System.out.println(d);

d=8.0

逻辑运算符、位运算符

&& 与

||或

!非

        Boolean a = false;
        Boolean b = true;
        System.out.println( a&&b);//与 false
        System.out.println(a||b);//或 true

左移<<

右移>>

 System.out.println(2<<3);//0010 左移3位 0001 0000

输出16

字符串连接符

 //字符串连接符   +   ,只要左有String类型
        int a =10 ;
        int b = 20;
        System.out.println(a+b);//30
        System.out.println(""+a+b);//1020
        System.out.println(a+b+"");//30

三元运算符

  //三元运算符 ? :
        int a =90;
        String type = a>60?"及格":"不及格";
        System.out.println(typE);

输出及格

包机制

java-day02

JavaDoc

java-day02

package com.monster.blog.day02;

/**
 * @author monster
 * @version 1.0
 * @since 1.7
 */
public class Doc {
    /**
     *
     * @param name
     * @return
     * @throws Exception
     */
    public  String test(String Name)throws Exception{
        return name;

    }

}

ScAnner对象

我们可以使用ScAnner对象来获取用户的输入

基本语法

ScAnner a = new ScAnner(System.in);

java-day02

next()接收

package com.monster.blog.scAnner;

import java.util.ScAnner;

public class demo01 {
    public static void main(String[] args) {
        //创建一个扫描对象,接受用户信息
        ScAnner scAnner = new ScAnner(System.in);
        System.out.println("使用Next方法接收:");
        //判断用户有没有输入字符串
        if (scAnner.hasNext()){
            //使用Next方法接收
            String str = scAnner.next();
            System.out.println("输入内容为"+str);
        }
        scAnner.close();
    }
}
//输出Hello

nextLine()接收

package com.monster.blog.scAnner;
import java.util.ScAnner;
public class demo02 {
    public static void main(String[] args) {
        ScAnner scAnner = new ScAnner(System.in);
        System.out.println("使用Nextline方法接收:");
        if (scAnner.hasNextLine()){
            String str = scAnner.nextLine();
            System.out.println("输入内容为:"+str);
        }
        scAnner.close();
    }
}
//输入内容为:Hello world
package com.monster.blog.scAnner;

import java.util.ScAnner;

public class demo03 {
    public static void main(String[] args) {
        int i =0;
        float j = 0.0f;
        System.out.println("请输入一个整数:");
        ScAnner scAnner = new ScAnner(System.in);
        if (scAnner.hasNexTint()){
            i = scAnner.nexTint();
            System.out.println("输入整数为:"+i);
        }else {
            System.out.println("你输入的不是整数");
        }
        System.out.println("请输入一个小数:");
        ScAnner scAnner1 = new ScAnner(System.in);
        if (scAnner.hasNextFloat()){
            j = scAnner.nextFloat();
            System.out.println("输入小数为:"+j);
        }else {
            System.out.println("你输入的不是小数");
        }
        scAnner.close();
    }
}

package com.monster.blog.scAnner;

import java.util.ScAnner;

public class demo04 {
    public static void main(String[] args) {
        //输入几个数,没输入一个数字用回车确认,求总和和平均数
        double sum;
        sum = 0;
        int m =0;
        ScAnner scAnner = new ScAnner(System.in);
        System.out.println("输入数字,回车输入下一个数字,输入其他字符停止:");
        while (scAnner.hasNextDouble()){
            double x = scAnner.nextDouble();
            m++;
            sum = sum +x;

        }
        System.out.println("总和为:"+sum);
        System.out.println("平均值为:"+sum/m);
        scAnner.close();
    }
}

输入数字,回车输入下一个数字,输入其他字符停止: 1 2 3 4 5 6 sf 总和为:21.0 平均值为:3.5

Process finished with exit @R_761_6756@

java-day02

顺序结构

package com.monster.blog.struct;

public class order {
    public static void main(String[] args) {
        System.out.println("Hello1");
        System.out.println("Hello2");
        System.out.println("Hello3");
        System.out.println("Hello4");
        System.out.println("Hello5");
    }

}

Hello1 Hello2 Hello3 Hello4 Hello5

Process finished with exit @R_761_6756@

选择结构

if单选择结构

package com.monster.blog.struct;

import java.util.ScAnner;

public class ifdemo01 {
    public static void main(String[] args) {
        ScAnner scAnner = new ScAnner(System.in);
        System.out.println("请输入内容:");
        String s = scAnner.nextLine();

        if(s.equals("Hello")){
            System.out.println(s);
        }
        System.out.println("end");
        scAnner.close();
    }
}

请输入内容: Hello Hello end

Process finished with exit @R_761_6756@

if双选择结构

package com.monster.blog.struct;

import java.util.ScAnner;

public class ifdemo02 {
    public static void main(String[] args) {
        //如果分数大于60 及格,否则不及格
        ScAnner scAnner = new ScAnner(System.in);
        System.out.println("输入成绩:");
        int score = scAnner.nexTint();
        if(score<60){
            System.out.println("不及格");
        }else {
            System.out.println("及格");
        }
        scAnner.close();
    }
}

输入成绩: 60 及格

Process finished with exit @R_761_6756@

if多选择结构

package com.monster.blog.struct;

import java.util.ScAnner;

public class ifdemo03 {
    public static void main(String[] args) {
        ScAnner scAnner = new ScAnner(System.in);
        System.out.println("输入成绩:");
        int score = scAnner.nexTint();
        if(score == 100){
            System.out.println("恭喜满分!");
        }else if(score<100&&score>=90){
            System.out.println("获得A");
        }else if(score<90&&score>=80){
            System.out.println("获得B");
        }else if(score<80&&score>=70){
            System.out.println("获得c");
        }else if(score<70&&score>=60){
            System.out.println("获得D");
        }else {
            System.out.println("不及格");
        }

        scAnner.close();
    }
}

输入成绩: 79 获得c

Process finished with exit @R_761_6756@

switch多选择结构

package com.monster.blog.struct;

public class Switchdemo01 {
    public static void main(String[] args) {
        char score ='A';
        switch (score){
            case 'A':
                System.out.println("优秀");
                break;
            case 'B':
                System.out.println("良好");
                break;
            case 'C':
                System.out.println("及格");
                break;
            case 'D':
                System.out.println("加油");
                break;
            case 'E':
                System.out.println("不及格");
                break;
            default:
                System.out.println("未知");
                break;
        }
    }
}

优秀

Process finished with exit @R_761_6756@

循环结构

while

package com.monster.blog.struct;

public class whiledemo01 {
    public static void main(String[] args) {
        //输出1-10
        int i=0;
        while (i<10){
            i++;
            System.out.println(i);
        }

    }
}

1 2 3 4 5 6 7 8 9 10

Process finished with exit @R_761_6756@

package com.monster.blog.struct;

public class whiledemo02 {
    public static void main(String[] args) {
        //输出1+2+3+···+10
        int i = 0;
        int sum =0;
        while(i<10){
            i++;
            sum=sum+i;
        }
        System.out.println(sum);
    }
}

55

Process finished with exit @R_761_6756@

do...while

package com.monster.blog.struct;

public class dowhiledemo03 {
    public static void main(String[] args) {
        //输出1+2+3+···+10
        int i = 0;
        int sum =0;
        do{
            i++;
            sum=sum+i;

        }while (i<10);
        System.out.println(sum);
    }

}

55

Process finished with exit @R_761_6756@

大佬总结

以上是大佬教程为你收集整理的java-day02全部内容,希望文章能够帮你解决java-day02所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。