首页 > 编程语言 > 详细

Java练习一

时间:2019-09-09 10:26:17      阅读:62      评论:0      收藏:0      [点我收藏+]

Java编码示例

各种java程序来说明多种概念。

  • 输出hello world
    /Hello,world
    /
    public class HelloWorld{
    public static void main(String[] args){
    System.out.println("Hello,world");
    }
    }
  • 如何在同一个类中调用方法的示例

    /CallingMethodsInSameClass.java
    说明如何在同一个类中调用静态方法
    */
    public class CallingMethodsInSampleClass{
    public static void main(String[] args){
    printOne();
    printOne();
    printTwo();
    }

      public static void printOne(){
          System.out.println("Hello world");
      }
    
      public static void printTwo(){
          printOne();
      }

    }

  • 使用for循环计算阶乘的简单示例。使用内置的int数据类型只有显示13!
    public class Factorial{
    public static void main(String[] args){
    final int NUM_FACTORS = 100;
    for(int i < 0; i < NUM_FACTORS; i ++)
    {
    System.out.println(i + "! is" + factorial(i));
    }
    }

        public static int factorial(int n){
            int result = 1;
            for(int i = 2;i < =n ;i++){
                result *= i;
            }
            return result;
        }
    }
  • 增强的循环
    public class EnhancedFor{
    public static void main(String[] args){
    int[] list = {1,2,3,4,5,6,7,8,9};
    int sum = sumListEnhanced(list);
    System.out.println("Sum of elements in list: " + sum);

          System.out.println("Original List");
          printList(list);
          System.out.println("Calling addOne");
          addOne(list);
          System.out.println("List after call to addOne");
          printList(list);
          System.out.println("Calling addOneError");
          addOneError(list);
          System.out.println("List after call to addOneErrro.Noete elements of list did not change");
          printList(list);
      }
      //使用加强的循环语句,进行求和
      public static int sumListEnhanced(int[] list){
          int total = 0;
          for(int var:list){
              total += var;
          }
          return total;
      }
    
      //使用传统的循环,进行求和
      public static int sumListold(int[] list){
          int total = 0;
          for(int i = 0; i < list.length; i++){
              total += list[i];
              System.out.println(list[i]);
          }
          return total;
      }
    
      //下面的代码好像是给每个元素加1,实际上并没有
      public static void addOneError(int[] list){
    
          for(int val:list){
              val += 1;
          }
      }
    
      //给每个元素加1
      public static void addOne(int[] list){
          for(int i = 0; i < list.length; i++){
              list[i] += 1;
          }
      }
    
      //打印每个数组
      public static void printList(int[] list){
          System.out.println("index, value");
          for(int i = 0; i<list.length;i++){
              System.out.println(i + ", "+ list[i]);
          }
      }
    }
  • 显示值参数行为的例子。在Java中,所有参数都是按照值进行传递
    public class PrimitiveParameters{
    public static void main(String[] args){
    go();
    }

      public static void go(){
          int x = 3;
          int y = 2;
          System.out.println("In method go. x:" + x +" y: "+y);
          falseSwap(x,y);
          System.out.println("In method go. x:" + x +" y: "+y);
          moreParameters(x,y);
          System.out.println("In method go. x:" + x+ "  y: "+y);
      }
    
      public static void falseSwap(int x,int y){
          System.out.println("In method go. x:" + x +" y: "+y);
          int temp = x;
          x = y;
          y = temp;
          System.out.println("in method falseSwap. x:"+x + "y:"+y);
      }
    
      public static void moreParameters(int a, int b){
    
          System.out.println("in method falseSwap. a:"+a + " b:"+b);
          a = a*b;
          b = 12;
          System.out.println("in method falseSwap. a:"+a + " b:"+b);
          falseSwap(b,a);
          System.out.println("in method falseSwap. a:"+a + " b:"+b);
      }
    }
    
    //下面是执行结果
    In method go. x:3 y: 2   //初始化x=3,y=2
    In method go. x:3 y: 2   //执行falseSwap函数,以在函数内成功调整两数值
    in method falseSwap. x:2y:3
    In method go. x:3 y: 2 //执行完falseSwap函数并没有交换两值
    in method falseSwap. a:3 b:2 //执行moreParameters
    in method falseSwap. a:6 b:12
    In method go. x:12 y: 6
    in method falseSwap. x:6y:12
    in method falseSwap. a:6 b:12
    In method go. x:3  y: 2 //对两个实际值并没有改变
  • 一些String操作的简短例子
    public class StringExample{
    public static void main(String[] args){
    String s1 = "Computer Science";
    int x = 307;
    String s2 = s1 + " " +x;
    String s3 = s2.substring(10,17);
    String s4 = "is fun";
    String s5 =s2 + s4;

          System.out.println("s1: "+s1);
          System.out.println("s2: "+s2);
          System.out.println("s3: "+s3);
          System.out.println("s4: "+s4);
          System.out.println("s5: "+s5);
    
          x = 3;
          int y = 5;
          String s6 = x + y + "total";
          String s7 = "total" + x + y;
          String s8 = ""+x+y + "total";
          System.out.println("s6: "+s6);
          System.out.println("s7: "+s7);
          System.out.println("s8: "+s8);
    
      }
    }
    
    //执行结果
    //Java编译器是从左到右处理表达式的,s6第一个表达式x+y的话就是Java编译器理解为运算符,而第二和第三则理解为连接符
    s1: Computer Science
    s2: Computer Science 307
    s3: cience 
    s4: is fun
    s5: Computer Science 307is fun
    s6: 8total
    s7: total 35
    s8: 35 total
  • 一个程序,包含各种Java语法的示例,它将10进制转化2进制
    public class BinaryConverter{
    public static void main(String[] args){
    for(int i = -5;i<32;i++){
    System.out.println(i+": "+ toBinary(i));
    System.out.println(i);
    System.out.println(i+": "+Integer.toBinaryString(i));
    }
    }

      //在base2中返回一个base10 num的字符串
      public static String toBinary(int base10Num){
          boolean isNeg = base10Num < 0; // 优先级,先计算<,base10Num小于0为真
          base10Num = Math.abs(base10Num); //返回绝对值Math.abs()
          String result = ""; 
    
          while(base10Num > 1){
                //以base10Num等于11为例子,result = 11%2+ ' ' = '1'
                //base10Num/2 = 5
                //result = 5%2 + '1'='11'
                //base10Num/2=1
                //result = 1%2 +'11'='011'
                //base10Num = 1 
              result = (base10Num %2) + result; 
              base10Num /= 2;
          }
            //断言 asser condition:expr;检查值是否等于0或1
          assert base10Num == 0 || base10Num == 1:"value is not <= 1: "+base10Num;
    
            //还是以11为例,经过上面的循环,result='011',base10Num=1
            //最终result=1011
          result = base10Num + result;
    
            //断言
          assert all0sAndls(result);
    
            //如果是false,不执行。正数不执行,负数执行
          if( isNeg )
              result = "-" + result;
    
          return result;
      }
    
    
        public static boolean all0sAndls(String val){
          //判断result是否有结果
            assert val != null: "Failed precondition all0sAndls.parameter cannot be null";
          boolean all = true;
          int i = 0;
          char c;
            //判断每个位置是否是0或1
          while(all && i < val.length()){
                //charAt() 方法用于返回指定索引处的字符。
              c = val.charAt(i);
              all = c == '0' || c == '1';
              i++;
          }
          return all;
      }
    }

Java练习一

原文:https://www.cnblogs.com/yihang996/p/11489641.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!