The readLine method uses System.out.print (not println) to print its string argument on the screen (later when we use the readLine method, the string argument of the method will be a message telling the user to type some text). Then the readLine method uses the input scanner object to read a whole line of text from the user of the program and returns the text as result. The readPosInt method uses System.out.print (not println) to print its string argument on the screen (later when we use the readPosInt method, the string argument of the method will be a message telling the user to type some integer). Then the readPosInt method uses the input scanner object to read an integer from the user of the program.写一个java文件

时间: 2023-12-14 17:38:48 浏览: 28
下面是一个实现了readLine和readPosInt方法的Java文件: ```java import java.util.Scanner; public class InputReader { private Scanner input; public InputReader() { input = new Scanner(System.in); } public String readLine(String message) { System.out.print(message); return input.nextLine(); } public int readPosInt(String message) { System.out.print(message); int num = input.nextInt(); input.nextLine(); // consume newline character while (num <= 0) { System.out.print("Please enter a positive integer: "); num = input.nextInt(); input.nextLine(); // consume newline character } return num; } } ``` 这个类有一个私有的Scanner对象,用于读取用户的输入。readLine方法和readPosInt方法都接受一个String类型的参数作为提示信息,然后通过System.out.print方法将提示信息打印到屏幕上。readLine方法调用input.nextLine()方法读取用户输入的一行文本,并返回该文本。readPosInt方法调用input.nextInt()方法读取用户输入的整数,然后消耗掉输入中的换行符(因为nextInt方法只会读取整数部分,而不包括后面的换行符)。然后,如果输入的整数不是正整数,就会提示用户重新输入,直到输入的整数为正整数为止。最后,readPosInt方法返回输入的正整数。

相关推荐

The readPosInt method uses System.out.print (not println) to print its string argument on the screen (later when we use the readPosInt method, the string argument of the method will be a message telling the user to type some integer). Then the readPosInt method uses the input scanner object to read an integer from the user of the program. After reading the integer, the readPosInt method must also use the scanner’s nextLine method to read the single newline character that comes from the user pressing the Enter key on the keyboard after typing the integer (if you do not read this newline character using the nextLine method inside the readPosInt method, then the newline character will remain in the input stream, and, the next time you use the readLine method described above, the readLine method will just immediately read only the newline character from the input stream and return an empty string as result, without waiting for the user to type anything!) If the user types something which is not an integer, then the nextInt method of the scanner will throw an InputMismatchException. In that case the code of your readPosInt method must catch the exception, use System.out.println to print the error message "You must type an integer!" to the user (use System.out.println for this, not System.err.println, otherwise you might hit a bug in Eclipse...), use the scanner’s nextLine method to read (and ignore) the wrong input typed by the user of the program (if you do not do this, the wrong input typed by the user will remain in the input stream, and the next time you call the nextInt method again, you will get an InputMismatchException again!), and then do the whole thing again (including printing again the string argument of the readPosInt method) to try to read an integer again (hint: put the whole code of the method inside a while loop). After reading the integer and the newline character (which is just ignored), the readPosInt method tests the integer.写java文件

最新推荐

recommend-type

Java_带有可选web的开源命令行RatioMaster.zip

Java_带有可选web的开源命令行RatioMaster
recommend-type

基于MATLAB实现的GA算法解决车辆调度问题VRP+使用说明文档.rar

CSDN IT狂飙上传的代码均可运行,功能ok的情况下才上传的,直接替换数据即可使用,小白也能轻松上手 【资源说明】 基于MATLAB实现的GA算法解决车辆调度问题VRP+使用说明文档.rar 1、代码压缩包内容 主函数:main.m; 调用函数:其他m文件;无需运行 运行结果效果图; 2、代码运行版本 Matlab 2020b;若运行有误,根据提示GPT修改;若不会,私信博主(问题描述要详细); 3、运行操作步骤 步骤一:将所有文件放到Matlab的当前文件夹中; 步骤二:双击打开main.m文件; 步骤三:点击运行,等程序运行完得到结果; 4、仿真咨询 如需其他服务,可后台私信博主; 4.1 期刊或参考文献复现 4.2 Matlab程序定制 4.3 科研合作 功率谱估计: 故障诊断分析: 雷达通信:雷达LFM、MIMO、成像、定位、干扰、检测、信号分析、脉冲压缩 滤波估计:SOC估计 目标定位:WSN定位、滤波跟踪、目标定位 生物电信号:肌电信号EMG、脑电信号EEG、心电信号ECG 通信系统:DOA估计、编码译码、变分模态分解、管道泄漏、滤波器、数字信号处理+传输+分析+去噪、数字信号调制、误码率、信号估计、DTMF、信号检测识别融合、LEACH协议、信号检测、水声通信 5、欢迎下载,沟通交流,互相学习,共同进步!
recommend-type

小程序源码-平安保险小程序.zip

小程序源码-平安保险小程序小程序源码-平安保险小程序小程序源码-平安保险小程序小程序源码-平安保险小程序小程序源码-平安保险小程序小程序源码-平安保险小程序小程序源码-平安保险小程序小程序源码-平安保险小程序小程序源码-平安保险小程序小程序源码-平安保险小程序小程序源码-平安保险小程序小程序源码-平安保险小程序小程序源码-平安保险小程序小程序源码-平安保险小程序
recommend-type

数据库查看工具网页版本

数据库查看工具网页版本
recommend-type

grpcio-1.46.5-cp38-cp38-linux_armv7l.whl

Python库是一组预先编写的代码模块,旨在帮助开发者实现特定的编程任务,无需从零开始编写代码。这些库可以包括各种功能,如数学运算、文件操作、数据分析和网络编程等。Python社区提供了大量的第三方库,如NumPy、Pandas和Requests,极大地丰富了Python的应用领域,从数据科学到Web开发。Python库的丰富性是Python成为最受欢迎的编程语言之一的关键原因之一。这些库不仅为初学者提供了快速入门的途径,而且为经验丰富的开发者提供了强大的工具,以高效率、高质量地完成复杂任务。例如,Matplotlib和Seaborn库在数据可视化领域内非常受欢迎,它们提供了广泛的工具和技术,可以创建高度定制化的图表和图形,帮助数据科学家和分析师在数据探索和结果展示中更有效地传达信息。
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

【实战演练】MATLAB用遗传算法改进粒子群GA-PSO算法

![MATLAB智能算法合集](https://static.fuxi.netease.com/fuxi-official/web/20221101/83f465753fd49c41536a5640367d4340.jpg) # 2.1 遗传算法的原理和实现 遗传算法(GA)是一种受生物进化过程启发的优化算法。它通过模拟自然选择和遗传机制来搜索最优解。 **2.1.1 遗传算法的编码和解码** 编码是将问题空间中的解表示为二进制字符串或其他数据结构的过程。解码是将编码的解转换为问题空间中的实际解的过程。常见的编码方法包括二进制编码、实数编码和树形编码。 **2.1.2 遗传算法的交叉和
recommend-type

openstack的20种接口有哪些

以下是OpenStack的20种API接口: 1. Identity (Keystone) API 2. Compute (Nova) API 3. Networking (Neutron) API 4. Block Storage (Cinder) API 5. Object Storage (Swift) API 6. Image (Glance) API 7. Telemetry (Ceilometer) API 8. Orchestration (Heat) API 9. Database (Trove) API 10. Bare Metal (Ironic) API 11. DNS
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。