猜数字游戏,水仙花数

来源:互联网 发布:男士护肤 知乎 编辑:程序博客网 时间:2024/05/17 09:05
猜数字游戏
import java.util.Random;import java.util.Scanner;    class Demo{        public static void main(String[] args){            Scanner input = new Scanner(System.in);            Random r = new Random();            System.out.println("-------------- 欢迎访问猜数字游戏 -----------");            System.out.println("-------------- 请输入一个0-5的数字 -----------");            //接收用户输入的内容            int a = input.nextInt();            //随机产生一个0-5的数字            int b = r.nextInt(6);            System.out.print("系统产生的数字为:");            System.out.print(b);            System.out.println("结果为:");            System.out.println(a==b);        }    }
水仙花数
  1. public class Demo {  
  2.     public static void main(String[] args) {  
  3.     <span style="white-space:pre">  </span>for(int i=100;i<1000;i++){  
  4.         //判断i是否为水仙花数 , 如果是则打印  
  5.         <span style="white-space:pre">  </span>int a = i/100;//百位数  
  6.             int b = i%100/10;//十位数  
  7.             int c = i%10;//个位数  
  8.             int sum = a*a*a+b*b*b+c*c*c;  
  9.             if(sum == i){  
  10.             System.out.println("找到了一个水仙花数:"+i);  
  11.             }  
  12.           
  13.         }  
  14.     }  
  15. }  
原创粉丝点击