




已阅读5页,还剩3页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Java 基本输入输出入门 一、Java 基本开发环境 1. JDK 1.5 以上 编译运行环境 2. Ecllipse 3.2 以上开发环境 二、Java 之输入输出处理 由于 ACM 竞赛题目的输入数据和输出数据一般有多组(不定) ,并 且格式多种多样,所以,如何处理题目的输入输出是对大家的一项 最基本的要求。这也是困扰初学者的一大问题。 1. 输入: 格式 1:Scanner sc = new Scanner (new BufferedInputStream(System.in); 格式 2:Scanner sc = new Scanner (System.in); 在读入数据量大的情况下,格式 1 的速度会快些。 读一个整数: int n = sc.nextInt(); 相当于 scanf(“%d“, 或 cin n; 读一个字符串:String s = sc.next(); 相当于 scanf(“%s“, s); 或 cin s; 读一个浮点数:double t = sc.nextDouble(); 相当于 scanf(“%lf“, 或 cin t; 读一整行: String s = sc.nextLine(); 相当于 gets(s); 或 cin.getline(.); 判断是否有下一个输入可以用 sc.hasNext()或 sc.hasNextInt()或 sc.hasNextDouble()或 sc.hasNextLine() 例 1:读入整数 Input 输入数据有多组,每组占一行,由一个整数组成。 Sample Input 56 67 100 123 import java.util.Scanner; public class Main public static void main(String args) Scanner sc =new Scanner(System.in); while(sc.hasNext() /判断是否结束 int score = sc.nextInt(); /读入整数 。 例 2:读入实数 输入数据有多组,每组占 2 行,第一行为一个整数 N,指示第二行包含 N 个实数。 Sample Input 4 56.9 67.7 90.5 12.8 5 56.9 67.7 90.5 12.8 import java.util.Scanner; public class Main public static void main(String args) Scanner sc =new Scanner(System.in); while(sc.hasNext() int n = sc.nextInt(); for(int i=0;i2) days +; days += d; for(int i=0;im;i+) days += ddi; System.out.println(days); 练习:熟习使用Java输入数据 /showproblem.php?pid=1089 /showproblem.php?pid=1090 /showproblem.php?pid=1091 /showproblem.php?pid=1092 /showproblem.php?pid=1093 /showproblem.php?pid=1094 2. 输出 函数: System.out.print(); System.out.println(); System.out.format(); System.out.printf(); 例4 杭电1170Balloon Comes! Give you an operator (+,-,*, / -denoting addition, subtraction, multiplication, division respectively) and two positive integers, your task is to output the result. Input Input contains multiple test cases. The first line of the input is a single integer T (0T1000) which is the number of test cases. T test cases follow. Each test case contains a char C (+,-,*, /) and two integers A and B(0A,B10000).Of course, we all know that A and B are operands and C is an operator. Output For each case, print the operation result. The result should be rounded to 2 decimal places If and only if it is not an integer. Sample Input 4 + 1 2 - 1 2 * 1 2 / 1 2 Sample Output 3 -1 2 0.50 import java.util.Scanner; public class Main public static void main(String args) Scanner sc =new Scanner(System.in); int n = sc.nextInt(); for(int i=0;in;i+) String op = sc.next(); int a = sc.nextInt(); int b = sc.nextInt(); if(op.charAt(0)=+) System.out.println(a+b); else if(op.charAt(0)=-) System.out.println(a-b); else if(op.charAt(0)=*) System.out.println(a*b); else if(op.charAt(0)=/) if(a % b = 0) System.out.println(a / b); else System.out.format(“%.2f“, (a / (1.0*b). Println(); 3. 规格化的输出: 函数: / 这里0指一位数字,#指除0以外的数字(如果是0,则不显示),四舍五入. DecimalFormat fd = new DecimalFormat(“#.00#“); DecimalFormat gd = new DecimalFormat(“0.000“); System.out.println(“x =“ + fd.format(x); System.out.println(“x =“ + gd.format(x); public static void main(String args) NumberFormat formatter = new DecimalFormat( “000000“); String s = formatter.format(-1234.567); / -001235 System.out.println(s); formatter = new DecimalFormat( “#“); s = formatter.format(-1234.567); / -1235 System.out.println(s); s = formatter.format(0); / 0 System.out.println(s); formatter = new DecimalFormat( “#00“); s = formatter.format(0); / 00 System.out.println(s); formatter = new DecimalFormat( “.00“); s = formatter.format(-.567); / -.57 System.out.println(s); formatter = new DecimalFormat( “0.00“); s = formatter.format(-.567); / -0.57 System.out.println(s); formatter = new DecimalFormat( “#.#“); s = formatter.format(-1234.567); / -1234.6 System.out.println(s); formatter = new DecimalFormat( “#.#“); s = formatter.format(-1234.567); / -1234.567 System.out.println(s); formatter = new DecimalFormat( “.#“); s = formatter.format(-1234.567); / -1234.567 System.out.println(s); formatter = new DecimalFormat( “#.000000“); s = formatter.format(-1234.567); / -1234.567000 System.out.println(s); formatter = new DecimalFormat( “#,#,#“); s = formatter.format(-1234.567); / -1,235 System.out.println(s); s = formatter.format(-1234567.890); / -1,234,568 System.out.println(s); / The ; symbol is used to specify an alternate pattern for negative values formatter = new DecimalFormat( “#;(#) “); s = formatter.format(-1234.567); / (1235) System.out.println(s); / The symbol is used to quote literal symbols formatter = new DecimalFormat( “ # # “); s = formatter.format(-1234.567); / -#1235 System.out.println(s); formatter = new DecimalFormat( “ abc # “); s = formatter.format(-1234.567); / - abc 1235 System.out.println(s); formatter = new DecimalFormat( “#.#%“); s = formatter.format(-12.5678987); System.out.println(s); 4. 字符串处理 String String 类用来存储字符串,可以用charAt方法来取出其中某一字节,计数从0开始: String a = “Hello“; / a.charAt(1) = e 用substring方法可得到子串,如上例 System.out.println(a.substring(0, 4) / output “Hell“ 注意第2个参数位置上的字符不包括进来。这样做使得 s.substring(a, b) 总是有 b-a个字符。 字符串连接可以直接用 + 号,如 String a = “Hello“; String b = “world“; System.out.println(a + “, “ + b + “!“); / output “Hello, world!“ 如想直接将字符串中的某字节改变,可以使用另外的StringBuffer类。 5. 高精度 BigInteger和BigDecimal可以说是acmer选择java 的首要原因。 函数:add, subtract, divide, mod, compareTo等,其中加减乘除模都要求是 BigInteger(BigDecimal)和BigInteger(BigDecimal)之间的运算,所以需要把int(double)类型转 换为BigInteger(BigDecimal),用函数BigInteger.valueOf(). 例程: import java.io.BufferedInputStream; import java.math.BigInteger; import java.util.Scanner; public class Main public static void main(String args) Scanner cin = new Scanner (new BufferedInputStream(System.in); int a = 123, b = 456, c = 7890; BigInteger x, y, z, ans; x = BigInteger.valueOf(a); y = BigInteger.valueOf(b); z = BigInteger.valueOf(c); ans = x.add(y); System.out.println(ans); ans = z.divide(y); System.out.println(ans); ans = x.mod(z); System.out.println(ans); if (pareTo(x) = 0) System.out.println(“1“); h
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025版汽车租赁合同细则-年度车型升级版
- 2025版企业ERP系统采购与服务全面合作协议
- 2025年二手房过户及房屋买卖合同解除协议
- 2025年度酒店客房智能设备采购与安装服务合同范本
- 2025茶艺主题公园投资建设合作框架协议
- 2025版全新泥水工施工材料采购合同
- 2025版教育培训机构招生合作合同
- 2025年度房产抵押贷款贷前调查与风险控制合同
- 2025版水面旅游开发承包合同
- 2025版挖掘机销售与承包服务合同规范21
- 公共邮箱使用管理办法
- 农贸市场可行性研究报告
- 2025东风汽车集团有限公司全球校园招聘笔试参考题库附带答案详解
- 铝格栅墙面安装方案
- 浙江首考2025年1月普通高等学校招生全国统一考试政治试卷(含答案)
- 2025至2030肥厚型心肌病(HCM)治疗学行业发展趋势分析与未来投资战略咨询研究报告
- 水利工程监理单位安全生产责任制
- 2025届江苏苏州中考语文真题试卷【含答案】
- 油漆涂料安全培训
- 2025版心肺复苏术指南
- 高一生物实验教学跨学科融合计划
评论
0/150
提交评论