java实践:小游戏:猜数字、判断回文数 。

猜数字小游戏源码:

import java.util.*;
public class caishuzi
{     
     public static void main (String args[ ])
     { 
           System.out.println("给你一个1至100之间的整数,请猜测这个数");
           int realNumber=(int)(Math.random()*100)+1;
           int yourGuess=0;
           Scanner reader=new Scanner(System.in);     
           System.out.println("输入您的猜测:");
            yourGuess=reader.nextInt();
       while ( yourGuess!=realNumber)  
       {    

         if (yourGuess>realNumber)  
          {
              System.out.println("猜大了,请再猜:");
             yourGuess=reader.nextInt(); 
           }
         else if(yourGuess
判断是否是回文数的源码:

import java.util.*;
public class Number
{  
    public static void main(String args[])
    {
        int number=0;
	    int d5=0;
	    int d4=0;
	    int d3=0;
	    int d2=0;
	    int d1=0;
        Scanner reader=new Scanner(System.in);     
        System.out.println("从键盘输入一个1至99999之间的数");
        while(reader.hasNextInt())  
         {
           number=reader.nextInt();
           if(number>=1&&number<=99999)    
            {
            d5=number%100000/10000;   
            d4=number%10000/1000; 
             d3=number%1000/100;   
                d2=number%100/10;
                d1=number%10;
              if(number>=10000&&number<=99999)  
                {
                  System.out.printf("\n%d是5位数", number);
                  if (d1==d5&&d2==d4)  
                   {
                     System.out.printf("\t%d是回文数", number);
                   }
                  else
                   { 
                     System.out.printf("\t%d不是回文数", number);
                   }
                }
              else if (number>=1000&&number<=9999)   
                {
                   System.out.printf("\n%d是4位数",number);
                   if (d1==d4&&d2==d3)  
                   {
                      System.out.printf("\t%d是回文数", number);
                   }
                  else
                   { 
                     System.out.printf("\t%d不是回文数",number);
                   }
                }
                else if (number>=100&&number<=999)  
                {
                   System.out.printf("\n%d是3位数", number);
                   if (d1==d3) 
                   {
                      System.out.printf("\t%d是回文数", number);
                   }
                  else
                   { 
                     System.out.printf("\t%d不是回文数", number);
                   }
                }
             else if(d2!=0)
                {
                   System.out.printf("\n%d是2位数", number);
                   if(d1==d2)
                   {
                      System.out.printf("\t%d是回文数", number);
                   }
                  else
                   { 
                     System.out.printf("\t%d不是回文数", number);
                   }
                }
             else if(d1!=0)
                {
                   System.out.printf("\n%d是1位数", number);
                   System.out.printf("\t%d是回文数", number);
                }
      
         else
            {
               System.out.printf("\n%d不在1至99999之间", number);
            }
         System.out.printf("%n可继续输入整数, 或输入非整数结束程序%n");
        }
         }
      System.out.println("你输出的数据不是整数");
    }
 
    }


你可能感兴趣的:(java基础实践,猜数字游戏,回文数)