2013年1月9日星期三
第9讲.面向对象之封装
第10讲.面向对象之封装.续
《Java核心技术卷一》p40~p50
测试
- 编写程序,产生两个(最好是随机产生的),并提示用户输入这两个数的和,最后判断用户的计算是否错误。
- 编写程序,提示用户输入10个1000以内的数字,最后将用户输入的这十个数字从小到大依此排列显示出来。
1 import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class shuxinjie
{
public static void main(String[] args)
{
Random randomNum1 = new Random();
Random randomNum2 = new Random();
int num1 = randomNum1.nextInt(100);
int num2 = randomNum2.nextInt(100);
System.out.print("加法运算:\n" + num1 + "+" + num2 + "=");
while (true)
{
Scanner input1 = new Scanner(System.in);
int result = input1.nextInt();
if (result == (num1 + num2))
{
System.out.println("you are good!\n");
break;
}
else
{
System.out.print("soory.you lost.please.Try it again!\n" + num1 + "+" + num2 + "=");
}
}
}
}
2 import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class shuxinjie1
{
public static void main(String[] args)
{
System.out.print("请输入要排序的的0~1000以内的数的个数:");
Scanner input3 = new Scanner(System.in);
int n = input3.nextInt();
System.out.println("请输入这组数:");
Scanner input2 = new Scanner(System.in);
int num[] = new int[n];
for (int i = 0; i < n; i++)
{
num[i] = input2.nextInt();
}
Arrays.sort(num);
System.out.println("这组数从小到大输出的结果是:");
for (int i = 0; i < n; i++)
{
System.out.print(num[i] + " ");
}
}
}