又被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);// 关闭scannerscanner.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);// 关闭scannerscanner.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://xiahunao.cn/news/3029995.html

如若内容造成侵权/违法违规/事实不符,请联系瞎胡闹网进行投诉反馈,一经查实,立即删除!

相关文章

中国地面基本气象逐小时数据获取方式

环境气象数据服务平台提供了全国大约2100个点位&#xff0c;2023年1月1日至今的小时级数据。包括气温、气压、湿度、风、降水等要素。 数据基于ECMWF ERA5-Land Hourly陆面再分析资料和中国地面基本气象观测逐三小时数据&#xff0c;使用机器学习模型加工所得&#xff0c;对比…

记一次DNS故障导致用户无法充值的问题(下)

上一篇说到DNS故障导致无法充值&#xff0c;后来我们通过拨测发现业务域名的解析目标地址被解析到了【127.0.0.1】IP。 1、联系阿里云厂商&#xff0c;通过沟通&#xff0c;阿里云反馈我们的域名被XX省通管单位封禁了&#xff0c;导致解析到了不正确的地址。 2、为了解决用户问…

ADS基础教程11 - TouchStone文件的导出及导入

目录 一、 T o u c h S t o n e 介绍 \color{#4285f4}{ \mathbf{ 一、TouchStone介绍}} 一、TouchStone介绍 二、文件导出、导入方式 \color{#4285f4}{ \mathbf{ 二、文件导出、导入方式}} 二、文件导出、导入方式1.原理图操作1&#xff09;原理图中导出2.原理图中导入 3.DDW中…

云服务器性能大揭秘:4核与8核你知道多少?

​  云计算服务中&#xff0c;通常使用具有多个 CPU 的服务器集群&#xff0c;以便为企业和个人等提供可扩展的计算资源。这使他们能够处理大量数据处理和存储&#xff0c;而无需自己投资昂贵的硬件。像术语“4核”和“8核”&#xff0c;在云服务器领域中就比较常见&#xff…

中国地面气候资料日值数据获取方式

数据简介 环境气象数据服务平台提供了全国大约2100个点位&#xff0c;2000年至2023年的逐日数据。包括气温、气压、湿度、风、降水等要素。 数据基于ECMWF reanalysis-era5-land、reanalysis-era5-single-levels 以及中国2100站点地面气候资料日值观测数据&#xff0c;使用机器…

winform植物大战僵尸

winform植物大战僵尸 植物大战僵尸源码 半成品 需要的拿去学习 登陆注册选择关卡 向日葵 豌豆射手 双枪豌豆射手 项目获取&#xff1a; 项目获取&#xff1a;typora: typora/img (gitee.com) 备用项目获取链接1&#xff1a;yifeiyixiang/kamo: 源码下载 (github.com) 备用…

Capl复合数据类型:枚举

用于定义一组有穷的、命名的常量。常量有了命名更容易理解和记忆&#xff0c;枚举中常量的命名&#xff0c;必须在整个capl程序中唯一。 定义的枚举类型变量&#xff0c;里面的成员如果没有赋值&#xff0c;默认从左到右&#xff0c;依次为012345...。如果有赋值就该成员就代表…

【全开源】Java外卖霸王餐免费吃外卖小程序+APP+公众号+H5多端霸王餐源码

一、特色功能 霸王餐活动管理&#xff1a;允许商家发布和管理霸王餐活动&#xff0c;包括设置活动时间、具体优惠、活动规则等。用户参与与浏览&#xff1a;用户可以在小程序中浏览霸王餐活动列表&#xff0c;查看活动的详情信息&#xff0c;如商品或服务的免费赠送、活动规则…

【NLP练习】使用seq2seq实现文本翻译

使用seq2seq实现文本翻译 &#x1f368; 本文为&#x1f517;365天深度学习训练营 中的学习记录博客&#x1f356; 原作者&#xff1a;K同学啊 from __future__ import unicode_literals, print_function, division from io import open import unicodedata import string impo…

Ubuntu/Linux 安装Docker + PyTorch

文章目录 1. 提前准备2. 安装Docker2.1. 卸载冲突软件&#xff08;非必要&#xff09;2.2. 在Ubuntu系统上添加Docker的官方GPG密钥2.3. 将Docker的仓库添加到Ubuntu系统的APT源列表中2.4. 安装最新Docker2.5. 检查 3. 安装Nvidia Container Toolkit3.1. 在Ubuntu系统上添加官方…

1688一键采购1000+商品||1688API数据采集接口||1688官方合作伙伴如何实现1688代采

货源采购是跨境电商卖家日常工作的一个重要部分&#xff0c;1688平台是很多卖家常用的货源采购平台。在1688平台采购时&#xff0c;很多卖家会进行批量采购&#xff0c;可能会达到上百甚至上千单&#xff0c;如果一件一件的采购&#xff0c;可能会浪费卖家大量的时间。我们可以…

【java9】java9新特性之改进JavaDocs

Java9在JavaDocs方面的主要新特性是&#xff0c;其输出现在符合兼容HTML5标准。在之前的版本中&#xff0c;默认的HTML版本是 HTML4.01&#xff0c;但在Java9及之后的版本中&#xff0c;JavaDocs命令行工具将默认使用HTML5作为输出标记语言。这意味着&#xff0c;使用JavaDocs工…

MemoryModule - exp - test

文章目录 MemoryModule - exp - test概述笔记测试环境GetModuleFileName不能正常执行GetModuleFileNameWntdll_LdrGetDllFullName猜测原因用LoadLibrary载入的DLL中功能是正常的 gLog可以正常使用内存载入DLL无法支持的功能的折中方法COM操作正常调用方代码接口代码 接口入参测…

Visual Studio的使用方法

目录 1. 下载软件 2. 软件安装 3. 软件使用 4. VS工具的字体背景美化 5. 程序调试 1. 下载软件 官网地址&#xff1a;Visual Studio 2022 IDE - 适用于软件开发人员的编程工具 (microsoft.com) 2. 软件安装 1.选中vs_Professional&#xff0c;鼠标右击选择“以管理员身份…

Capl复合数据类型:结构

结构是由一系列的相同或不同的数据类型构成的整体。结构中的每一项数据成为一个成员。每个成员可以有各自的数据类型。所有成员一起表示一个整体结构。 1.定义结构体类型 运行结果 2.修改结构体变量的成员。自定义一个常量给结构体变量的成员重新赋值即可。 3.定义一个结构体类…

【平时工作中的各种术语__持续更新~~~~】

中文&#xff1a; 1、jar包 JAR包&#xff08;Java Archive Package&#xff09;是一种将多个Java类文件以及与它们相关的元数据和资源&#xff08;如文本、图片等&#xff09;打包到一个单一文件中的归档工具。它基于ZIP文件格式。JAR文件主要用于分发和部署Java应用程序。J…

java期末复习

java jdk jre jvm .java-------->.class----jvm---->机器语言 编写源文件 编译源文件生成字节码 加载运行字节码 java语句执行顺序 顺序 选择 循环 异常处理 基本语法 方法格式 权限修饰符 返回值声明 方法名称(参数列表){方法中封装的逻辑功能;return 返回值;}–权…

Mybatis-Plus常用的增删改查坑

添加依赖 <!--实体类上加上Data注解就不用写get&#xff0c;set&#xff0c;toString&#xff0c;equals等方法了--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional…

Xshell连接提示“SSH服务器拒绝了密码”

原因1&#xff1a;数字锁没有打开 没有打开NumLock&#xff08;数字小键盘上面有一个【Num】按键&#xff09;&#xff0c;需要按键开启。 注意要检查NumLock灯是否亮起。 或者改成用字母键上面的数字键输入就好了。 原因2&#xff1a;root密码设置错误&#xff08;这个是比较常…

linux上安装Jmeter环境

以前都是在Windows本机上使用界面版Jmeter&#xff0c;今天试一下安装到linux上在linux中使用&#xff0c;Jmeter的使用需要先安装jdk环境然后再配置jmeter。 1.配置环境 linux环境&#xff1a;Centos 8.2 64位 JDK版本&#xff1a;jdk-8u221-linux-x64.tar.gz &#xff08;…