zoukankan      html  css  js  c++  java
  • 01Java经典问题

    1.利用Dos输出hello world

    建立一个Test.java文件,放在e盘;

    public class Test{
        public static void main(String[] args){
            System.out.println("hello world");
        }
    }

    进入e盘所在目录

    注意:javac后面是文件Test.java,而java后面是Test(类名)。

    2.Scanner中nextLine()方法和next()方法的区别

    import java.util.Scanner;
    
    /*
     * 中间都不带空格
     */
    public class ScannerString {
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
    
            System.out.println("请输入字符串(nextLine):");
            String strNextLine = input.nextLine();
    
            System.out.println("请输入字符串(next):");
            String strNext = input.next();
    
            System.out.println("strNextLine:" + strNextLine);
            System.out.println("strNext:" + strNext);
        }
    }

    以上结果都没有问题。

    import java.util.Scanner;
    
    /*
     * nextLine与next都有空格
     */
    public class ScannerString {
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
    
            System.out.println("请输入字符串(nextLine):");
            String strNextLine = input.nextLine();
    
            System.out.println("请输入字符串(next):");
            String strNext = input.next();
    
            System.out.println("strNextLine:" + strNextLine);
            System.out.println("strNext:" + strNext);
        }
    }

    next()方法读取到空白符就结束

    nextLine()读取到回车结束也就是“ ”

    import java.util.Scanner;
    
    /*
     * 第三次:我们将代码中next和nextLine的顺序调整一下,然后再进行测试
     */
    public class ScannerString {
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            System.out.println("请输入字符串(next):");
            String strNext = input.next();
    
            System.out.println("请输入字符串(nextLine):");
            String strNextLine = input.nextLine();
    
            System.out.println("strNext:" + strNext);
            System.out.println("strNextLine:" + strNextLine);
        }
    }

     修改顺序后遇到的问题就是因为next()读取到空白符前的数据时结束了,然后把回车“ ”留给了nextLine();所以上面nextLine()没有输出,不输出不代表没数据,是接到了空(回车“/r”)的数据。

    3.java异或运算

    public class XOR {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            int i = 15;
            int j = 2;
            System.out.println("i^j:"+(i^j));
        }
    }

    分析上面程序,i=15转成二进制是1111,j=2转成二进制是0010,根据异或的运算规则得到的是1101,转成十进制就是13.

    4.利用异或进行加密

    import java.util.Arrays;
    import java.util.Scanner;
    
    public class Password {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Scanner scanner =new Scanner(System.in);
            System.out.println("请输入一个字符串:");
            String password = scanner.nextLine();
            char[] array = password.toCharArray();
            System.out.println(Arrays.toString(array));
            for (int i = 0; i < array.length; i++) {
                array[i] = (char)(array[i]^20000);
            }
            System.out.println("加密结果如下:");
            System.out.println(Arrays.toString(array));
            for (int i = 0; i < array.length; i++) {
                array[i] = (char)(array[i]^20000);
            }
            System.out.println("解密的结果如下:");
            System.out.println(Arrays.toString(array));
        }
    
    }

    5.位运算实现乘法

    import java.util.Arrays;
    import java.util.Scanner;
    
    public class Example {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入一个数字:");
            long number = scanner.nextLong();
            System.out.println("该数字乘以2的运算符为:" + (number << 1));
            System.out.println("该数字乘以4的运算符为:" + (number << 2));
            System.out.println("该数字乘以8的运算符为:" + (number << 3));
            System.out.println("该数字乘以16的运算符为:" + (number << 4));
        }
    
    }

    6.实现两个变量的交换,不借助第三个变量

    import java.util.Arrays;
    import java.util.Scanner;
    
    public class Example {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入一个变量a的值:");
            long a = scanner.nextLong();
            System.out.println("请输入一个变量b的值:");
            long b = scanner.nextLong();
            System.out.println("a=" + a + " " + "b=" + b);
            System.out.println("执行变量交换");
            a = a ^ b;
            b = b ^ a;
            a = a ^ b;
            System.out.println("a=" + a + " " + "b=" + b);
        }
    
    }

    ① 执行前两个赋值语句:“a=a∧b;”和“b=b∧a;”相当于b=b∧(a∧b)。而b∧a∧b等于a∧b∧b(异或满足交换律,结合律)。b∧b的结果为0,因为同一个数与本身相∧,结果必为0。因此b的值等于a∧0,即a,其值为3。
    ② 再执行第三个赋值语句:a=a∧b。由于a的值等于(a∧b),b的值等于(b∧a∧b),因此,相当于a=a∧b∧b∧a∧b,即a的值等于a∧a∧b∧b∧b,等于b。
      a得到b原来的值

    7.判断是不是闰年

    import java.util.Arrays;
    import java.util.Scanner;
    
    /**
     * 判断一年是不是闰年:能被400整除或者能被4整除但不能被100整除
     */
    public class Example {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入一个年份:");
            int year = scanner.nextInt();
            if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
                System.out.println(year + "是闰年");
            } else {
                System.out.println(year + "不是闰年");
            }
    
        }
    
    }

    8.单例模式的应用

    class Emepror {
    
        private static Emepror emperor = null;
    
        private Emepror() {
    
        }
    
        public static Emepror getInstance() {
            if (emperor == null) {
                emperor = new Emepror();
            }
            return emperor;
        }
    
    }
    
    public class Test {
        public static void main(String[] args) {
            System.out.println("创建皇帝对象1");
            Emepror emepror1 = Emepror.getInstance();
            System.out.println("创建皇帝对象2");
            Emepror emepror2 = Emepror.getInstance();
            System.out.println("创建皇帝对象3");
            Emepror emepror3 = Emepror.getInstance();
            System.out.println(emepror1);
            System.out.println(emepror2);
            System.out.println(emepror3);
        }
    }

    9.构造方法的应用

    class Person {
        private String name;
        private String gender;
        private String age;
    
        public Person() {
            // TODO Auto-generated constructor stub
            System.out.println("使用无参的构造方法创建对象");
        }
    
        public Person(String name, String gender, String age) {
            this.name = name;
            this.gender = gender;
            this.age = age;
            System.out.println("使用有参的构造方法创建对象");
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getGender() {
            return gender;
        }
    
        public void setGender(String gender) {
            this.gender = gender;
        }
    
        public String getAge() {
            return age;
        }
    
        public void setAge(String age) {
            this.age = age;
        }
    
    }
    
    public class Test2 {
        public static void main(String[] args) {
            Person person1 = new Person();
            Person person2 = new Person("小明", "男", "24");
            System.out.println("员工1的信息:");
            System.out.println("员工姓名:" + person1.getName());
            System.out.println("员工性别:" + person1.getGender());
            System.out.println("员工年龄:" + person1.getAge());
            System.out.println("员工2的信息:");
            System.out.println("员工姓名:" + person2.getName());
            System.out.println("员工性别:" + person2.getGender());
            System.out.println("员工年龄:" + person2.getAge());
        }
    }

    10.重写equals方法

    class Cat {
        private String name;
        private int age;
        private double weight;
    
        public Cat(String name, int age, double weight) {
            this.name = name;
            this.age = age;
            this.weight = weight;
        }
    
        @Override
        public boolean equals(Object obj) {
            // TODO Auto-generated method stub
            if (this == obj)
                return true;
            if (obj == null)
                return false;
    
            if (getClass() != obj.getClass())
                return false;
            Cat cat = (Cat) obj;
            return name.equals(cat.name) && (age == cat.age) && (weight == cat.weight);
    
        }
    
        @Override
        public String toString() {
            return "Cat [name=" + name + ", age=" + age + ", weight=" + weight + "]";
        }
    
    }
    
    public class TestCat {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Cat cat1 = new Cat("java", 12, 1.0);
            Cat cat2 = new Cat("python", 12, 8.0);
            Cat cat3 = new Cat("java", 12, 1.0);
            System.out.println("cat1" + cat1);
            System.out.println("cat2" + cat2);
            System.out.println("cat3" + cat3);
            System.out.println("cat1与cat2相同吗?"+cat1.equals(cat2));
            System.out.println("cat1与cat3相同吗?"+cat1.equals(cat3));
        }
    
    }

    11.将一个int型数组转化成list类型

    package exam;
    
    import java.util.*;
    
    public class Main8 {
        public static void main(String[] args) {
            int[] a= {1,21,3,4};
            Integer[] b = new Integer[a.length];
            for (int i = 0; i < b.length; i++) {
                b[i] = a[i];
            }
            
            List<Integer> list= Arrays.asList(b);
            System.out.println(list);
            System.out.println("最大值为:");
            System.out.println(Collections.max(list)); 
            
            System.out.println("反转");
            Collections.reverse(list);
            System.out.println(list);
            
            System.out.println("打乱");
            Collections.shuffle(list);
            System.out.println(list);
            
            System.out.println("排序");
            Collections.sort(list);
            System.out.println(list);
            
            System.out.println("复制一个集合");
            List<Integer> copyList = new ArrayList<>();
            copyList.addAll(list);
            System.out.println(copyList);
            
        }
    }

  • 相关阅读:
    Oracle创建表空间、创建用户以及授权
    链接正常但IE浏览器无法显示网页的几种情况
    Oracle11g自带的SQL_developer无法打开
    Oracle- 复杂查询及总结
    oracle用户管理
    Oracle简介及安装
    【js】setInterval动态改变定时器时间周期,三次点击事件,时间间隔内只捕捉一次点击事件
    【tip】java_mysql遇到的一些问题
    【tip】java_部署名称与项目名称不一致
    【tip】java_1
  • 原文地址:https://www.cnblogs.com/xinmomoyan/p/11355067.html
Copyright © 2011-2022 走看看