Poechant 练习 Java API - 包装类
2012-03-20 00:00
351 查看
利用三个包装类编写一个计算三个整数平均值的程序。程序首先提示用户输入3个整数,然后将3个整数分别赋予3个Integer对象。利用适当的包装类方法将3个整数转换成整数值。将其平均值包装到一个Double对象。最后打印这三个整数值以及平均值。程序将继续运行直到用户打入“n”。
$(document).ready(function(){dp.SyntaxHighlighter.HighlightAll('code');});
原文链接:
http://blog.csdn.net/poechant/article/details/6920043
import java.util.Scanner; public class test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); Integer in[] = new Integer[3]; int count = 0; boolean isValid = false; while(!isValid){ if (sc.hasNextInt()) { String str = sc.next(); in[count++] = Integer.parseInt(str); if (count == 3) { Double dou = new Double((in[0].intValue() + in[1].intValue() + in[2].intValue()) / 3.0); System.out.print(in[0].intValue() + "\t" + in[1].intValue() + "\t" + in[2].intValue()); System.out.println("\t" + dou.doubleValue()); sc.nextLine(); count = 0; } } else if (sc.next().equals("n")) { isValid = true; } else { System.out.println("Please input an integer or \"N\"."); sc.nextLine(); } } } }
$(document).ready(function(){dp.SyntaxHighlighter.HighlightAll('code');});
原文链接:
http://blog.csdn.net/poechant/article/details/6920043
相关文章推荐
- Poechant 练习 Java API - 包装类
- Poechant 练习 Java API - Y/N 输入验证
- Poechant 练习 Java API - Y/N 输入验证
- JAVA读取XML练习,JAVA读取XML文档,JAVA解析XML文档,JAVA与XML,XML文档解析,SAX (Simple API for XML)
- Java常用工具类API(Arrays类,Collections类,包装类,日期类,Math类,Systeml类)
- 编码练习——Java-String-API-练习
- 黑马程序员-JavaAPI-16天-8(TreeMap练习-字母出现的次数)
- 黑马程序员_JAVA常用类API(StringBuffer类,数组高级应用,包装类)
- 黑马程序员-JavaAPI-16天-7(TreeMap练习)
- hbase java Api练习
- java2d常用api练习
- Java基础学习笔记十四 常用API之基本类型包装类
- Java常用API----包装类
- Poechant 解读 Java API —— 借助货币格式化,初识 Locale 和 NumberFormat
- Java核心API -- 5(包装类、BigDecimal、BigInteger)
- 编码练习——Java-数组-API-练习
- 【Hadoop】HBase Java API 练习
- 编码练习——Java-数组-API-练习
- Java api 入门教程 之 JAVA的包装类
- Java笔记九.常用APIの数据类型包装类