【免费Java系列】给大家出一些JavaSE基础第二天的内容案例 , 让大家更好的理解与掌握

 

一、运算符的案例

案例1:加法运算符
 

int a = 10;
int b = 20;
int sum = a + b;
System.out.println("sum: " + sum); // 输出:sum: 30

案例2:减法运算符
 

int a = 20;
int b = 10;
int difference = a - b;
System.out.println("difference: " + difference); // 输出:difference: 10

案例3:乘法运算符
 

int a = 10;
int b = 20;
int product = a * b;
System.out.println("product: " + product); // 输出:product: 200

案例4:除法运算符
 

int a = 20;
int b = 10;
int quotient = a / b;
System.out.println("quotient: " + quotient); // 输出:quotient: 2

案例5:取模运算符
 

int a = 20;
int b = 7;
int remainder = a % b;
System.out.println("remainder: " + remainder); // 输出:remainder: 6

案例6:自增运算符
 

int a = 10;
a++;
System.out.println("a: " + a); // 输出:a: 11

案例7:自减运算符
 

int a = 20;
a--;
System.out.println("a: " + a); // 输出:a: 19

二、Scanner案例

案例1:读取用户输入的整数

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入一个整数:");
        int num = scanner.nextInt();
        System.out.println("你输入的整数是:" + num);
    }
}

根据用户输入的整数,输出相应的结果。

案例2:读取用户输入的浮点数

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入一个浮点数:");
        double num = scanner.nextDouble();
        System.out.println("你输入的浮点数是:" + num);
    }
}

根据用户输入的浮点数,输出相应的结果。

案例3:读取用户输入的字符串

import java.util.Scanner;

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

        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入一个字符串:");

        String str = scanner.nextLine();

        System.out.println("你输入的字符串是:" + str);
    }
}

根据用户输入的字符串,输出相应的结果。 

 伙伴们今天的案例就到此为止啦 , 大家是否都学会了呢.

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值