又被System.out.print给坑了一把

学过java的同学都应该知道,第一个程序很多人都是这样:

public class Hello {

    public static void main(String[] args) {        

        System.out.print("Hello,world!");

    }
}

打印结果是:Hello,world!

 

接着可能会学习用户控制台输入Scanner的例子:

import java.util.Scanner;

public class ScannerExample {
    public static void main(String[] args) {
        System.out.println("请输入第一个整数:");

        // 创建Scanner对象用于接收用户输入
        Scanner scanner = new Scanner(System.in);        
        
        // 使用nextInt()方法读取一个整数
        int num1 = scanner.nextInt();
        
        System.out.println("请输入第二个整数:");
        // 再次使用nextInt()方法读取另一个整数
        int num2 = scanner.nextInt();
        
        // 计算两个整数的和
        int sum = num1 + num2;
        
        // 输出结果
        System.out.println("两个整数的和为:" + sum);
        
        // 关闭scanner
        scanner.close();
    }
}

这个代码很正常,提示用户输入两个整数,然后打印它们的和。

我把程序改成如下:

import java.util.Scanner;

public class ScannerExample {
    public static void main(String[] args) {
        // 创建Scanner对象用于接收用户输入
        Scanner scanner = new Scanner(System.in);
        
        System.out.println("请输入第一个整数:");
        // 使用nextInt()方法读取一个整数
        int num1 = scanner.nextInt();
        
        System.out.println("请输入第二个整数:");
        // 再次使用nextInt()方法读取另一个整数
        int num2 = scanner.nextInt();
        
        // 计算两个整数的和
        int sum = num1 + num2;
        
        // 输出结果
        System.out.println("两个整数的和为:" + sum);
        
        // 关闭scanner
        scanner.close();
    }
}

使用vs code运行后竟然是这样:

63861176e68b42aaaaad3e0dcf5ea22a.png

第6行运行之后不应该停下来等我输入吗?怎么直接输出第8行的提示语句了啊?

查看了System的源码:

public final class System {
    /* Register the natives via the static initializer.
     *
     * The VM will invoke the initPhase1 method to complete the initialization
     * of this class separate from <clinit>.
     */
    private static native void registerNatives();
    static {
        registerNatives();
    }

    /** Don't let anyone instantiate this class */
    private System() {
    }

    /**
     * The "standard" input stream. This stream is already
     * open and ready to supply input data. Typically this stream
     * corresponds to keyboard input or another input source specified by
     * the host environment or user. In case this stream is wrapped
     * in a {@link java.io.InputStreamReader}, {@link Console#charset()}
     * should be used for the charset, or consider using
     * {@link Console#reader()}.
     *
     * @see Console#charset()
     * @see Console#reader()
     */
    public static final InputStream in = null;

    /**
     * The "standard" output stream. This stream is already
     * open and ready to accept output data. Typically this stream
     * corresponds to display output or another output destination
     * specified by the host environment or user. The encoding used
     * in the conversion from characters to bytes is equivalent to
     * {@link Console#charset()} if the {@code Console} exists,
     * <a href="#stdout.encoding">stdout.encoding</a> otherwise.
     * <p>
     * For simple stand-alone Java applications, a typical way to write
     * a line of output data is:
     * <blockquote><pre>
     *     System.out.println(data)
     * </pre></blockquote>
     * <p>
     * See the {@code println} methods in class {@code PrintStream}.
     *
     * @see     java.io.PrintStream#println()
     * @see     java.io.PrintStream#println(boolean)
     * @see     java.io.PrintStream#println(char)
     * @see     java.io.PrintStream#println(char[])
     * @see     java.io.PrintStream#println(double)
     * @see     java.io.PrintStream#println(float)
     * @see     java.io.PrintStream#println(int)
     * @see     java.io.PrintStream#println(long)
     * @see     java.io.PrintStream#println(java.lang.Object)
     * @see     java.io.PrintStream#println(java.lang.String)
     * @see     Console#charset()
     * @see     <a href="#stdout.encoding">stdout.encoding</a>
     */
    public static final PrintStream out = null;

    /**
     * The "standard" error output stream. This stream is already
     * open and ready to accept output data.
     * <p>
     * Typically this stream corresponds to display output or another
     * output destination specified by the host environment or user. By
     * convention, this output stream is used to display error messages
     * or other information that should come to the immediate attention
     * of a user even if the principal output stream, the value of the
     * variable {@code out}, has been redirected to a file or other
     * destination that is typically not continuously monitored.
     * The encoding used in the conversion from characters to bytes is
     * equivalent to {@link Console#charset()} if the {@code Console}
     * exists, <a href="#stderr.encoding">stderr.encoding</a> otherwise.
     *
     * @see     Console#charset()
     * @see     <a href="#stderr.encoding">stderr.encoding</a>
     */
    public static final PrintStream err = null;
    
    ......
}

来试图理解一下里面的流程:

System.in代表标准输入流,通常与键盘输入相关联。
System.out代表标准输出流,通常与控制台输出相关联。
程序启动之后,通过registerNatives()和设备进行关联。

我对这个设计的理解是,System.inSystem.out是在程序启动时就已经准备好的,并且会在程序的整个生命周期中保持可用状态。当程序需要读取用户输入或向控制台输出内容时,会使用这两个标准流对象来进行交互操作。因此,它们的执行顺序可以看作是在程序执行期间随时可用。

不知道对不对?

问题来源:

java用scanner类从控制台读取输入怎么可以先读取再提示呢?_编程语言-CSDN问答

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/610456.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

SSIM(Structural Similarity),结构相似性及MATLAB实现

参考文献 Wang, Zhou; Bovik, A.C.; Sheikh, H.R.; Simoncelli, E.P. (2004-04-01). “Image quality assessment: from error visibility to structural similarity”. IEEE Transactions on Image Processing. 13 (4): 600–612. Bibcode:2004ITIP…13…600W. CiteSeerX 10.…

ctype--数据类型转换函数——vb.net

CType 函数 语法 CType(expression, typename) 组成部分 expression 任何有效表达式。 如果 expression 的值超出 typename 所允许的范围&#xff0c;Visual Basic 将引发异常。 typenameDim 语句的 As 子句中的任何合法表达式&#xff0c;即任何数据类型、对象、结构、类或接…

【系统架构师】-选择题(十三)数据库基础

1、在某企业的营销管理系统设计阶段&#xff0c;属性"员工"在考勤管理子系统中被称为"员工"&#xff0c;而在档案管理子系统中被称为"职工"&#xff0c;这类冲突称为&#xff08; 命名冲突&#xff09;。 同一个实体在同系统中存在不同的命名&am…

2024年财富自由秘籍,创业项目大揭秘!

2024年&#xff0c;一个崭新的创业项目如日中天般迅速崛起&#xff0c;吸引了无数创业者的目光——那就是APP广告变现。这不仅是一条轻松实现财富自由的道路&#xff0c;更是一个充满无限可能的黄金领域。 在移动互联网高速发展的今天&#xff0c;智能手机已成为我们生活中不可…

UE4\UE5 调试源代码流程(重点讲不去Github装源代码情况)

UE4\UE5 调试源代码流程 前言&#xff1a; 很多写UE C代码的小伙伴&#xff0c;肯定发现了&#xff0c;在虚幻源代码里面是没办法打断点进行调试的&#xff0c;就算走Debug调试流程&#xff0c;也依旧不能正常打断点调试&#xff0c;今天我们来分享一下不装Github源代码情况下…

各种数据获取stream流的方式

1.单列集合&#xff08;直接调用&#xff09; ArrayList<Integer> list new ArrayList<>();list.stream(); 2.双列集合 HashMap<String, Integer> map new HashMap<>();map.put("aaa",111);map.put("bbb",222);map.put("c…

Vue中引入Element组件、路由router、Nginx打包部署

目录 1、Element-ui(饿了么ui) 演示&#xff1a; 怎么打开NPM脚本&#xff1f; Vue路由router Nginx打包部署Vue-Cli项目 1、Element-ui(饿了么ui) element-ui(饿了么ui)是一个非常好用且美观的组件库(插件库)&#xff0c;主要用于网站快速成型&#xff0c;由国产团队饿了么…

RH850F1KM Part1 创建一个新工程

1、选择File->New ECU Project.# 2、填写工程名和工程文件路径&#xff0c;点击Next 3、点击Next 4、点击Finish 5、报错&#xff1a;# 6、步骤5报错原因&#xff1a; RH850F1KM 搭建MCAL配置环境中复制到BSWMD文件夹下的文件过多&#xff0c;除包含当前芯片型号外&#…

618值得入手的平价好物清单,看完再买不吃亏!

即将到来的618年中购物狂欢节&#xff0c;无疑是一年一度的购物盛宴。为了让大家的购物体验更加愉悦和充实&#xff0c;我特地为大家精选了一系列好物。如果你也打算在618尽情购物&#xff0c;那就赶紧收藏这份清单吧&#xff01; 一、舒适佩戴不伤耳——南卡骨传导耳机Runner…

EDA(四)Verilog

EDA&#xff08;四&#xff09;Verilog Verilog是一种用于电子系统设计自动化&#xff08;EDA&#xff09;的硬件描述语言&#xff08;HDL&#xff09;&#xff0c;主要用于设计和模拟电子系统&#xff0c;特别是在集成电路&#xff08;IC&#xff09;和印刷电路板&#xff08;…

关于服务端接口知识的汇总

大家好&#xff0c;今天给大家分享一下之前整理的关于接口知识的汇总&#xff0c;对于测试人员来说&#xff0c;深入了解接口知识能带来诸多显著的好处。 一、为什么要了解接口知识&#xff1f; 接口是系统不同模块之间交互的关键通道。只有充分掌握接口知识&#xff0c;才能…

面试算法之哈希专题

赎金信 class Solution { public:bool canConstruct(string ransomNote, string magazine) {// 小写字母int r_cnt[26];int m_cnt[26];for(int i 0; i< magazine.size(); i) {m_cnt[magazine[i]-a]; // 统计}// 对比for(int i 0; i< ransomNote.size(); i) {if(m_cnt[r…

用幽兰本体验大语言模型

大语言模型&#xff08;LLM&#xff09;是目前炙手可热的话题&#xff0c;每个人都想体验一下大语言模型的魅力。然而如果使用云端的大语言模型服务&#xff0c;则不仅速度慢&#xff0c;而且可能泄露自己的隐私。幽兰代码本使用瑞芯微公司推出的 RK3588 SoC 芯片作为核心硬件&…

财务管理|基于SprinBoot+vue的财务管理系统(源码+数据库+文档)

财务管理系统 目录 基于SprinBootvue的财务管理系统 一、前言 二、系统设计 三、系统功能设计 系统功能实现 1管理员功能模块 2员工功能模块 四、数据库设计 五、核心代码 六、论文参考 七、最新计算机毕设选题推荐 八、源码获取&#xff1a; 博主介绍&#xff1…

2024年必看:13大顶尖Scrum工具助力敏捷项目管理

Scrum 管理工具有&#xff1a;PingCode、Jira、Trello、Zoho Sprints、Active Collab、ProProfs Project、Scrumwise、ClickUp、Monday.com、QuickScrum、Yodiz、ScrumDo、nTask 在过去几年中&#xff0c;Scrum方法论已成为敏捷项目管理的主要框架之一。使用Scrum&#xff0c;项…

存储和NFS共享

存储类型 存储类型分为三种 直连式存储&#xff1a;Direct-Attached Storage&#xff0c;简称DAS网络附加存储&#xff1a;Network-Attached Storage&#xff0c;简称NAS存储区域网络&#xff1a;Storage Area Network&#xff0c;简称SAN DAS:存储和主机是直连的&#xff0…

为什么 Cloudflare 是 2024 年 Vercel 的最佳替代品?生态系统和价格比较

本文探讨了 Vercel 的功能&#xff0c;并与 Cloudflare 生态系统中的类似产品进行了比较。从托管到存储&#xff0c;我们将看到为什么 Cloudflare 可以在 2024 年成为 Vercel 的最佳替代品。 文章目录 介绍什么是 Cloudflare&#xff1f;Cloudflare vs Vercel&#xff1a;托管和…

防雷防浪涌电路设计

通信线路或者电源线路通常会铺设到户外&#xff0c;一旦线路铺到户外后&#xff0c;就需要考虑防雷的问题了&#xff0c;那么怎么设计保护电路&#xff0c;能够防止雷电等浪涌对电路的破坏呢&#xff1f; 通信线路或者电源线路通常会铺设到户外&#xff0c;一旦线路铺到户外后&…

每日Attention学习3——Cross-level Feature Fusion

模块出处 [link] [code] [PR 23] Cross-level Feature Aggregation Network for Polyp Segmentation 模块名称 Cross-level Feature Fusion (CFF) 模块作用 双级特征融合 模块结构 模块代码 import torch import torch.nn as nnclass BasicConv2d(nn.Module):def __init__(…

Google搜索广告怎么开户?谷歌广告开户投放引流技巧、账户搭建、谷歌ads广告推广投放策略 #搜索引擎 #谷歌广告#互联网营销

Google搜索广告开户步骤&#xff1a; 选择代理商&#xff1a;首先&#xff0c;您需要选择一个经验丰富、信誉良好的Google广告代理商。可以选择上海上弦来广告开户和代运营。 初步咨询&#xff1a;与代理商进行初步沟通&#xff0c;了解他们的服务内容、成功案例、收费标准等。…