• 牛客网语法篇之Java入门


    JAVA1 类型转换

    1. import java.util.Scanner;
    2. public class Main {
    3. public static void main(String[] args) {
    4. Scanner scanner = new Scanner(System.in);
    5. double d = scanner.nextDouble();
    6. System.out.println(Main.typeConversion(d));
    7. }
    8. public static int typeConversion(double d){
    9. //write your code here......
    10. return (int)d;
    11. }
    12. }

    JAVA2 简单运算

    1. import java.util.Scanner;
    2. public class Main {
    3. public static void main(String[] args) {
    4. Scanner scanner = new Scanner(System.in);
    5. int a = scanner.nextInt();
    6. int b = scanner.nextInt();
    7. scanner.close();
    8. //write your code here......
    9. int sum = a + b;
    10. int difference = a > b ? a - b : b - a;
    11. int product = a * b;
    12. int quotient = a > b ? a / b : b / a;
    13. int module = a > b ? a % b : b % a;
    14. System.out.println(sum + " " + difference + " " + product + " " + quotient + " " + module);
    15. }
    16. }

    JAVA3 四舍五入

    1. import java.util.Scanner;
    2. public class Main {
    3. public static void main(String[] args) {
    4. Scanner scanner = new Scanner(System.in);
    5. double d= scanner.nextDouble();
    6. //write your code here......
    7. int i = (int)(d * 10) % 10 >= 5 ? (int)d + 1 : (int)d;
    8. System.out.println(i);
    9. }
    10. }

    JAVA4 交换变量值

    1. import java.util.Scanner;
    2. public class Main {
    3. public static void main(String[] args) {
    4. Scanner scanner = new Scanner(System.in);
    5. int a = scanner.nextInt();
    6. int b = scanner.nextInt();
    7. //write your code here.......
    8. int temp = a;
    9. a = b;
    10. b = temp;
    11. System.out.println(a+" "+b);
    12. }
    13. }

    JAVA5 计算商场折扣

    1. import java.util.*;
    2. public class Main {
    3. public static void main(String[] args) {
    4. Scanner console = new Scanner(System.in);
    5. int price = console.nextInt();
    6. int cost = 0;
    7. //write your code here......
    8. if (price < 100) {
    9. cost = price;
    10. } else if (price < 500) {
    11. cost = (int)(price * 0.9);
    12. } else if (price < 2000) {
    13. cost = (int)(price * 0.8);
    14. } else if (price < 5000) {
    15. cost = (int)(price * 0.7);
    16. } else {
    17. cost = (int)(price * 0.6);
    18. }
    19. System.out.println(cost);
    20. }
    21. }

    JAVA6 判断体重指数

    1. import java.util.Scanner;
    2. public class Main {
    3. public static void main(String[] args) {
    4. Scanner scanner = new Scanner(System.in);
    5. double height = scanner.nextDouble();
    6. double weight = scanner.nextDouble();
    7. //write your code here......
    8. double bmi = weight / (height * height);
    9. if (bmi < 18.5) {
    10. System.out.println("偏瘦");
    11. } else if (bmi < 20.9) {
    12. System.out.println("苗条");
    13. } else if (bmi < 24.9) {
    14. System.out.println("适中");
    15. } else {
    16. System.out.println("偏胖");
    17. }
    18. }
    19. }

    JAVA7 判断学生成绩等级

    1. import java.util.Scanner;
    2. public class Main {
    3. public static void main(String[] args) {
    4. Scanner scanner = new Scanner(System.in);
    5. String grade =scanner.next();
    6. //write your code here......
    7. switch (grade) {
    8. case "A":
    9. System.out.println("优秀");
    10. break;
    11. case "B":
    12. System.out.println("良好");
    13. break;
    14. case "C":
    15. System.out.println("及格");
    16. break;
    17. case "D":
    18. System.out.println("不及格");
    19. break;
    20. default:
    21. System.out.println("未知等级");
    22. }
    23. }
    24. }

    JAVA8 邮箱验证

    1. import java.util.Scanner;
    2. public class Main {
    3. public static void main(String[] args) {
    4. Scanner scanner = new Scanner(System.in);
    5. String str = scanner.next();
    6. String emailMatcher="[a-zA-Z0-9]+@[a-zA-Z0-9]+\\.[a-zA-Z0-9]+";
    7. //write your code here......
    8. if (str.matches(emailMatcher)) {
    9. System.out.println("邮箱格式合法");
    10. } else {
    11. System.out.println("邮箱格式不合法");
    12. }
    13. }
    14. }

    JAVA9 数列求和

    1. public class Main {
    2. public static void main(String[] args) {
    3. //write your code here........
    4. long sum = 0;
    5. for (long i = 9; i <= 9999999999L; i = i * 10 + 9) {
    6. sum += i;
    7. }
    8. System.out.println(sum);
    9. }
    10. }

    JAVA10 统计输入正数个数

    1. import java.util.*;
    2. public class Main {
    3. public static void main(String[] args) {
    4. int count = 0;
    5. Scanner scanner = new Scanner(System.in);
    6. //write your code here......
    7. while (scanner.nextInt() > 0) {
    8. count++;
    9. }
    10. System.out.println(count);
    11. }
    12. }

    JAVA11 求最小公倍数

    1. import java.util.*;
    2. public class Main {
    3. public static void main(String[] args) {
    4. Scanner console = new Scanner(System.in);
    5. int m = console.nextInt();
    6. int n = console.nextInt();
    7. int result = getCM(m, n);
    8. System.out.println(result);
    9. }
    10. public static int getCM(int m, int n){
    11. //write your code here......
    12. int a = m > n ? m : n;
    13. int b = m > n ? n : m;
    14. while (b > 0) {
    15. int temp = a % b;
    16. a = b;
    17. b = temp;
    18. }
    19. return m * n / a;
    20. }
    21. }

    JAVA12 小球走过路程计算

    1. import java.util.Scanner;
    2. public class Main {
    3. public static void main(String[] args) {
    4. Scanner scanner=new Scanner(System.in);
    5. float h=scanner.nextFloat();
    6. int n =scanner.nextInt();
    7. //write your code here......
    8. float sum = h;
    9. h /= 2;
    10. for (int i = 1; i < n; ++i) {
    11. sum += h * 2;
    12. h /= 2;
    13. }
    14. System.out.println(String.format("%.3f", h)+" "+String.format("%.3f", sum));
    15. //输出格式为:System.out.println(String.format("%.3f", h)+" "+String.format("%.3f", sum));
    16. }
    17. }

    JAVA13 求平均数

    1. import java.util.Scanner;
    2. public class Main {
    3. public static void main(String[] args) {
    4. Scanner scan = new Scanner(System.in);
    5. //write your code here......
    6. int number;
    7. double avg = 0;
    8. int count = 0;
    9. while ((number = scan.nextInt()) != -1) {
    10. avg += number;
    11. count++;
    12. }
    13. avg /= count;
    14. //输出格式为:System.out.println(String.format("%.2f",avg));
    15. System.out.println(String.format("%.2f",avg));
    16. }
    17. }

    JAVA14 判断质数

    1. import java.util.Scanner;
    2. public class Main {
    3. public static void main(String[] args) {
    4. Main main = new Main();
    5. Scanner scan = new Scanner(System.in);
    6. int number = scan.nextInt();
    7. System.out.println(main.isPrimeNumber(number));
    8. }
    9. public Boolean isPrimeNumber(int number) {
    10. //write your code here......
    11. for (int i = 2; i * i <= number; i++) {
    12. if (number % i == 0) {
    13. return false;
    14. }
    15. }
    16. return true;
    17. }
    18. }

    JAVA15 计算整数位数

    1. import java.util.Scanner;
    2. public class Main {
    3. public static void main(String[] args) {
    4. Scanner scan = new Scanner(System.in);
    5. int num = scan.nextInt();
    6. scan.close();
    7. //write code here......
    8. if (num > 0) {
    9. int numOfDigits = 0;
    10. while (num > 0) {
    11. numOfDigits++;
    12. num /= 10;
    13. }
    14. System.out.println(numOfDigits);
    15. } else {
    16. System.out.println(num);
    17. }
    18. }
    19. }

    JAVA16 数组遍历

    1. import java.util.Scanner;
    2. public class Main {
    3. public static void main(String[] args) {
    4. int[] ary = new int[6];
    5. int max;
    6. int min;
    7. Scanner scanner = new Scanner(System.in);
    8. for (int i = 0; i
    9. ary[i]=scanner.nextInt();
    10. }
    11. //write your code here......
    12. max = ary[0];
    13. min = ary[0];
    14. for (int i = 1; i < ary.length; i++) {
    15. max = max < ary[i] ? ary[i] : max;
    16. min = min > ary[i] ? ary[i] : min;
    17. }
    18. System.out.println(max+" "+min);
    19. }
    20. }

    JAVA17 数组倒转

    1. import java.util.Arrays;
    2. import java.util.Scanner;
    3. public class Main {
    4. public static void main(String[] args) {
    5. int[] arr = new int[6];
    6. Scanner scanner = new Scanner(System.in);
    7. for (int i = 0; i < arr.length; i++) {
    8. arr[i] = scanner.nextInt();
    9. }
    10. System.out.println(Arrays.toString(arr));
    11. //write your code here......
    12. for (int i = 0, j = arr.length - 1; i < j; i++, j--) {
    13. int temp = arr[i];
    14. arr[i] = arr[j];
    15. arr[j] = temp;
    16. }
    17. System.out.println(Arrays.toString(arr));
    18. }
    19. }

    JAVA18 二维数组求和

    1. public class Main {
    2. public static void main(String[] args) {
    3. int[][] arr = {{11,33,55},{22,44,66,88},{131,214,315,146},{928,827,726,625},{424,525}};
    4. int sum=add(arr);
    5. System.out.println(sum);
    6. }
    7. public static int add(int[][] arr) {
    8. int sum=0;
    9. //write your code here......
    10. for (int i = 0; i < arr.length; i++) {
    11. for (int j = 0; j < arr[i].length; j++) {
    12. sum += arr[i][j];
    13. }
    14. }
    15. return sum;
    16. }
    17. }

    JAVA19 修改Data类的定义

    1. import java.util.Scanner;
    2. public class Main {
    3. public static void main(String[] args) {
    4. Scanner scanner = new Scanner(System.in);
    5. while (scanner.hasNextInt()) {
    6. int x = scanner.nextInt();
    7. int y = scanner.nextInt();
    8. Data data = new Data(x, y);
    9. System.out.println(data.getX() + data.getY());
    10. }
    11. }
    12. }
    13. class Data {
    14. private int x;
    15. private int y;
    16. Data(int x, int y) {
    17. this.x = x;
    18. this.y = y;
    19. }
    20. public int getX() {
    21. return x;
    22. }
    23. public int getY() {
    24. return y;
    25. }
    26. }

    JAVA20 验证年龄

    1. import java.util.Scanner;
    2. public class Main {
    3. public static void main(String[] args) {
    4. Person p = new Person();
    5. Scanner scanner = new Scanner(System.in);
    6. while (scanner.hasNextInt()) {
    7. int age = scanner.nextInt();
    8. p.setAge(age);
    9. System.out.println(p.getAge());
    10. }
    11. }
    12. }
    13. class Person {
    14. private int age;
    15. //write your code here......
    16. public void setAge(int age) {
    17. this.age = age;
    18. }
    19. public int getAge() {
    20. if (age < 0) {
    21. return 0;
    22. } else if (age > 200) {
    23. return 200;
    24. } else {
    25. return age;
    26. }
    27. }
    28. }

    JAVA21 补全构造方法

    1. import java.util.Scanner;
    2. public class Main {
    3. public static void main(String[] args) {
    4. Scanner scanner = new Scanner(System.in);
    5. while (scanner.hasNextInt()) {
    6. int x = scanner.nextInt();
    7. int y = scanner.nextInt();
    8. int z = scanner.nextInt();
    9. Sub sub = new Sub(x, y, z);
    10. System.out.println(sub.calculate());
    11. }
    12. }
    13. }
    14. class Base {
    15. private int x;
    16. private int y;
    17. public Base(int x, int y) {
    18. this.x = x;
    19. this.y = y;
    20. }
    21. public int getX() {
    22. return x;
    23. }
    24. public int getY() {
    25. return y;
    26. }
    27. }
    28. class Sub extends Base {
    29. private int z;
    30. public Sub(int x, int y, int z) {
    31. //write your code here
    32. super(x, y);
    33. this.z = z;
    34. }
    35. public int getZ() {
    36. return z;
    37. }
    38. public int calculate() {
    39. return super.getX() * super.getY() * this.getZ();
    40. }
    41. }

    JAVA22 重写计算逻辑

    1. import java.util.Scanner;
    2. public class Main {
    3. public static void main(String[] args) {
    4. Scanner scanner = new Scanner(System.in);
    5. while (scanner.hasNextInt()) {
    6. int x = scanner.nextInt();
    7. int y = scanner.nextInt();
    8. Sub sub = new Sub(x, y);
    9. sub.calculate();
    10. }
    11. }
    12. }
    13. class Base {
    14. private int x;
    15. private int y;
    16. public Base(int x, int y) {
    17. this.x = x;
    18. this.y = y;
    19. }
    20. public int getX() {
    21. return x;
    22. }
    23. public int getY() {
    24. return y;
    25. }
    26. public void calculate() {
    27. System.out.println(getX() * getY());
    28. }
    29. }
    30. class Sub extends Base {
    31. //write your code here......
    32. public Sub(int x, int y) {
    33. super(x, y);
    34. }
    35. public void calculate() {
    36. System.out.println(getY() == 0 ? "Error" : getX() / getY());
    37. }
    38. }

    JAVA23 定义打印方法

    1. import java.util.Scanner;
    2. public class Main {
    3. public static void main(String[] args) throws Exception {
    4. Scanner scanner = new Scanner(System.in);
    5. while (scanner.hasNext()) {
    6. String className = scanner.next();
    7. // print就是需要你定义的方法
    8. print(Class.forName(className).newInstance());
    9. }
    10. }
    11. //write your code here......
    12. private static void print(Object clazz) {
    13. System.out.println(clazz.toString());
    14. }
    15. }
    16. class First {
    17. public String toString() {
    18. return "First";
    19. }
    20. }
    21. class Second {
    22. public String toString() {
    23. return "Second";
    24. }
    25. }
    26. class Third {
    27. public String toString() {
    28. return "Third";
    29. }
    30. }

    JAVA24 类型判断

    1. import java.util.Scanner;
    2. public class Main {
    3. public static void main(String[] args) throws Exception {
    4. Scanner scanner = new Scanner(System.in);
    5. while (scanner.hasNext()) {
    6. String className = scanner.next();
    7. Base obj = (Base) Class.forName(className).newInstance();
    8. System.out.println(getClassName(obj));
    9. }
    10. }
    11. public static String getClassName(Base obj) {
    12. //write your code here......
    13. return obj.getClass().getName();
    14. }
    15. }
    16. class Base {
    17. }
    18. class Sub1 extends Base {
    19. }
    20. class Sub2 extends Base {
    21. }

    JAVA25 实现抽象方法

    1. import java.util.Scanner;
    2. public class Main {
    3. public static void main(String[] args) {
    4. // Sub是需要你定义的子类
    5. Base base = new Sub();
    6. Scanner scanner = new Scanner(System.in);
    7. while (scanner.hasNextInt()) {
    8. int x = scanner.nextInt();
    9. int y = scanner.nextInt();
    10. base.setX(x);
    11. base.setY(y);
    12. System.out.println(base.calculate());
    13. }
    14. }
    15. }
    16. abstract class Base {
    17. private int x;
    18. private int y;
    19. public int getX() {
    20. return x;
    21. }
    22. public void setX(int x) {
    23. this.x = x;
    24. }
    25. public int getY() {
    26. return y;
    27. }
    28. public void setY(int y) {
    29. this.y = y;
    30. }
    31. public int calculate() {
    32. if (avg() == 0) {
    33. return 0;
    34. } else {
    35. return sum() / avg();
    36. }
    37. }
    38. /**
    39. * 返回x和y的和
    40. */
    41. public abstract int sum();
    42. /**
    43. * 返回x和y的平均值
    44. */
    45. public abstract int avg();
    46. }
    47. class Sub extends Base {
    48. //write your code here......
    49. public int sum() {
    50. return getX() + getY();
    51. }
    52. public int avg() {
    53. return (getX() + getY()) / 2;
    54. }
    55. }

    JAVA26 实现接口

    1. import java.util.Scanner;
    2. public class Main {
    3. public static void main(String[] args) {
    4. Comparator comparator = new ComparatorImpl();
    5. Scanner scanner = new Scanner(System.in);
    6. while (scanner.hasNextInt()) {
    7. int x = scanner.nextInt();
    8. int y = scanner.nextInt();
    9. System.out.println(comparator.max(x, y));
    10. }
    11. }
    12. }
    13. interface Comparator {
    14. /**
    15. * 返回两个整数中的最大值
    16. */
    17. int max(int x, int y);
    18. }
    19. //write your code here......
    20. class ComparatorImpl implements Comparator {
    21. public int max(int x, int y) {
    22. return x > y ? x : y;
    23. }
    24. }

    JAVA27 重写父类方法

    1. import java.util.Scanner;
    2. public class Main {
    3. public static void main(String[] args) {
    4. Scanner scanner = new Scanner(System.in);
    5. while (scanner.hasNextInt()) {
    6. int x = scanner.nextInt();
    7. int y = scanner.nextInt();
    8. Sub sub = new Sub(x, y);
    9. System.out.println(sub.sum());
    10. }
    11. }
    12. }
    13. class Base {
    14. private int x;
    15. private int y;
    16. public Base(int x, int y) {
    17. this.x = x;
    18. this.y = y;
    19. }
    20. public int getX() {
    21. return x;
    22. }
    23. public final int getY() {
    24. return y;
    25. }
    26. public final int sum() {
    27. return getX() + getY();
    28. }
    29. }
    30. class Sub extends Base {
    31. public Sub(int x, int y) {
    32. super(x, y);
    33. }
    34. //write your code here......
    35. public int getX() {
    36. return 10 * super.getX();
    37. }
    38. }

    JAVA28 创建单例对象

    1. public class Main {
    2. public static void main(String[] args) {
    3. Singleton s1 = Singleton.getInstance();
    4. Singleton s2 = Singleton.getInstance();
    5. System.out.println(s1 == s2);
    6. }
    7. }
    8. class Singleton {
    9. private static Singleton instance;
    10. private Singleton() {
    11. }
    12. //write your code here......
    13. public static Singleton getInstance() {
    14. return instance;
    15. }
    16. }

    JAVA29 动态字符串

    1. import java.util.Scanner;
    2. public class Main {
    3. public static void main(String[] args) {
    4. Scanner scanner = new Scanner(System.in);
    5. String str = scanner.next();
    6. //write your code here......
    7. StringBuilder modifiedStr = new StringBuilder();
    8. int ptr = str.length();
    9. modifiedStr.append(str, Math.max(ptr - 3, 0), ptr);
    10. ptr -= 3;
    11. while (ptr > 0) {
    12. modifiedStr.insert(0, str.substring(Math.max(ptr - 3, 0), ptr) + ",");
    13. ptr -= 3;
    14. }
    15. System.out.println(modifiedStr.toString());
    16. }
    17. }

    JAVA30 统计字符串中字母出现次数

    1. import java.util.Scanner;
    2. public class Main {
    3. public static void main(String[] args) {
    4. String string = "H e l l o ! n o w c o d e r";
    5. Scanner scanner= new Scanner(System.in);
    6. String word = scanner.next();
    7. scanner.close();
    8. System.out.println(check(string, word));
    9. }
    10. public static int check(String str, String word) {
    11. //write your code here......
    12. char query = word.charAt(0);
    13. int count = 0;
    14. for (int i = 0; i < str.length(); i++) {
    15. if (str.charAt(i) == query) {
    16. count++;
    17. }
    18. }
    19. return count;
    20. }
    21. }

    JAVA31 十进制数转二进制数

    1. import java.util.*;
    2. public class Main {
    3. public static void main(String[] args) {
    4. Scanner scanner = new Scanner(System.in);
    5. int num = scanner.nextInt();
    6. //write your code here......
    7. System.out.println(Integer.toBinaryString(num));
    8. }
    9. }

    JAVA33 掷骰子游戏

    1. import java.util.Random;
    2. import java.util.Scanner;
    3. public class Main {
    4. public static void main(String[] args) {
    5. Scanner scanner = new Scanner(System.in);
    6. while (scanner.hasNextInt()) {
    7. int seed = scanner.nextInt();
    8. Random random = new Random(seed);
    9. //write your code here......
    10. System.out.println(random.nextInt(6) + 1);
    11. }
    12. }
    13. }

    JAVA34 求绝对值,平方根,对数,正弦值

    1. import java.util.Scanner;
    2. public class Main {
    3. public static void main(String[] args) {
    4. Scanner scanner=new Scanner(System.in);
    5. double num = scanner.nextDouble();
    6. //write your code here......
    7. System.out.println(Math.abs(num));
    8. System.out.println(Math.sqrt(num));
    9. System.out.println(Math.log(num));
    10. System.out.println(Math.sin(num));
    11. }
    12. }

    JAVA35 输出某一年的各个月份的天数

    1. import java.util.Calendar;
    2. import java.util.Scanner;
    3. public class Main {
    4. public static void main(String[] args) {
    5. Scanner console = new Scanner(System.in);
    6. int year = console.nextInt();
    7. //write your code here......
    8. Calendar c = Calendar.getInstance();
    9. for (int month = 1; month <= 12; month++) {
    10. c.set(year, month - 1, 1);
    11. System.out.println(year + "年" + month + "月:" + c.getActualMaximum(Calendar.DAY_OF_MONTH) + "天");
    12. }
    13. }
    14. }

    JAVA36 日期换算

    1. import java.text.ParseException;
    2. import java.text.SimpleDateFormat;
    3. import java.util.Date;
    4. import java.util.Scanner;
    5. public class Main {
    6. public static void main(String[] args) throws ParseException {
    7. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    8. Scanner in = new Scanner(System.in);
    9. String str1 = in.nextLine();
    10. //write your code here......
    11. try {
    12. Date date = new SimpleDateFormat("yyyy MM dd HH mm ss").parse(str1);
    13. System.out.println("北京时间为:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date));
    14. System.out.println("纽约时间为:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date.getTime() - 12 * 60 * 60 * 1000));
    15. } catch (ParseException ex) {
    16. System.out.println("您输入的数据不合理");
    17. }
    18. }
    19. }

    JAVA37 判断学生成绩

    1. import java.util.*;
    2. public class Main {
    3. public static void main(String[] args) {
    4. Scanner scanner = new Scanner(System.in);
    5. int score = scanner.nextInt();
    6. //write your code here......
    7. try {
    8. if (0 <= score && score <= 100) {
    9. System.out.println(score);
    10. } else {
    11. throw new ScoreException("分数不合法");
    12. }
    13. } catch (ScoreException ex) {
    14. System.out.println(ex.getMessage());
    15. }
    16. }
    17. }
    18. class ScoreException extends Exception {
    19. //write your code here......
    20. public ScoreException() {
    21. super();
    22. }
    23. public ScoreException(String message) {
    24. super(message);
    25. }
    26. }

    JAVA38 字符串去重

    1. import java.util.HashSet;
    2. import java.util.Scanner;
    3. public class Main {
    4. public static void main(String[] args) {
    5. Scanner scanner = new Scanner(System.in);
    6. String str = scanner.nextLine();
    7. scanner.close();
    8. HashSet hs = new HashSet<>();
    9. //write your code here......
    10. for (int i = 0; i < str.length(); i++) {
    11. hs.add(str.charAt(i));
    12. }
    13. for (char c:hs) {
    14. System.out.print(c);
    15. }
    16. }
    17. }

    JAVA39 集合遍历

    1. import java.util.ArrayList;
    2. import java.util.Iterator;
    3. import java.util.List;
    4. import java.util.Scanner;
    5. public class Main {
    6. public static void main(String[] args) {
    7. Scanner scanner = new Scanner(System.in);
    8. List list = new ArrayList<>();
    9. int num1 = scanner.nextInt();
    10. int num2 = scanner.nextInt();
    11. int num3 = scanner.nextInt();
    12. int num4 = scanner.nextInt();
    13. int num5 = scanner.nextInt();
    14. scanner.close();
    15. list.add(num1);
    16. list.add(num2);
    17. list.add(num3);
    18. list.add(num4);
    19. list.add(num5);
    20. System.out.print("普通for循环:");
    21. //write your code here......
    22. for (int i = 0; i < list.size(); i++) {
    23. System.out.print(list.get(i) + " ");
    24. }
    25. System.out.println();
    26. System.out.print("增强for循环:");
    27. //write your code here......
    28. for (Integer x : list) {
    29. System.out.print(x + " ");
    30. }
    31. System.out.println();
    32. System.out.print("迭代器遍历:");
    33. //write your code here......
    34. Iterator iter = list.listIterator();
    35. while (iter.hasNext()) {
    36. System.out.print(iter.next() + " ");
    37. }
    38. System.out.println();
    39. }
    40. }

    JAVA40 排队系统

    1. import java.util.*;
    2. public class Main {
    3. public static void main(String[] args) {
    4. Guest guest1 = new Guest("小明",false);
    5. Guest guest2 = new Guest("小军",false);
    6. Guest vipGuest = new Guest("小红",true);
    7. Deque deque = new ArrayDeque<>();
    8. deque.add(guest1);
    9. deque.add(guest2);
    10. //write your code here......
    11. deque.push(vipGuest);
    12. System.out.println(deque);
    13. }
    14. }
    15. class Guest{
    16. String name;
    17. Boolean vip;
    18. @Override
    19. public String toString() {
    20. return name;
    21. }
    22. public Guest(String name, Boolean vip) {
    23. this.name = name;
    24. this.vip = vip;
    25. }
    26. }

    JAVA41 Head and Tail of the Queue

    1. import java.util.ArrayDeque;
    2. import java.util.Scanner;
    3. public class Main {
    4. public static void main(String[] args) {
    5. ArrayDeque deque = new ArrayDeque();
    6. Scanner scanner = new Scanner(System.in);
    7. while (scanner.hasNext()) {
    8. String name = scanner.next();
    9. // 初始化队列中的数据
    10. deque.offerLast(name);
    11. }
    12. // write your code here......
    13. boolean fromFirst = true;
    14. while (!deque.isEmpty()) {
    15. if (fromFirst) {
    16. System.out.println(deque.pollFirst());
    17. } else {
    18. System.out.println(deque.pollLast());
    19. }
    20. fromFirst = !fromFirst;
    21. }
    22. }
    23. }

    JAVA42 统计一句话中重复单词的个数

    1. import java.util.*;
    2. public class Main {
    3. public static void main(String[] args) {
    4. Scanner scanner = new Scanner(System.in);
    5. String line = scanner.nextLine();
    6. Map map = new LinkedHashMap();
    7. //write your code here......
    8. for (int i = 0; i < line.length(); i++) {
    9. char ch = line.charAt(i);
    10. if (('A' <= ch && ch <= 'Z') || ('a' <= ch && ch <= 'z')) {
    11. map.put(ch, map.getOrDefault(ch, 0) + 1);
    12. }
    13. }
    14. Set> entrys = map.entrySet();
    15. for (Map.Entry entry : entrys) {
    16. System.out.println(entry.getKey() + ":" + entry.getValue());
    17. }
    18. }
    19. }

    JAVA43 map简单应用

    1. import java.util.*;
    2. public class Main {
    3. public static void main(String[] args) {
    4. Scanner scanner = new Scanner(System.in);
    5. String name = scanner.next();
    6. Map map = new HashMap();
    7. map.put(1, "Amy");
    8. map.put(2, "Joe");
    9. map.put(3, "Tom");
    10. map.put(4, "Susan");
    11. //write your code here......
    12. for (Integer key : map.keySet()) {
    13. System.out.println(key + ":" + map.get(key));
    14. }
    15. map.put(5, name);
    16. map.remove(4);
    17. map.replace(3, "Tommy");
    18. System.out.println();
    19. for (Integer key : map.keySet()) {
    20. System.out.println(key + ":" + map.get(key));
    21. }
    22. }
    23. }

    JAVA44 集合排序

    1. import java.util.ArrayList;
    2. import java.util.Collections;
    3. import java.util.List;
    4. import java.util.Scanner;
    5. public class Main {
    6. public static void main(String[] args) {
    7. Scanner scanner = new Scanner(System.in);
    8. Customer customer1 = new Customer("小明",scanner.nextInt());
    9. Customer customer2 = new Customer("小军",scanner.nextInt());
    10. Customer customer3 = new Customer("小红",scanner.nextInt());
    11. List customers = new ArrayList<>();
    12. customers.add(customer1);
    13. customers.add(customer2);
    14. customers.add(customer3);
    15. //write your code here......
    16. Collections.sort(customers);
    17. System.out.println(customers);
    18. }
    19. }
    20. class Customer implements Comparable{
    21. private String name;
    22. private int consumption;
    23. public Customer(String name, int consumption) {
    24. this.name = name;
    25. this.consumption = consumption;
    26. }
    27. @Override
    28. public String toString() {
    29. return "Customer{" +
    30. "name='" + name + '\'' +
    31. ", consumption=" + consumption +
    32. '}';
    33. }
    34. //write your code here......
    35. public int getConsumption() {
    36. return consumption;
    37. }
    38. public int compareTo(Customer customer) {
    39. return customer.getConsumption() - this.consumption;
    40. }
    41. }

    JAVA45 判断各类型字符个数

    1. import java.util.Scanner;
    2. public class Main {
    3. public static void main(String[] args) {
    4. int numbers = 0;
    5. int words = 0;
    6. int space = 0;
    7. int other = 0;
    8. Scanner scanner = new Scanner(System.in);
    9. String str = scanner.nextLine();
    10. //write your code here......
    11. for (int i = 0; i < str.length(); i++) {
    12. char ch = str.charAt(i);
    13. if (('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z')) {
    14. words++;
    15. } else if ('0' <= ch && ch <= '9') {
    16. numbers++;
    17. } else if (ch == ' ') {
    18. space++;
    19. } else {
    20. other++;
    21. }
    22. }
    23. System.out.println("英文字母"+words+"数字"+numbers+"空格"+space+"其他"+other);
    24. }
    25. }

    JAVA46 编写个人所得税计算程序

    1. import java.util.*;
    2. public class Main {
    3. public static void main(String[] args) {
    4. List employees = new ArrayList<>();
    5. //write your code here......
    6. employees.add(new Employee("小明", 2500));
    7. employees.add(new Employee("小军", 8000));
    8. employees.add(new Employee("小红", 100000));
    9. for (Employee employee : employees) {
    10. double salaryForTax = employee.getSalary() - 3500;
    11. double tax = 0;
    12. if (salaryForTax <= 0) {
    13. tax = 0;
    14. } else if (salaryForTax <= 1500) {
    15. tax = salaryForTax * 0.03;
    16. } else if (salaryForTax <= 4500) {
    17. tax = salaryForTax * 0.10 - 105;
    18. } else if (salaryForTax <= 9000) {
    19. tax = salaryForTax * 0.20 - 555;
    20. } else if (salaryForTax <= 35000) {
    21. tax = salaryForTax * 0.25 - 1005;
    22. } else if (salaryForTax <= 55000) {
    23. tax = salaryForTax * 0.30 - 2755;
    24. } else if (salaryForTax <= 80000) {
    25. tax = salaryForTax * 0.35 - 5505;
    26. } else {
    27. tax = salaryForTax * 0.45 - 13505;
    28. }
    29. System.out.println(employee.getName() + "应该缴纳的个人所得税是:" + String.format("%.1f", tax));
    30. }
    31. }
    32. }
    33. class Employee{
    34. private String name;
    35. private double salary;
    36. public Employee(String name, double salary) {
    37. this.name = name;
    38. this.salary = salary;
    39. }
    40. public String getName() {
    41. return name;
    42. }
    43. public double getSalary() {
    44. return salary;
    45. }
    46. }

    JAVA47 记录点赞用户

    1. import java.util.*;
    2. public class Main {
    3. public static void main(String[] args) {
    4. LikeRecorder recorder = new LikeRecorderImpl();
    5. Scanner scanner = new Scanner(System.in);
    6. while (scanner.hasNext()) {
    7. String name = scanner.next();
    8. recorder.like(name);
    9. }
    10. System.out.println(Arrays.toString(recorder.getLikeUsers()));
    11. }
    12. }
    13. /**
    14. * 点赞记录器
    15. */
    16. interface LikeRecorder {
    17. /**
    18. * 若用户没有点赞过,则记录此次点赞行为。
    19. * 若用户曾经点赞过,则删除用户点赞记录。
    20. *
    21. * @param username 用户名
    22. */
    23. void like(String username);
    24. /**
    25. * 返回所有点赞的用户名
    26. *
    27. * @return 用户名数组
    28. */
    29. String[] getLikeUsers();
    30. }
    31. class LikeRecorderImpl implements LikeRecorder {
    32. // write your code here......
    33. private HashSet users = new HashSet<>();
    34. public void like(String username) {
    35. if (users.contains(username)) {
    36. users.remove(username);
    37. } else {
    38. users.add(username);
    39. }
    40. }
    41. public String[] getLikeUsers() {
    42. return users.toArray(new String[users.size()]);
    43. }
    44. }

    JAVA48 回文数判断

    1. import java.util.Scanner;
    2. public class Main {
    3. public static void main(String[] args) {
    4. Scanner console = new Scanner(System.in);
    5. Main main = new Main();
    6. int number = console.nextInt();
    7. System.out.println(main.palindromeNumber(number));
    8. }
    9. public Boolean palindromeNumber(int number) {
    10. //write your code here......
    11. StringBuffer numStr = new StringBuffer("" + number);
    12. return numStr.toString().equals(numStr.reverse().toString());
    13. }
    14. }

    JAVA49 判断素数个数

    1. import java.util.Scanner;
    2. public class Main {
    3. public static void main(String[] args) {
    4. Scanner scanner = new Scanner(System.in);
    5. int start = scanner.nextInt();
    6. int end = scanner.nextInt();
    7. method(start,end);
    8. }
    9. public static void method(int start, int end) {
    10. int count=0;
    11. //write your code here......
    12. if (start > end) {
    13. int temp = start;
    14. start = end;
    15. end = temp;
    16. }
    17. for (int i = start <= 2 ? 3 : start; i <= end; ++i) {
    18. boolean isPrime = true;
    19. for (int j = 2; j * j <= i; j++) {
    20. if (i % j == 0) {
    21. isPrime = false;
    22. break;
    23. }
    24. }
    25. count += isPrime ? 1 : 0;
    26. }
    27. System.out.println(start+"到"+end+"之间有"+count+"个大于2的素数");
    28. }
    29. }

    JAVA50 根据周长求面积

    1. import java.util.Scanner;
    2. public class Main {
    3. public static void main(String[] args) {
    4. Scanner scanner = new Scanner(System.in);
    5. while (scanner.hasNextDouble()) {
    6. double s = scanner.nextDouble();
    7. // Circle和Square是需要你定义的类
    8. System.out.println(String.format("%.3f",new Circle(s).getArea()));
    9. System.out.println(String.format("%.3f", new Square(s).getArea()));
    10. }
    11. }
    12. }
    13. class Shape {
    14. private double s; // 周长
    15. public Shape(double s) {
    16. this.s = s;
    17. }
    18. public double getS() {
    19. return s;
    20. }
    21. }
    22. interface Area {
    23. double getArea(); // 面积
    24. }
    25. // 圆形
    26. class Circle extends Shape implements Area {
    27. //write your code here......
    28. public Circle(double s) {
    29. super(s);
    30. }
    31. public double getArea() {
    32. return getS() * getS() / (4 * Math.PI);
    33. }
    34. }
    35. // 方形
    36. class Square extends Shape implements Area {
    37. //write your code here......
    38. public Square(double s) {
    39. super(s);
    40. }
    41. public double getArea() {
    42. return getS() * getS() / 16;
    43. }
    44. }

    JAVA51 冒泡排序

    1. import java.util.Arrays;
    2. import java.util.Scanner;
    3. public class Main {
    4. public static void main(String[] args) {
    5. Scanner scanner = new Scanner(System.in);
    6. int[] arr = new int[7];
    7. for (int i = 0; i < arr.length; i++) {
    8. arr[i] = scanner.nextInt();
    9. }
    10. scanner.close();
    11. //write your code here......
    12. for (int j = arr.length - 1; j > 0; j--) {
    13. for (int i = j - 1; i >= 0; i--) {
    14. if (arr[i] > arr[j]) {
    15. int temp = arr[i];
    16. arr[i] = arr[j];
    17. arr[j] = temp;
    18. }
    19. }
    20. }
    21. for (int k = 0; k < arr.length; k++) {
    22. System.out.print(arr[k]+" ");
    23. }
    24. }
    25. }
  • 相关阅读:
    网页版组态软件:Sovit2D Web组态可视化编辑器
    docker入门配置
    day30 SQL注入&CTF&二次&堆叠&DNS带外
    Redis的内存淘汰策略(简单版)
    Vue路由跳转至页面后多次渲染
    python,满分,砝码称重【第十二届】【省赛】【研究生组】
    Kubectl 使用详解——k8s陈述式资源管理
    2022 年 25 大 Java 8 面试问题和答案 - 从基础到有经验
    「开源系统」mybatis-plus代码生成工具(自己基于官方的封装的,打包成了maven插件的方式)
    C++ 读取txt文件,按行读取,每行按照空格分隔
  • 原文地址:https://blog.csdn.net/liulizhi1996/article/details/127970735