1.水仙花数是一个三位数
2.水仙花数的个位,十位,百位的数字立方和等于原数
public static void main(String[] args) {
for(int i=100;i<1000;i++) {
//获取个位数
int g = i%10;
//获取十位数
int s = i/10%10;
//获取百位数
int b = i/100;
if(Math.pow(g, 3)+Math.pow(s, 3)+Math.pow(b, 3)==i) {
//输出水仙花数
System.out.println(i);
}
}
}
案例2:猜数字游戏
需求:系统随机生成一个0~100之间的随机数,让玩家猜,并提示大了还是猜小了,
直到猜对为止。
public static void main(String[] args) {
long time = System.currentTimeMillis();
long number = time%101;
System.out.println(number);
Scanner scan = new Scanner(System.in);
System.out.println("开始猜吧。。。");
int count = 0;
int answer;
do{
count++;
answer = scan.nextInt();
if(answer > number) {
System.out.println("猜大了");
}else if(answer < number){
System.out.println("猜小了");
}else {
System.out.println("猜对了");
System.out.println("猜了"+count+"次!");
break;//跳出循环
}
}while(answer!=number);
scan.close();
}
案例3:随机加法出题程序
需求:系统随机出10道两位正整数以内的加法题目,让用户作答;
答对一题得10分,答错得分不扣分,满分100分;
答错的题目要提示正确答案,所有题目答题完毕后统计得分和答题时长
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("开始答题:");
Random rand = new Random();
int score = 0;
//开始计时
long start = System.currentTimeMillis();
for(int i=1;i<=10;i++) {
int n1 = rand.nextInt(101);
int n2 = rand.nextInt(101);
System.out.println("第"+i+"题:"+n1+"+"+n2+"=???");
int a1 = scan.nextInt();
if(a1==n1+n2) {
System.out.println("答对了,加10分!");
score+=10;
}else {
System.out.println("很遗憾答错了!");
System.out.println("正确答案:"+n1+"+"+n2+"="+(n1+n2));
}
}
//终止计时
long end = System.currentTimeMillis();
long time = (end-start)/1000;
System.out.println("得分:"+score+"分");
System.out.println("用时:"+time+"秒");
scan.close();
}
案例4:有数列为:9,99,999,…,9999999999。要求使用程序计算此数列的和。
public static void main(String[] args) {
long num=9;
long sum=0;
for(int i=1;i<=10;i++){
sum=sum+num;
num=num*10+9;
}
System.out.println(sum);
}
案例5:操场上有不到100人,三人一组多1人,四人一组多2人,五人一组多3人,
共多少人?
public static void main(String[] args) {
for(int i=0;i<100;i++){
if(i%3==1 && i%4==2 && i%5==3){
System.out.println("操场上有"+i+"人");
}
}
}
案例6:输入年份和月份,打印该年当月的日历
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("请输入您要查询的年份:");
int year = scan.nextInt();
System.out.println("请输入您要查询的月份:");
int month = scan.nextInt();
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
for(int i=1;i<=31;i++) {
System.out.print(i+"t");
if(i%7==0){
System.out.println();
}
}
break;
case 4:
case 6:
case 9:
case 11:
for(int i=1;i<=30;i++) {
System.out.print(i+"t");
if(i%7==0){
System.out.println();
}
}
break;
case 2:
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
for(int i=1;i<=29;i++) {
System.out.print(i+"t");
if(i%7==0){
System.out.println();
}
}
} else {
for(int i=1;i<=28;i++) {
System.out.print(i+"t");
if(i%7==0){
System.out.println();
}
}
}
break;
default:
System.out.println("您输入的月份有误!");
}
scan.close();
}
案例7:打印菱形
public static void main(String[] args) {
for (int i = 1; i <= 4; i++) {
// 打印空白的倒立三角形的每一行
for (int j = 1; j <= 4 - i; j++) {
System.out.print(' ');
}
// 打印正立星星三角形的每一行
for (int j = 1; j <= 2 * i - 1; j++) {
System.out.print("*");
}
// 换行
System.out.println();
}
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(' ');
}
// 打印倒立星星三角形的每一行
for (int j = 1; j <= 7 - 2 * i; j++) {
System.out.print("*");
}
// 换行
System.out.println();
}
}
案例8:输入一行字符,分别统计出其英文字母、空格、数字和其它字符的个数
程序分析:
1、获取一行字符串,nextLine()
2、把字符串的每一个字符赋值到一个数值中
3、对比每一个数值在ASK码的范围,就可以确定它符号的类别
4、char字符ASK码的范围
(1)数字0到9: 48~57
(2)字母A到Z:65到90 a到z:97到122
(3)空格是32
public static void main(String[] args) {
int num = 0;// 数字的个数
int letter = 0;// 字母的个数
int space = 0;// 空格的个数
int others = 0;// 其他的个数
System.out.println("请输入一串字符:");
Scanner scan = new Scanner(System.in);
// 获取一行字符串
String string = scan.nextLine();
// 把字符串里面的值赋值给一个字符型数组
char[] arr = string.toCharArray();
// 遍历字符串里面的所有值
for (int i = 0; i < arr.length; i++) {
// 字符是数字
if (arr[i] >= 48 && arr[i] <= 57) {
num++;
// 字符是字母
} else if ((arr[i] >= 65 && arr[i] <= 90) || (arr[i] >= 97 && arr[i] <= 122)) {
letter++;
// 字符是空格
} else if (arr[i] == 32) {
space++;
} else {
others++;
}
}
System.out.println("数字:" + num + "个,字母:" + letter + "个,空格:" + space + "个,其他:" + others + "个");
scan.close();
}
案例9:输入某年某月某日,判断这一天是这一年的第几天
程序分析:
以3月9日为例,应该先把前两个月的加起来,然后再加上9天即本年的第几天,若为闰年且输入月份大于2时需多加一天。
public static void main(String[] args) {
System.out.println("请输入年月日(用空格隔开):");
Scanner scan = new Scanner(System.in);
int year = scan.nextInt();
int month = scan.nextInt();
int day = scan.nextInt();
// 统计天数变量
int sum = 0;
// 创建月份天数的数组
int[] arr = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
// 统计整月产生的天数
for (int i = 1; i < month; i++) {
sum += arr[i - 1];
}
// 闰年且月份大于2月份加一天
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0 && month > 2) {
sum += 1;
}
// 加上日期中天数
sum += day;
System.out.println(year + "年" + month + "月" + day + "日,是" + year + "年的第" + sum + "天");
scan.close();
}
案例10:双色球 6颗红球(33选1) 1颗蓝球(16选1)
public static void main(String[] args) {
String[] pool = { "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15",
"16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32",
"33" };
// 用一个数组存放6个蓝色的球
String[] balls = new String[6];
int length = 0;
boolean[] used = new boolean[pool.length];
Random rand = new Random();
while (true) {
int index = rand.nextInt(pool.length);
// 说明该下标已经被使用过
if (used[index])
continue;
// 把选中的球放入球数组
balls[length++] = pool[index];
// 把选中的球标识为已经使用过
used[index] = true;
// 下标等于6红球选好
if (length == balls.length)
break;
}
// 排序红球
Arrays.sort(balls);
// 扩容,最后一个放蓝色球
balls = Arrays.copyOf(balls, balls.length + 1);
balls[balls.length - 1] = pool[rand.nextInt(16)];
// 输出双色球数组
System.out.println(Arrays.toString(balls));
}



