您的位置:首页 > 编程语言 > Java开发

Poechant 练习 Java API - 包装类

2012-03-20 00:00 351 查看
利用三个包装类编写一个计算三个整数平均值的程序。程序首先提示用户输入3个整数,然后将3个整数分别赋予3个Integer对象。利用适当的包装类方法将3个整数转换成整数值。将其平均值包装到一个Double对象。最后打印这三个整数值以及平均值。程序将继续运行直到用户打入“n”。

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: