01JAVA入门

Wesley13
• 阅读 666

1 Welcome to java

public class ch01Welcome {
    public static void main(String[] args) {
        System.out.println("Welcome to Java!"); //运行的快捷键为crtl + F11
    }
}

01JAVA入门

01JAVA入门

2 从控制台输入

package cn.jxufe.java;

import java.util.Scanner;

public class ch02ConsoleInput {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        System.out.println("enter a number for radius: ");
        double radius = input.nextDouble();
        double area = radius * radius * 3.14159;
        System.out.println("the area for the circle of radius " + radius + " is " + area);
    }

}

输入三个数,计算三个数的平均值:

package cn.jxufe.java;

import java.util.Scanner;

import jdk.nashorn.internal.objects.NativeUint16Array;

public class ch03ComputerAverage {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        System.out.print(" Enter three numbers : ");
        double number1 = input.nextDouble();
        double number2 = input.nextDouble();
        double number3 = input.nextDouble();

        double average = (number1 + number2 + number3) / 3;

        System.out.println("the average of " + number1 + " " + number2 + " " + number3 + " is " + average);
    }

}

01JAVA入门

3 产生随机数

package cn.jxufe.java;

import java.util.Scanner;

public class ch04RanomNumber {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int number1 = (int) (Math.random() * 10);
        int number2 = (int) (Math.random() * 10);

        if (number1 < number2) {
            int temp = number1;
            number1 = number2;
            number2 = temp;
        }

        System.out.println("what is " + number1 + " - " + number2 + " ?");
        Scanner input = new Scanner(System.in);
        int answer = input.nextInt();

        if (number1 - number2 == answer)
            System.out.println("you are right");
        else {
            System.out.println("you are wrong");
            System.out.println(number1 + " - " + number2 + " should be " + (number1 - number2));
        }
    }

}

01JAVA入门

4 数学函数

4.1 三角函数

01JAVA入门

4.2 指数函数

01JAVA入门

4.3 取整方法

01JAVA入门

4.4 min、max和abs方法

 01JAVA入门 01JAVA入门

4.5 random方法

01JAVA入门

5 String类型

5.1 求字符串长度、从字符串中获取字符、连接字符串和字符串大小写转换

01JAVA入门

01JAVA入门

package cn.jxufe.java;

public class ch05StringType {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String message = "Welcome to Java";
        System.out.println("the length of " + message + " is " + message.length()); // 求字符串长度
        System.out.println(message.charAt(0)); // 从字符串中获取字符
        System.out.println(message.concat(" and python")); // 连接字符串
        System.out.println(message + " and python");
        System.out.println(message.toLowerCase()); // 字符串大小写转换
        System.out.println(message.toUpperCase());
        
    }

}

01JAVA入门

5.2 从控制台读取字符串

package cn.jxufe.java;

import java.util.Scanner;

public class ch05StringType {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Scanner input = new Scanner(System.in);
        System.out.println("enter three words separated by spaces");
        String s1 = input.next();
        String s2 = input.next();
        String s3 = input.next();
        
        System.out.println("s1 is " + s1);
        System.out.println("s2 is " + s2);
        System.out.println("s3 is " + s3);
        
        System.out.println("enter a line: ");
        Scanner input2 = new Scanner(System.in);
        String s= input2.nextLine();
        System.out.println("the line entered is " + s);
    }

}

01JAVA入门

5.3 从控制台读取字符

package cn.jxufe.java;

import java.util.Scanner;

public class ch06ReadChar {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        System.out.println("enter a character: ");
        String s = input.nextLine();
        char ch = s.charAt(0);
        System.out.println("the character entered is " + ch);
    }
}

01JAVA入门

5.4 字符串比较

01JAVA入门

package cn.jxufe.java;

public class ch07StringCompare {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String s1 = "Welcome to java ";
        String s2 = "Welcome to java ";
        String s3 = "Welcome to python ";
        System.out.println(s1.equals(s2)); // 内容相等
        System.out.println(s1.equals(s3));
        System.out.println(s1.compareTo(s2)); // 从左至右第一个不同字符之间的距离
        System.out.println(s1.compareTo(s3));
        System.out.println(s1.startsWith("Wel")); // 字符串检测
        System.out.println(s1.endsWith("va"));
        System.out.println(s1.contains("to"));
    }

}

01JAVA入门

5.5 获取字符串中的字符或者子串

01JAVA入门

package cn.jxufe.java;

import sun.print.resources.serviceui;

public class ch08subString {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String s = "Welcome to java";
        System.out.println(s.indexOf('W'));
        System.out.println(s.indexOf('o'));
        System.out.println(s.indexOf("to"));
        System.out.println(s.indexOf("To"));
        System.out.println();
        System.out.println(s.lastIndexOf('W'));
        System.out.println(s.lastIndexOf('o'));
        System.out.println(s.lastIndexOf("to"));
        System.out.println(s.lastIndexOf("To"));
    }

}

01JAVA入门

6 方法重载

package cn.jxufe.java;

import com.sun.org.apache.regexp.internal.recompile;

public class ch09OverLoading {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("the maximum of 3 and 4 is " + max(3, 4));
        System.out.println("the maximum of 3.0 and 5.4 is " + max(3, 5.4));
        System.out.println("the maximum of 3.0, 5.4 and 10.14 is " + max(3, 5.4, 10.14));

    }

    public static int max(int num1, int num2) {
        return Math.max(num1, num2);
    }

    public static double max(double num1, double num2) {

        return Math.max(num1, num2);
    }

    public static double max(double num1, double num2, double num3) {
        return max(max(num1, num2), num3);
    }

}

01JAVA入门

7 foreach循环

package cn.jxufe.java;

public class ch10ForEach {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        double[] myList = { 1.9, 2.9, 3.4, 3.5 };
        for (double e : myList) {
            System.out.print(e + " ");
        }
    }
}

01JAVA入门

8 应用一维数组分析数字

找出一些数中大于平均值的个数。让用户给出数据的个数和输入的数据。

package cn.jxufe.java;

import java.util.Scanner;

public class ch11AnalyzeNumber {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        System.out.println("enter the number of items: ");
        int n = input.nextInt();
        double[] numbers = new double[n];
        double sum = 0;
        System.out.println("enter the numbers: ");
        for (int i = 0; i < n; i++) {
            numbers[i] = input.nextDouble();
            sum += numbers[i];
        }
        double average = sum / n;

        int count = 0;
        for (double e : numbers) {
            if (e > average)
                count++;
        }
        System.out.println("average is " + average);
        System.out.println("number of elements above the average is " + count);
    }

}

01JAVA入门

9 一维数组的复制

01JAVA入门 01JAVA入门

01JAVA入门

package cn.jxufe.java;

public class ch12Arraycopy {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        double[] myList = { 1.9, 2.9, 3.4, 3.5 };
        double[] newList = new double[myList.length];
        System.arraycopy(myList, 0, newList, 0, myList.length);
        for(double e:newList)
            System.out.print(e + " ");
    }
}

01JAVA入门

10 将一维数组传递给方法

01JAVA入门

01JAVA入门 01JAVA入门

package cn.jxufe.java;

public class ch13ArrayTest {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int x = 1;
        int[] y = new int[10];
        m(x, y);
        System.out.println("x is " + x);
        System.out.println("y[0] is " + y[0]);
    }

    public static void m(int number, int[] numbers) {
        number = 1001;
        numbers[0] = 5555;

    }
}

01JAVA入门

11 可变长参数列表

01JAVA入门

package cn.jxufe.java;

public class ch14VarArgsDemo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        printMax(34, 3, 3, 2, 56.5);
        printMax(new double[] { 1, 2, 3 }); // 匿名数组
    }

    public static void printMax(double... numbers) {
        if (numbers.length == 0) {
            System.out.println("no argument passed ");
            return;
        }
        double result = numbers[0];
        for (double e : numbers) {
            if (e > result)
                result = e;
        }
        System.out.println("the max value is " + result);

    }

}

01JAVA入门

12 Arrays类

01JAVA入门

package cn.jxufe.java;

import java.util.Arrays;

public class ch15ArraysDemo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        double[] numbers = { 6.0, 4.4, 1.9, 2.9, 3.4, 3.5 };
        Arrays.sort(numbers);
        System.out.println(Arrays.toString(numbers)); // 打印一维数组的简单方法

        int[] list = { 2, 4, 7, 10, 11, 45 };
        System.out.println(" 11 index is " + Arrays.binarySearch(list, 11));

        int[] list2 = { 2, 3, 5, 1 };
        Arrays.fill(list2, 5);
        System.out.println(Arrays.toString(list2));

    }

}

01JAVA入门

13 将二维数组传递给方法

package cn.jxufe.java;

import java.util.Arrays;
import java.util.Scanner;

public class ch16TowDimensionArray {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int[][] m = getArray();
        System.out.println(Arrays.deepToString(m)); // 打印二维数组的简单方法
        System.out.println("Sumof all element is " + sum(m));
    }

    public static int[][] getArray() {
        Scanner input = new Scanner(System.in);

        int[][] m = new int[3][4];
        System.out.println("enter " + m.length + " rows and " + m[0].length + " columns: ");
        for (int i = 0; i < m.length; i++) {
            for (int j = 0; j < m[i].length; j++)
                m[i][j] = input.nextInt();
        }
        return m;
    }

    public static int sum(int[][] m) {
        int total = 0;
        for (int row = 0; row < m.length; row++) {
            for (int column = 0; column < m[row].length; column++) {
                total += m[row][column];
            }
        }
        return total;
    }
}

01JAVA入门

点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
待兔 待兔
4个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Jacquelyn38 Jacquelyn38
3年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Stella981 Stella981
3年前
Android So动态加载 优雅实现与原理分析
背景:漫品Android客户端集成适配转换功能(基于目标识别(So库35M)和人脸识别库(5M)),导致apk体积50M左右,为优化客户端体验,决定实现So文件动态加载.!(https://oscimg.oschina.net/oscnet/00d1ff90e4b34869664fef59e3ec3fdd20b.png)点击上方“蓝字”关注我
Wesley13 Wesley13
3年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
3年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Wesley13 Wesley13
3年前
35岁是技术人的天花板吗?
35岁是技术人的天花板吗?我非常不认同“35岁现象”,人类没有那么脆弱,人类的智力不会说是35岁之后就停止发展,更不是说35岁之后就没有机会了。马云35岁还在教书,任正非35岁还在工厂上班。为什么技术人员到35岁就应该退役了呢?所以35岁根本就不是一个问题,我今年已经37岁了,我发现我才刚刚找到自己的节奏,刚刚上路。
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
10个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这