初识Java 2

前言

文章列出的是java与c语言中有区别或者相通但是c语言没有提到的内容

运算符

  1. 负数取余:
public class Test {
    public static void main(String[] args) {
        System.out.println(10 % 3);
        System.out.println(-10 % 3);
        System.out.println(10 % -3);
        System.out.println(-10 % -3);
    }
}

在这里插入图片描述

  1. 当&和|左右两边都为布尔表达式时也可以起到逻辑或和逻辑与的作用,但是不能短路与和短路或

  2. 无符号右移

-1>>1    右移
-1>>>1   无符号右移

不存在无符号左移

逻辑控制

选择结构

  1. if 括号中的表达时必须为布尔表达式
  2. 不能做switch的参数的数据类型有:long,float,double,boolean

输入

从键盘输入

Scanner scan = new Scanner(System.in);
int a = scan.nextInt();
或者
Scanner a = new Sanner(System.in);
int num = a.nextInt();
String name = a.next();
String name = a.nextLine();
当使用next的时候,遇到空格时,就不会继续读了,空格会保留在缓冲区中
当输入的数据有多个类型时,尽量优先输入字符串,因为读入字符串可能会将上次输入但遗留在缓冲区的回车当作要输入的内容
scan.colse();//关闭资源(类似c语言中的文件)

Ait加回车会提示要导入的包,ctr加鼠标左键会进入工具界面

多行输入:
import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        while(scan.hasNextInt()){
        //idea中输入ctrl+d退出
        //cmd窗口中输入ctrl+z退出
            System.out.println("输入您的年龄:");
            int age = scan.nextInt();
            System.out.println("年龄:" + age);
        }
    }
}
import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
//        while(scan.hasNextInt()){
//            System.out.println("输入您的年龄:");
//            int age = scan.nextInt();
//            System.out.println("年龄:" + age);
//        }
        while(scan.hasNextLine()){
            System.out.println("输入您的姓名");
            String name = scan.nextLine();
            System.out.println("姓名:" + name);
        }
        scan.close();
    }
}

猜数字游戏

java中生成随机数

需要用到工具Random

//生成随机数:
    public static void main(String[] args) {
        Random random = new Random();
        int a = random.nextInt(100);//bound是设置数字最大值(不包括)
        //[0,100)
        System.out.println(a);
    }

随机数的生成是根据种子数(时间)来生成的,这里若要将每次生成的随机数都相等,需要手动设置种子数为定值:

Random random = new Random(32);
public class Test {
    //生成随机数:
    public static void main(String[] args) {
        Random random = new Random(32);//设置种子数
        int a = random.nextInt(100);//bound是设置数字最大值(不包括)
        //[0,100)
        System.out.println(a);
    }

还有一种生成随机数的方法:
Math.Random

游戏代码

public static void main(String[] args) {
        Random random = new Random();
        int a = random.nextInt(101);
        int i = 102;
        System.out.println("已生成一个0到100的随机数,请猜一下他是几:");
        while( i != a){
            Scanner scan = new Scanner(System.in);
            i = scan.nextInt();
            if (i > a){
                System.out.println("猜大了");
            }else{
                System.out.println("猜小了");
            }
        }
        System.out.println("猜对了");
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

With Order @!147

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

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

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

打赏作者

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

抵扣说明:

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

余额充值