java之变量、运算符、表达式、输入与输出

变量

变量必须先定义,才可以使用。不能重名。
变量定义的方式:

public class Main {
    public static void main(String[] args) {
        int a = 5;
        int b, c = a, d = 10 / 2;
    }
}

内置数据类型:
在这里插入图片描述

常量:

使用final修饰

final int N = 110;

类型转化:

  • 显示转化:int x = (int)'A';
  • 隐式转化:double x = 12, y = 4 * 3.3;

运算符

在这里插入图片描述

表达式

整数的加减乘除四则运算:

测试代码:

public class Main {
    public static void main(String[] args) {
        int a = 6 + 3 * 4 / 2 - 2;

        System.out.println(a);

        int b = a * 10 + 5 / 2;

        System.out.println(b);

        System.out.println(23 * 56 - 78 / 3);
    }
}

浮点数(小数)的运算:

测试代码:

public class Main {
    public static void main(String[] args) {
        double x = 1.5, y = 3.2;

        System.out.println(x * y);
        System.out.println(x + y);
        System.out.println(x - y);
        System.out.println(x / y);
    }
}

整型变量的自增、自减:

测试代码:

public class Main {
    public static void main(String[] args) {
        int a = 1;
        int b = a ++ ;
        System.out.println(a + " " + b);

        int c = ++ a;
        System.out.println(a + " " + c);
    }
}

输入

方式1,效率较低,输入规模较小时使用。

测试代码:
注意最后那个!

import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        String str = sc.next();  // 读入下一个字符串
        int x = sc.nextInt();  // 读入下一个整数
        float y = sc.nextFloat();  // 读入下一个单精度浮点数
        double z = sc.nextDouble();  // 读入下一个双精度浮点数
        String line = sc.nextLine();  // 读入下一行
        while(sc.hasNext()){//判断是否一直在输入
        }
    }
}

注意
next()表明不可以读空格,回车等
要用nextline则可以读入空格了
如果不用nextline输入"hellow world":

import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        String str = sc.next();  // 读入下一个字符串
        str = sc.next();
        String str = sc.next();
        }
}

如果要是使用nextline

import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextline();
        }
}

方式2,效率较高,输入规模较大时使用。注意需要抛异常。

测试代码:

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str = br.readLine();//读入一个字符串
        System.out.println(str);
        int x=Integer.parseInt(br.readline());//输入一个整形
    }
}

如果要是输入在一行里面,我们还要分割

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] strs=br.readline().split(" ") 
        String str = br.readLine();//读入一个字符串
        System.out.println(str);
        int x=Integer.parseInt(br.readline());
    }
}

输出

方式1,效率较低,输出规模较小时使用。

测试代码实现:

public class Main {
    public static void main(String[] args) throws Exception {
        System.out.println(123);  // 输出整数 + 换行
        System.out.println("Hello World");  // 输出字符串 + 换行
        System.out.print(123);  // 输出整数,去掉ln是不换行
        System.out.print("yxc\n");  // 输出字符串
        System.out.printf("%04d %.2f\n", 4, 123.456D);  // 格式化输出,float与double都用%f输出
    }
}

字符串输出用println,凡是输出有数字或是要求几位数的用printf
System.out.printf()中不同类型变量的输出格式:

(1) int:%d
(2) float: %f, 默认保留6位小数
(3) double: %f, 默认保留6位小数
(4) char: %c, 回车也是一个字符,用’\n’表示
(5) String: %s

方式2,效率较高,输出规模较大时使用。注意需要抛异常。

测试代码如下:

import java.io.BufferedWriter;
import java.io.OutputStreamWriter;

public class Main {
    public static void main(String[] args) throws Exception {//抛出异常
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        bw.write("Hello World\n");
        bw.flush();  // 需要手动刷新缓冲区
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

想进步的22级本科生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值