JAVA--Lec2+Lec3

一:内容
1.Scanner库

import java.util.Scanner;

Scanner kb = new Scanner(System.in);

        System.out.println("Please Input");

        String next = kb.nextLine();

        System.out.println(next);

        nextLine();// 读取一行文本,直到遇到换行符。
        nextInt();//读取一个整数。
        nextDouble(); //读取一个双精度浮点数。
        next(): //读取下一个单词(以空格为分隔符)

2.Math库

         double floorRand = Math.floor((Math.random() * 6 ) + 1);
        //Math.Random 随即小数    Math.floor向下取整

3.关于强制转换

// 使用在前面(数据类型)转换
double d = 9.78;
int i = (int) d;


String myString = "5";
int myInt = Integer.parseInt(myString);

double res = Double.parseDouble(s);
String out = Double.toString(res);

boolean x = false;
String m = Boolean.toString(x);

二:练习题
1.计算星期几

        Scanner kb = new Scanner(System.in);
        int year = kb.nextInt();
        int month = kb.nextInt();
        int day = kb.nextInt();


        int  a = year- (14 - month) /12;
        int b = a+ a/4-a/100+a/400;
        int c = month+12*((14-month)/12)-2;
        int whichday = (day+b+(31*c)/12)%7;
        System.out.println("Today is day" + whichday);

2.色彩CMYK--->RGB

        Scanner kb = new Scanner(System.in);
        double C = kb.nextDouble();
        double M = kb.nextDouble();
        double Y = kb.nextDouble();
        double K = kb.nextDouble();


        double white = 1-K;
        double red = 255*white*(1-C);
        double green = 255 * white *(1-M);
        double blue = 255 * white *(1-Y);

        System.out.println(red);
        System.out.println(green);
        System.out.println(blue);

3. 一些奇奇怪怪的换算

Scanner kb = new Scanner(System.in);
        double x1 = kb.nextDouble();
        double y1= kb.nextDouble();
        double x2 = kb.nextDouble();
        double y2 = kb.nextDouble();
        double r = 6371.0f;
        //转换成弧度
        double x1R = Math.toRadians(x1);
        double y1R = Math.toRadians(y1);
        double x2R = Math.toRadians(x2);
        double y2R = Math.toRadians(y2);
        //sin公式
        double in1 = Math.sin((x2R - x1R) / 2) * Math.sin((x2R - x1R) / 2);
        double in2dot1 = Math.sin((y2R - y1R) / 2) * Math.sin((y2R - y1R) / 2);
        double in2 = Math.cos(x1R) * Math.cos(x2R) * in2dot1;
        double in1plus2 = in1 + in2;
        double in = Math.sqrt(in1plus2);
        double arcsinIn = Math.asin(in);
        double dist = 2 * r * arcsinIn;
        System.out.println(dist + " kilometres");

4.一个五边形的面积计算

import java.util.Scanner;
import java.lang.Math;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        double r = sc.nextDouble();

        double s = 2 * r * Math.sin(Math.PI/5);

        double Area = 5 * Math.pow(s,2)/(4*Math.tan(Math.PI/5));

        System.out.println(Area);
}

三:内容----循环判断

四:例题

1.传播疫情

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int init = sc.nextInt();
        int numInfect = sc.nextInt();
        int population = sc.nextInt();
        System.out.println(speardday(init,numInfect,population));
    }

    public static int speardday(int init, int numInfect, int population){
        int day = 1;
        while (init <= population){
            init += init * numInfect;
            day ++;
        }
        return day;
    }

2.判断直角

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        boolean bool = determineTriangle(a,b,c);
        System.out.println(bool);
    }

    private static boolean determineTriangle(int a, int b, int c){
        boolean bool = true;
        if(Math.pow(a,2)+Math.pow(b,2) == Math.pow(c,2)){
            bool = true;
        }else {
            bool = false;
        }
        return  bool;
    }

五: 关于数组的知识

public  class Main{
    public  static  void  main(String[] args){

        int[] myArray;
        String[] myString;
        myArray = new int[10]; //分配10个整数空间
        myString =new String[]{"HI","Nihao"}; // 两字符串

        //直接定义
        String[] myStingArray = {"hhh","iii","ooo"};

        //匿名函数
        System.out.println(new int[]{1, 2, 3, 4, 5}.length); // 打印匿名整型数组的长度
        //给赋值
        myArray[0] = 10;
        System.out.println(myArray[0]);
        //查看长度
        System.out.println(myArray.length);

        for(int i = 0; i<myString.length; i++){
            System.out.println(myString[i]);
        }

        //加强版for循环 --------- foreach
        for(String s : myString){
            System.out.println(s);
        }

        //定义二维数组
        int[][] my2DArray = new int[3][4]; // 3行4列的二维整型数组

    }

}

六:char+Srting

1.可以代表任何字符--使用单引号--用于单字符---是Unicode所以可以跟ASCII互换

char 类型的值是不可变的。一旦创建,其值就不能改变。

char Id = 'A';
char 5 = '5';
char newline = '/n';

2.检查char类型的方法

char firstLetter = 'A';
char five = '5';
char newLine = '\n';
//是否是字母
Character.isLetter(firstLetter);
//是否是数字
Character.isDigit(five);
//是否是空白字符。空白字符包括空格、制表符、换行符等
Character.isWhitespace(newLine);
//是否是大写
Character.isUpperCase(firstLetter);
//是否是小写
Character.isLowerCase(firstLetter);


//将小写大写转换
Character.toUpperCase(firstLetter);

Character.toLowerCase(firstLetter);

3.关于String

拼接String------获取String长度--------String的index取值-----equal()的用法----chartat

String firstName = "Hu";
String lastName = "Ning";

String name = firstName + lastName ;

int len = name.length();

String FN = name.substring(0,2);
String LN =  name.substring(2,6);

String cosmo = "Halloween";
String makima = "Bang";
if (cosmo.equals("Halloween")) {
// correct use .equals() to compare Strings
}
if (cosmo == "Halloween") {
// do not use == with Strings
}

//不区分大小写
什么.equalsIgnoreCase();

String cosmo = "Halloween";
cosmo.isEmpty();
//包含
cosmo.contains("lowe");
//制定前缀后缀
cosmo.startsWith("hal");
cosmo.endsWith("ween");



String cosmo = "Halloween";

// 获取字符串的第一个字符(索引0处的字符)
char firstChar = cosmo.charAt(0); // 'H'

// 获取字符串的第三个字符(索引2处的字符)
char thirdChar = cosmo.charAt(2); // 'l'

// 获取字符串的最后一个字符(索引长度-1处的字符)
char lastChar = cosmo.charAt(cosmo.length() - 1); // 'n'

2.找字符串的某个值的位置

indexOf-----找第一个出现的

lastIndexOf--找最后一次出现的

String str = "Hello, World!";
int indexOfComma = str.indexOf(","); // 返回 5,因为逗号在索引5处
int indexOfSpace = str.indexOf(" "); // 返回 6,因为空格在索引6处
int indexOfExclamation = str.indexOf("!"); // 返回 12,因为感叹号在索引12处
int indexOfNotFound = str.indexOf("Java"); // 返回 -1,因为"Java"未在字符串中找到


String str = "Hello, World!";
int indexOfH = str.indexOf('H'); // 返回 0,因为'H'在索引0处
int indexOfW = str.indexOf('W'); // 返回 7,因为'W'在索引7处
int indexOfNotFound = str.indexOf('X'); // 返回 -1,因为'X'未在字符串中找到

 在指定位置找是否是对的

String str = "Hello, World!";
int indexOfComma = str.indexOf(",", 7); // 返回 -1,因为在索引7及之后的位置未找到逗号

1. String trim()

  • 用途
    • trim() 方法用于移除字符串两端的空白字符(包括空格、制表符、换行符等)。
  • 示例
    • 如果有一个字符串 String str = " Hello World ";,调用 str.trim() 会返回 "Hello World"

2. String replace(CharSequence target, CharSequence replacement)

  • 用途
    • replace() 方法用于将字符串中的某个子字符串(或字符序列)替换为另一个子字符串(或字符序列)。
  • 示例
    • 如果有一个字符串 String str = "Hello World";,调用 str.replace("World", "Java") 会返回 "Hello Java"

3. String replaceAll(String regex, String replacement)

  • 用途
    • replaceAll() 方法用于根据正则表达式替换字符串中的子串。
    • 它使用正则表达式作为匹配规则,将所有符合规则的部分替换为指定的字符串。
  • 示例
    • 如果有一个字符串 String str = "Hello World";,调用 str.replaceAll("o", "O") 会返回 "HellO WOrld"

4. String[] split(String delimiter)

  • 用途
    • split() 方法用于根据指定的分隔符将字符串拆分为子串数组。
  • 示例
    • 如果有一个字符串 String str = "one,two,three";,调用 str.split(",") 会返回一个数组 {"one", "two", "three"}
      String cosmo = " ha haa halloween ";
      cosmo = cosmo.trim(); // 移除两端空白,结果为"ha haa halloween"
      cosmo = cosmo.replaceAll("h", "H"); // 替换所有的'h'为'H',结果为"Ha Haa Halloween"
      String[] words = cosmo.split(" "); // 根据空格分割字符串
      
      for (int i = 0; i < words.length; i++) {
          System.out.println(words[i]); 
      }
      
      

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值