Java程序设计之经典样例

简介: 小知识,大挑战

例子1字符型变量


public class  CharacterTest {
    public static void main (String args[ ]) {
char chinaWord='你',japanWord='ぁ';
      int  p1=36328,p2=38358;
      System.out.println("汉字'你'在unicode表中的顺序位置:"+(int)chinaWord);
      System.out.println("日语'ぁ'在unicode表中的顺序位置:"+(int)japanWord);
      System.out.println("unicode表中第20328位置上的字符是:"+(char)p1);
      System.out.println("unicode表中第12358位置上的字符是:"+(char)p2);
    }
}
复制代码

例子2:数据类型转换


public classDataTypeTest {
public static void main (String args[ ]) {
  int c=2200;   
 long d=8000;
      float f;
      double g=123456789.123456789;
      c=(int)d;
      f=(float)g;   //导致精度的损失.
   System.out.print("c=  "+c);  
System.out.println("  d=  "+d);
      System.out.println("f=  "+f);
 System.out.println("g=  "+g);
    }
}
复制代码

例子3:使用异或对字符进行加密和解密


class XORTest {
   public static void main(String args[]){
   char a1='十',a2='点',a3='进',a4='攻';
     char secret='8';
     a1=(char)(a1^secret);  
     a2=(char)(a2^secret);
     a3=(char)(a3^secret);  
     a4=(char)(a4^secret);
     System.out.println("密文:"+a1+a2+a3+a4);
     a1=(char)(a1^secret);  
     a2=(char)(a2^secret);
     a3=(char)(a3^secret); 
     a4=(char)(a4^secret);
     System.out.println("原文:"+a1+a2+a3+a4);
    }
}
复制代码

例子4:短路逻辑或(||)和位运算(|)的区别


class OrTest {
    public static void main(String args[]) {
  int x,y=10;
       if(((x=0)==0)||((y=20)==20)) {
   System.out.println("现在y的值是:"+y);
       }
       int a,b=10;
       if(((a=0)==0)|((b=20)==20)) {
   System.out.println("现在b的值是:"+b);
       }
    }
}
复制代码

例子5:用if语句实现a、b、c的值按从小到大排序


 

public class SortABC{
      public static void main(String args[]){
     int a=9,b=5,c=7,t;
     if(a>b) {
        t=a; a=b; b=t;
     }
     if(a>c) {
       t=a; a=c; c=t;
     }
     if(b>c) {
       t=b; b=c; c=t;
     }
     System.out.println("a="+a+",b="+b+",c="+c);
   }
    }
复制代码

例子6:用if语句判断给定的成绩是否及格


public class Score {
  public static void main(String args[]){
  int math=65 ,english=85;
      if(math>=60) {
   System.out.println("数学及格了");
      }
      else {
    System.out.println("数学不及格");
      }
      if(english>90) {
   System.out.println("英语是优");
      }
      else {
   System.out.println("英语不是优");
      }
      System.out.println("我在学习控制语句");
    }
}
复制代码

例子7:switch语句的使用,当主程序执行时,如果第一个命令行参数的首字符分别是数字、小写字母及大写字母时,系统会显示这个首字符。如果输入的是非数字或字母,则显示不是数字或字母。


class Ex2_07 {
    public static void main(String[] args) {
        char ch = args[0].charAt(0);
        switch (ch) {
        case '0': case '1': case '2': case '3': case '4':
        case '5': case '6': case '7': case '8': case '9':
            System.out.println("The character is digit " + ch);
            break;
        case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
        case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
        case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
        case 'v': case 'w': case 'x': case 'y': case 'z':
            System.out.println("The character is lowercase letter " + ch);
            break;
        case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
        case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
        case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
        case 'V': case 'W': case 'X': case 'Y': case 'Z':
            System.out.println("The character is uppercase letter " + ch);
            break;
        default:
            System.out.println("The character " + ch
                     + " is neither a digit nor a letter.");
        }
    }
}
复制代码

例子8:使用for循环,计算 5+ 55 + 555 +  。。。 的前10项的和


public class Example3_6{
   public static void main(String args[]){
     long sum=0,a=5,item=a,n=10,i=1;
        for(i=1;i<=n;i++) {
     sum=sum+item;
             item=item*10+a; 
         }
        System.out.println(sum);
     }
}
复制代码

例子9:使用while循环,计算 1 + 1/2! + 1/3! + 1/4! +   + 1/20! 的值


class Example3_7 {
     public static void main(String args[]) {
     double sum=0,a=1;
int i=1;
         while(i<=20) {
   sum=sum+a;
             i=i+1;
             a=a*(1.0/i);        
         }
         System.out.println("sum="+sum);
     }
}
复制代码

例子10:计算给定整数的各数字的和


class Ex2_10 {
    public static void main(String args[]) {
         int x = 12345;
         int y=x;
         int r=0;
         int sum = 0;
         while(y!=0) {
             r = y % 10;
             sum += r;
             y = y / 10;          
         }
         System.out.println("x -> " + sum);
    }
}
复制代码

例子11:break和continue使用举例,分别计算10以内的奇数的和,计算50以内的素数


class Example3_8 {
   public static void main(String args[]){
      int sum=0,i,j;
      for( i=1;i<=10;i++){
         if(i%2==0)            //计算1+3+5+7+9
              continue;   
         sum=sum+i;
      }
      System.out.println("sum="+sum);
      for(j=2;j<=50;j++) {   //求50以内的素数
          for( i=2;i<=j/2;i++) {
             if(j%i==0)
                break;
          }
          if(i>j/2) {
             System.out.println(""+j+"是素数");
           }
       }     
   }
}
复制代码

例子11:一维数组举例,输出一维整型数组中的值最小的那个元素及其下标)


public class ArrayTest {
public static void main(String args[]) {
      int a[] = { 52, 78, 90, 34, 16, 34, 67 };
      int indexOfMinElement = 0;
      for (int i = 1; i < a.length; i++) {
          if (a[indexOfMinElement] > a[i]) {
              indexOfMinElement = i;
          }
      }
      System.out.println("a[" + indexOfMinElement + "] = "
              + a[indexOfMinElement]);
}
}
复制代码

例子12:计算二维数组中各行元素之和并查找其值最大的那个行


public class TableTester {
  public static void main(String args[]) {
       int myTable[][] = {
           {23, 45, 65, 34, 21, 67, 78},
           {46, 14, 18, 46, 98, 63, 88},
           {98, 81, 64, 90, 21, 14, 23},
           {54, 43, 55, 76, 22, 43, 33}};
       int sum, max, maxRow=0;
       max = 0; //Assume all numbers are positive
       for (int row=0; row<4; row++) {
           sum = 0;
           for (int col=0; col<7; col++)
               sum += myTable[row][col];
           if (sum > max) {
               max = sum;
               maxRow = row;
           }
       }
       System.out.println("Row " + maxRow + " has the highest sum of " + max);
  }
}


作者:zhulin1028

著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

相关文章
|
6天前
|
Java
【专栏】Java反射机制,该机制允许程序在运行时获取类信息、动态创建对象、调用方法和访问属性
【4月更文挑战第27天】本文探讨了Java反射机制,该机制允许程序在运行时获取类信息、动态创建对象、调用方法和访问属性。反射通过Class、Constructor、Method和Field类实现。文中列举了反射的应用场景,如动态创建对象、调用方法、访问属性和处理注解,并提供了相关实例代码演示。
|
6天前
|
Java Maven
【Java报错】显示错误“Error:java: 程序包org.springframework.boot不存在“
【Java报错】显示错误“Error:java: 程序包org.springframework.boot不存在“
44 3
|
1天前
|
缓存 算法 Java
如何提高Java程序的性能?
Java是一种非常流行的编程语言,但是在处理大规模数据和高并发时,程序性能容易受到影响。本文将分享一些提高Java程序性能的技巧。
|
6天前
|
搜索推荐 算法 Java
滚雪球学Java(29):数组长度和排序算法:让你的程序更高效
【5月更文挑战第4天】🏆本文收录于「滚雪球学Java」专栏,专业攻坚指数级提升,希望能够助你一臂之力,帮你早日登顶实现财富自由🚀;同时,欢迎大家关注&&收藏&&订阅!持续更新中,up!up!up!!
13 0
滚雪球学Java(29):数组长度和排序算法:让你的程序更高效
|
6天前
|
前端开发 Java 应用服务中间件
【异常解决】java程序连接MinIO报错The request signature we calculated does not match the signature you provided.
【异常解决】java程序连接MinIO报错The request signature we calculated does not match the signature you provided.
25 0
|
6天前
|
Java Linux C语言
一步带你了解java程序逻辑控制
一步带你了解java程序逻辑控制
17 2
|
6天前
|
Java 数据安全/隐私保护
java中程序控制的典例
java中程序控制的典例
13 1
|
6天前
|
存储 Java 数据库连接
使用Java开发桌面应用程序
使用Java开发桌面应用程序
25 0
|
6天前
|
关系型数据库 MySQL Java
通过使用阿里云服务器,搭建Java程序的运行环境
通过使用阿里云服务器,搭建Java程序的运行环境
|
6天前
|
存储 网络协议 Java
本地MinIO存储服务通过Java程序结合cpolar实现远程连接上传文件
本地MinIO存储服务通过Java程序结合cpolar实现远程连接上传文件