- import java.util.Scanner;
-
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- double d = scanner.nextDouble();
- System.out.println(Main.typeConversion(d));
- }
- public static int typeConversion(double d){
-
- //write your code here......
- return (int)d;
-
- }
- }
- import java.util.Scanner;
-
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int a = scanner.nextInt();
- int b = scanner.nextInt();
- scanner.close();
-
- //write your code here......
- int sum = a + b;
- int difference = a > b ? a - b : b - a;
- int product = a * b;
- int quotient = a > b ? a / b : b / a;
- int module = a > b ? a % b : b % a;
- System.out.println(sum + " " + difference + " " + product + " " + quotient + " " + module);
-
- }
- }
- import java.util.Scanner;
-
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- double d= scanner.nextDouble();
-
- //write your code here......
- int i = (int)(d * 10) % 10 >= 5 ? (int)d + 1 : (int)d;
-
- System.out.println(i);
- }
- }
- import java.util.Scanner;
-
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int a = scanner.nextInt();
- int b = scanner.nextInt();
-
- //write your code here.......
- int temp = a;
- a = b;
- b = temp;
-
-
- System.out.println(a+" "+b);
- }
- }
- import java.util.*;
-
- public class Main {
- public static void main(String[] args) {
- Scanner console = new Scanner(System.in);
- int price = console.nextInt();
- int cost = 0;
-
- //write your code here......
- if (price < 100) {
- cost = price;
- } else if (price < 500) {
- cost = (int)(price * 0.9);
- } else if (price < 2000) {
- cost = (int)(price * 0.8);
- } else if (price < 5000) {
- cost = (int)(price * 0.7);
- } else {
- cost = (int)(price * 0.6);
- }
-
- System.out.println(cost);
- }
- }
- import java.util.Scanner;
-
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- double height = scanner.nextDouble();
- double weight = scanner.nextDouble();
-
- //write your code here......
- double bmi = weight / (height * height);
- if (bmi < 18.5) {
- System.out.println("偏瘦");
- } else if (bmi < 20.9) {
- System.out.println("苗条");
- } else if (bmi < 24.9) {
- System.out.println("适中");
- } else {
- System.out.println("偏胖");
- }
-
- }
- }
- import java.util.Scanner;
-
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String grade =scanner.next();
-
- //write your code here......
- switch (grade) {
- case "A":
- System.out.println("优秀");
- break;
- case "B":
- System.out.println("良好");
- break;
- case "C":
- System.out.println("及格");
- break;
- case "D":
- System.out.println("不及格");
- break;
- default:
- System.out.println("未知等级");
- }
-
- }
- }
- import java.util.Scanner;
-
- public class Main {
- public static void main(String[] args) {
-
- Scanner scanner = new Scanner(System.in);
- String str = scanner.next();
- String emailMatcher="[a-zA-Z0-9]+@[a-zA-Z0-9]+\\.[a-zA-Z0-9]+";
-
- //write your code here......
- if (str.matches(emailMatcher)) {
- System.out.println("邮箱格式合法");
- } else {
- System.out.println("邮箱格式不合法");
- }
-
- }
- }
- public class Main {
- public static void main(String[] args) {
-
- //write your code here........
- long sum = 0;
- for (long i = 9; i <= 9999999999L; i = i * 10 + 9) {
- sum += i;
- }
- System.out.println(sum);
-
- }
- }
- import java.util.*;
-
- public class Main {
- public static void main(String[] args) {
- int count = 0;
- Scanner scanner = new Scanner(System.in);
-
- //write your code here......
- while (scanner.nextInt() > 0) {
- count++;
- }
- System.out.println(count);
-
- }
- }
- import java.util.*;
-
- public class Main {
- public static void main(String[] args) {
- Scanner console = new Scanner(System.in);
- int m = console.nextInt();
- int n = console.nextInt();
- int result = getCM(m, n);
- System.out.println(result);
- }
-
- public static int getCM(int m, int n){
-
- //write your code here......
- int a = m > n ? m : n;
- int b = m > n ? n : m;
-
- while (b > 0) {
- int temp = a % b;
- a = b;
- b = temp;
- }
-
- return m * n / a;
-
- }
- }
- import java.util.Scanner;
-
- public class Main {
- public static void main(String[] args) {
- Scanner scanner=new Scanner(System.in);
- float h=scanner.nextFloat();
- int n =scanner.nextInt();
-
- //write your code here......
- float sum = h;
- h /= 2;
- for (int i = 1; i < n; ++i) {
- sum += h * 2;
- h /= 2;
- }
- System.out.println(String.format("%.3f", h)+" "+String.format("%.3f", sum));
-
- //输出格式为:System.out.println(String.format("%.3f", h)+" "+String.format("%.3f", sum));
-
-
- }
- }
- import java.util.Scanner;
-
- public class Main {
-
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
-
- //write your code here......
- int number;
- double avg = 0;
- int count = 0;
- while ((number = scan.nextInt()) != -1) {
- avg += number;
- count++;
- }
- avg /= count;
-
- //输出格式为:System.out.println(String.format("%.2f",avg));
- System.out.println(String.format("%.2f",avg));
-
- }
- }
- import java.util.Scanner;
-
- public class Main {
- public static void main(String[] args) {
- Main main = new Main();
- Scanner scan = new Scanner(System.in);
- int number = scan.nextInt();
- System.out.println(main.isPrimeNumber(number));
- }
-
- public Boolean isPrimeNumber(int number) {
-
- //write your code here......
- for (int i = 2; i * i <= number; i++) {
- if (number % i == 0) {
- return false;
- }
- }
- return true;
-
- }
- }
- import java.util.Scanner;
-
- public class Main {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- int num = scan.nextInt();
- scan.close();
-
- //write code here......
- if (num > 0) {
- int numOfDigits = 0;
- while (num > 0) {
- numOfDigits++;
- num /= 10;
- }
- System.out.println(numOfDigits);
- } else {
- System.out.println(num);
- }
-
- }
- }
- import java.util.Scanner;
-
- public class Main {
- public static void main(String[] args) {
- int[] ary = new int[6];
- int max;
- int min;
- Scanner scanner = new Scanner(System.in);
- for (int i = 0; i
- ary[i]=scanner.nextInt();
- }
-
- //write your code here......
- max = ary[0];
- min = ary[0];
- for (int i = 1; i < ary.length; i++) {
- max = max < ary[i] ? ary[i] : max;
- min = min > ary[i] ? ary[i] : min;
- }
-
- System.out.println(max+" "+min);
- }
- }
JAVA17 数组倒转
- import java.util.Arrays;
- import java.util.Scanner;
-
- public class Main {
- public static void main(String[] args) {
- int[] arr = new int[6];
- Scanner scanner = new Scanner(System.in);
- for (int i = 0; i < arr.length; i++) {
- arr[i] = scanner.nextInt();
- }
- System.out.println(Arrays.toString(arr));
-
- //write your code here......
- for (int i = 0, j = arr.length - 1; i < j; i++, j--) {
- int temp = arr[i];
- arr[i] = arr[j];
- arr[j] = temp;
- }
-
-
- System.out.println(Arrays.toString(arr));
- }
- }
JAVA18 二维数组求和
- public class Main {
- public static void main(String[] args) {
- int[][] arr = {{11,33,55},{22,44,66,88},{131,214,315,146},{928,827,726,625},{424,525}};
- int sum=add(arr);
- System.out.println(sum);
- }
-
- public static int add(int[][] arr) {
- int sum=0;
-
- //write your code here......
- for (int i = 0; i < arr.length; i++) {
- for (int j = 0; j < arr[i].length; j++) {
- sum += arr[i][j];
- }
- }
-
- return sum;
- }
- }
JAVA19 修改Data类的定义
- import java.util.Scanner;
-
- public class Main {
-
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- while (scanner.hasNextInt()) {
- int x = scanner.nextInt();
- int y = scanner.nextInt();
- Data data = new Data(x, y);
- System.out.println(data.getX() + data.getY());
- }
- }
-
- }
-
- class Data {
-
- private int x;
- private int y;
-
- Data(int x, int y) {
- this.x = x;
- this.y = y;
- }
-
- public int getX() {
- return x;
- }
-
- public int getY() {
- return y;
- }
-
- }
JAVA20 验证年龄
- import java.util.Scanner;
-
- public class Main {
-
- public static void main(String[] args) {
- Person p = new Person();
- Scanner scanner = new Scanner(System.in);
- while (scanner.hasNextInt()) {
- int age = scanner.nextInt();
- p.setAge(age);
- System.out.println(p.getAge());
- }
- }
-
- }
-
- class Person {
-
- private int age;
-
- //write your code here......
- public void setAge(int age) {
- this.age = age;
- }
-
- public int getAge() {
- if (age < 0) {
- return 0;
- } else if (age > 200) {
- return 200;
- } else {
- return age;
- }
- }
-
- }
JAVA21 补全构造方法
- import java.util.Scanner;
-
- public class Main {
-
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- while (scanner.hasNextInt()) {
- int x = scanner.nextInt();
- int y = scanner.nextInt();
- int z = scanner.nextInt();
- Sub sub = new Sub(x, y, z);
- System.out.println(sub.calculate());
- }
- }
-
- }
-
- class Base {
-
- private int x;
- private int y;
-
- public Base(int x, int y) {
- this.x = x;
- this.y = y;
- }
-
- public int getX() {
- return x;
- }
-
- public int getY() {
- return y;
- }
-
- }
-
- class Sub extends Base {
-
- private int z;
-
- public Sub(int x, int y, int z) {
-
- //write your code here
- super(x, y);
- this.z = z;
-
- }
-
- public int getZ() {
- return z;
- }
-
- public int calculate() {
- return super.getX() * super.getY() * this.getZ();
- }
-
- }
JAVA22 重写计算逻辑
- import java.util.Scanner;
-
- public class Main {
-
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- while (scanner.hasNextInt()) {
- int x = scanner.nextInt();
- int y = scanner.nextInt();
- Sub sub = new Sub(x, y);
- sub.calculate();
- }
- }
-
- }
-
- class Base {
-
- private int x;
- private int y;
-
- public Base(int x, int y) {
- this.x = x;
- this.y = y;
- }
-
- public int getX() {
- return x;
- }
-
- public int getY() {
- return y;
- }
-
- public void calculate() {
- System.out.println(getX() * getY());
- }
-
- }
-
- class Sub extends Base {
-
- //write your code here......
- public Sub(int x, int y) {
- super(x, y);
- }
-
- public void calculate() {
- System.out.println(getY() == 0 ? "Error" : getX() / getY());
- }
-
- }
JAVA23 定义打印方法
- import java.util.Scanner;
-
- public class Main {
-
- public static void main(String[] args) throws Exception {
- Scanner scanner = new Scanner(System.in);
- while (scanner.hasNext()) {
- String className = scanner.next();
- // print就是需要你定义的方法
- print(Class.forName(className).newInstance());
- }
- }
-
- //write your code here......
- private static void print(Object clazz) {
- System.out.println(clazz.toString());
- }
-
- }
-
- class First {
- public String toString() {
- return "First";
- }
- }
-
- class Second {
- public String toString() {
- return "Second";
- }
- }
-
- class Third {
- public String toString() {
- return "Third";
- }
- }
JAVA24 类型判断
- import java.util.Scanner;
-
- public class Main {
-
- public static void main(String[] args) throws Exception {
- Scanner scanner = new Scanner(System.in);
- while (scanner.hasNext()) {
- String className = scanner.next();
- Base obj = (Base) Class.forName(className).newInstance();
- System.out.println(getClassName(obj));
- }
- }
-
- public static String getClassName(Base obj) {
-
- //write your code here......
- return obj.getClass().getName();
-
- }
-
- }
-
- class Base {
-
- }
-
- class Sub1 extends Base {
-
- }
-
- class Sub2 extends Base {
-
- }
JAVA25 实现抽象方法
- import java.util.Scanner;
-
- public class Main {
-
- public static void main(String[] args) {
- // Sub是需要你定义的子类
- Base base = new Sub();
-
- Scanner scanner = new Scanner(System.in);
- while (scanner.hasNextInt()) {
- int x = scanner.nextInt();
- int y = scanner.nextInt();
- base.setX(x);
- base.setY(y);
- System.out.println(base.calculate());
- }
- }
-
- }
-
- abstract class Base {
-
- private int x;
- private int y;
-
- public int getX() {
- return x;
- }
-
- public void setX(int x) {
- this.x = x;
- }
-
- public int getY() {
- return y;
- }
-
- public void setY(int y) {
- this.y = y;
- }
-
- public int calculate() {
- if (avg() == 0) {
- return 0;
- } else {
- return sum() / avg();
- }
- }
-
- /**
- * 返回x和y的和
- */
- public abstract int sum();
-
- /**
- * 返回x和y的平均值
- */
- public abstract int avg();
-
- }
-
- class Sub extends Base {
-
- //write your code here......
- public int sum() {
- return getX() + getY();
- }
-
- public int avg() {
- return (getX() + getY()) / 2;
- }
-
- }
JAVA26 实现接口
- import java.util.Scanner;
-
- public class Main {
-
- public static void main(String[] args) {
- Comparator comparator = new ComparatorImpl();
-
- Scanner scanner = new Scanner(System.in);
- while (scanner.hasNextInt()) {
- int x = scanner.nextInt();
- int y = scanner.nextInt();
- System.out.println(comparator.max(x, y));
- }
- }
-
- }
-
- interface Comparator {
- /**
- * 返回两个整数中的最大值
- */
- int max(int x, int y);
- }
-
- //write your code here......
- class ComparatorImpl implements Comparator {
- public int max(int x, int y) {
- return x > y ? x : y;
- }
- }
JAVA27 重写父类方法
- import java.util.Scanner;
-
- public class Main {
-
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- while (scanner.hasNextInt()) {
- int x = scanner.nextInt();
- int y = scanner.nextInt();
- Sub sub = new Sub(x, y);
- System.out.println(sub.sum());
- }
- }
-
- }
-
- class Base {
-
- private int x;
- private int y;
-
- public Base(int x, int y) {
- this.x = x;
- this.y = y;
- }
-
- public int getX() {
- return x;
- }
-
- public final int getY() {
- return y;
- }
-
- public final int sum() {
- return getX() + getY();
- }
-
- }
-
- class Sub extends Base {
-
- public Sub(int x, int y) {
- super(x, y);
- }
-
- //write your code here......
- public int getX() {
- return 10 * super.getX();
- }
- }
JAVA28 创建单例对象
- public class Main {
-
- public static void main(String[] args) {
- Singleton s1 = Singleton.getInstance();
- Singleton s2 = Singleton.getInstance();
- System.out.println(s1 == s2);
- }
-
- }
-
- class Singleton {
-
- private static Singleton instance;
-
- private Singleton() {
-
- }
-
- //write your code here......
- public static Singleton getInstance() {
- return instance;
- }
-
- }
JAVA29 动态字符串
- import java.util.Scanner;
-
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String str = scanner.next();
-
- //write your code here......
- StringBuilder modifiedStr = new StringBuilder();
- int ptr = str.length();
- modifiedStr.append(str, Math.max(ptr - 3, 0), ptr);
- ptr -= 3;
- while (ptr > 0) {
- modifiedStr.insert(0, str.substring(Math.max(ptr - 3, 0), ptr) + ",");
- ptr -= 3;
- }
-
- System.out.println(modifiedStr.toString());
-
- }
- }
JAVA30 统计字符串中字母出现次数
- import java.util.Scanner;
-
- public class Main {
- public static void main(String[] args) {
- String string = "H e l l o ! n o w c o d e r";
- Scanner scanner= new Scanner(System.in);
- String word = scanner.next();
- scanner.close();
- System.out.println(check(string, word));
- }
-
- public static int check(String str, String word) {
-
- //write your code here......
- char query = word.charAt(0);
- int count = 0;
- for (int i = 0; i < str.length(); i++) {
- if (str.charAt(i) == query) {
- count++;
- }
- }
- return count;
-
- }
- }
JAVA31 十进制数转二进制数
- import java.util.*;
-
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int num = scanner.nextInt();
-
- //write your code here......
- System.out.println(Integer.toBinaryString(num));
-
- }
- }
JAVA33 掷骰子游戏
- import java.util.Random;
- import java.util.Scanner;
-
- public class Main {
-
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- while (scanner.hasNextInt()) {
- int seed = scanner.nextInt();
- Random random = new Random(seed);
-
- //write your code here......
- System.out.println(random.nextInt(6) + 1);
-
- }
- }
-
- }
JAVA34 求绝对值,平方根,对数,正弦值
- import java.util.Scanner;
-
- public class Main {
- public static void main(String[] args) {
- Scanner scanner=new Scanner(System.in);
- double num = scanner.nextDouble();
-
- //write your code here......
- System.out.println(Math.abs(num));
- System.out.println(Math.sqrt(num));
- System.out.println(Math.log(num));
- System.out.println(Math.sin(num));
-
- }
- }
JAVA35 输出某一年的各个月份的天数
- import java.util.Calendar;
- import java.util.Scanner;
-
- public class Main {
- public static void main(String[] args) {
- Scanner console = new Scanner(System.in);
- int year = console.nextInt();
-
- //write your code here......
- Calendar c = Calendar.getInstance();
- for (int month = 1; month <= 12; month++) {
- c.set(year, month - 1, 1);
- System.out.println(year + "年" + month + "月:" + c.getActualMaximum(Calendar.DAY_OF_MONTH) + "天");
- }
-
- }
- }
JAVA36 日期换算
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.Scanner;
-
- public class Main {
- public static void main(String[] args) throws ParseException {
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- Scanner in = new Scanner(System.in);
- String str1 = in.nextLine();
-
- //write your code here......
- try {
- Date date = new SimpleDateFormat("yyyy MM dd HH mm ss").parse(str1);
- System.out.println("北京时间为:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date));
- System.out.println("纽约时间为:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date.getTime() - 12 * 60 * 60 * 1000));
-
- } catch (ParseException ex) {
- System.out.println("您输入的数据不合理");
- }
-
- }
- }
JAVA37 判断学生成绩
- import java.util.*;
-
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int score = scanner.nextInt();
-
- //write your code here......
- try {
- if (0 <= score && score <= 100) {
- System.out.println(score);
- } else {
- throw new ScoreException("分数不合法");
- }
- } catch (ScoreException ex) {
- System.out.println(ex.getMessage());
- }
- }
- }
-
- class ScoreException extends Exception {
-
- //write your code here......
- public ScoreException() {
- super();
- }
-
- public ScoreException(String message) {
- super(message);
- }
-
- }
JAVA38 字符串去重
- import java.util.HashSet;
- import java.util.Scanner;
-
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String str = scanner.nextLine();
- scanner.close();
- HashSet
hs = new HashSet<>(); -
- //write your code here......
- for (int i = 0; i < str.length(); i++) {
- hs.add(str.charAt(i));
- }
-
- for (char c:hs) {
- System.out.print(c);
- }
- }
- }
JAVA39 集合遍历
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Scanner;
-
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- List
list = new ArrayList<>(); - int num1 = scanner.nextInt();
- int num2 = scanner.nextInt();
- int num3 = scanner.nextInt();
- int num4 = scanner.nextInt();
- int num5 = scanner.nextInt();
- scanner.close();
- list.add(num1);
- list.add(num2);
- list.add(num3);
- list.add(num4);
- list.add(num5);
- System.out.print("普通for循环:");
-
- //write your code here......
- for (int i = 0; i < list.size(); i++) {
- System.out.print(list.get(i) + " ");
- }
-
-
- System.out.println();
- System.out.print("增强for循环:");
-
- //write your code here......
- for (Integer x : list) {
- System.out.print(x + " ");
- }
-
-
- System.out.println();
- System.out.print("迭代器遍历:");
-
- //write your code here......
- Iterator iter = list.listIterator();
- while (iter.hasNext()) {
- System.out.print(iter.next() + " ");
- }
-
- System.out.println();
- }
- }
JAVA40 排队系统
- import java.util.*;
-
- public class Main {
- public static void main(String[] args) {
- Guest guest1 = new Guest("小明",false);
- Guest guest2 = new Guest("小军",false);
- Guest vipGuest = new Guest("小红",true);
- Deque
deque = new ArrayDeque<>(); - deque.add(guest1);
- deque.add(guest2);
-
- //write your code here......
- deque.push(vipGuest);
-
-
- System.out.println(deque);
- }
- }
- class Guest{
- String name;
- Boolean vip;
-
- @Override
- public String toString() {
- return name;
- }
-
- public Guest(String name, Boolean vip) {
- this.name = name;
- this.vip = vip;
-
- }
- }
JAVA41 Head and Tail of the Queue
- import java.util.ArrayDeque;
- import java.util.Scanner;
-
- public class Main {
-
- public static void main(String[] args) {
- ArrayDeque deque = new ArrayDeque();
-
- Scanner scanner = new Scanner(System.in);
- while (scanner.hasNext()) {
- String name = scanner.next();
- // 初始化队列中的数据
- deque.offerLast(name);
- }
-
- // write your code here......
- boolean fromFirst = true;
- while (!deque.isEmpty()) {
- if (fromFirst) {
- System.out.println(deque.pollFirst());
- } else {
- System.out.println(deque.pollLast());
- }
- fromFirst = !fromFirst;
- }
-
- }
-
- }
JAVA42 统计一句话中重复单词的个数
- import java.util.*;
-
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String line = scanner.nextLine();
- Map
map = new LinkedHashMap(); -
- //write your code here......
- for (int i = 0; i < line.length(); i++) {
- char ch = line.charAt(i);
- if (('A' <= ch && ch <= 'Z') || ('a' <= ch && ch <= 'z')) {
- map.put(ch, map.getOrDefault(ch, 0) + 1);
- }
- }
-
-
- Set
> entrys = map.entrySet(); - for (Map.Entry
entry : entrys) { - System.out.println(entry.getKey() + ":" + entry.getValue());
- }
- }
- }
JAVA43 map简单应用
- import java.util.*;
-
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String name = scanner.next();
- Map
map = new HashMap(); - map.put(1, "Amy");
- map.put(2, "Joe");
- map.put(3, "Tom");
- map.put(4, "Susan");
-
- //write your code here......
- for (Integer key : map.keySet()) {
- System.out.println(key + ":" + map.get(key));
- }
- map.put(5, name);
- map.remove(4);
- map.replace(3, "Tommy");
- System.out.println();
- for (Integer key : map.keySet()) {
- System.out.println(key + ":" + map.get(key));
- }
-
- }
- }
JAVA44 集合排序
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.List;
- import java.util.Scanner;
-
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- Customer customer1 = new Customer("小明",scanner.nextInt());
- Customer customer2 = new Customer("小军",scanner.nextInt());
- Customer customer3 = new Customer("小红",scanner.nextInt());
- List
customers = new ArrayList<>(); - customers.add(customer1);
- customers.add(customer2);
- customers.add(customer3);
-
- //write your code here......
- Collections.sort(customers);
-
- System.out.println(customers);
-
- }
- }
-
- class Customer implements Comparable
{ - private String name;
- private int consumption;
-
- public Customer(String name, int consumption) {
- this.name = name;
- this.consumption = consumption;
- }
-
- @Override
- public String toString() {
- return "Customer{" +
- "name='" + name + '\'' +
- ", consumption=" + consumption +
- '}';
- }
-
- //write your code here......
- public int getConsumption() {
- return consumption;
- }
-
- public int compareTo(Customer customer) {
- return customer.getConsumption() - this.consumption;
- }
-
- }
JAVA45 判断各类型字符个数
- import java.util.Scanner;
-
- public class Main {
- public static void main(String[] args) {
- int numbers = 0;
- int words = 0;
- int space = 0;
- int other = 0;
- Scanner scanner = new Scanner(System.in);
- String str = scanner.nextLine();
-
- //write your code here......
- for (int i = 0; i < str.length(); i++) {
- char ch = str.charAt(i);
- if (('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z')) {
- words++;
- } else if ('0' <= ch && ch <= '9') {
- numbers++;
- } else if (ch == ' ') {
- space++;
- } else {
- other++;
- }
- }
-
- System.out.println("英文字母"+words+"数字"+numbers+"空格"+space+"其他"+other);
- }
- }
JAVA46 编写个人所得税计算程序
- import java.util.*;
-
- public class Main {
- public static void main(String[] args) {
- List
employees = new ArrayList<>(); -
- //write your code here......
- employees.add(new Employee("小明", 2500));
- employees.add(new Employee("小军", 8000));
- employees.add(new Employee("小红", 100000));
-
- for (Employee employee : employees) {
- double salaryForTax = employee.getSalary() - 3500;
- double tax = 0;
- if (salaryForTax <= 0) {
- tax = 0;
- } else if (salaryForTax <= 1500) {
- tax = salaryForTax * 0.03;
- } else if (salaryForTax <= 4500) {
- tax = salaryForTax * 0.10 - 105;
- } else if (salaryForTax <= 9000) {
- tax = salaryForTax * 0.20 - 555;
- } else if (salaryForTax <= 35000) {
- tax = salaryForTax * 0.25 - 1005;
- } else if (salaryForTax <= 55000) {
- tax = salaryForTax * 0.30 - 2755;
- } else if (salaryForTax <= 80000) {
- tax = salaryForTax * 0.35 - 5505;
- } else {
- tax = salaryForTax * 0.45 - 13505;
- }
- System.out.println(employee.getName() + "应该缴纳的个人所得税是:" + String.format("%.1f", tax));
- }
-
- }
- }
- class Employee{
- private String name;
- private double salary;
- public Employee(String name, double salary) {
- this.name = name;
- this.salary = salary;
- }
- public String getName() {
- return name;
- }
-
- public double getSalary() {
- return salary;
- }
- }
JAVA47 记录点赞用户
- import java.util.*;
-
- public class Main {
-
- public static void main(String[] args) {
- LikeRecorder recorder = new LikeRecorderImpl();
-
- Scanner scanner = new Scanner(System.in);
- while (scanner.hasNext()) {
- String name = scanner.next();
- recorder.like(name);
- }
-
- System.out.println(Arrays.toString(recorder.getLikeUsers()));
- }
-
- }
-
- /**
- * 点赞记录器
- */
- interface LikeRecorder {
-
- /**
- * 若用户没有点赞过,则记录此次点赞行为。
- * 若用户曾经点赞过,则删除用户点赞记录。
- *
- * @param username 用户名
- */
- void like(String username);
-
- /**
- * 返回所有点赞的用户名
- *
- * @return 用户名数组
- */
- String[] getLikeUsers();
-
- }
-
- class LikeRecorderImpl implements LikeRecorder {
-
- // write your code here......
- private HashSet
users = new HashSet<>(); -
- public void like(String username) {
- if (users.contains(username)) {
- users.remove(username);
- } else {
- users.add(username);
- }
- }
-
- public String[] getLikeUsers() {
- return users.toArray(new String[users.size()]);
- }
-
- }
JAVA48 回文数判断
- import java.util.Scanner;
-
- public class Main {
- public static void main(String[] args) {
- Scanner console = new Scanner(System.in);
- Main main = new Main();
- int number = console.nextInt();
- System.out.println(main.palindromeNumber(number));
- }
-
- public Boolean palindromeNumber(int number) {
-
- //write your code here......
- StringBuffer numStr = new StringBuffer("" + number);
- return numStr.toString().equals(numStr.reverse().toString());
-
- }
- }
JAVA49 判断素数个数
- import java.util.Scanner;
-
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int start = scanner.nextInt();
- int end = scanner.nextInt();
- method(start,end);
- }
-
- public static void method(int start, int end) {
- int count=0;
-
- //write your code here......
- if (start > end) {
- int temp = start;
- start = end;
- end = temp;
- }
-
- for (int i = start <= 2 ? 3 : start; i <= end; ++i) {
- boolean isPrime = true;
- for (int j = 2; j * j <= i; j++) {
- if (i % j == 0) {
- isPrime = false;
- break;
- }
- }
- count += isPrime ? 1 : 0;
- }
-
-
- System.out.println(start+"到"+end+"之间有"+count+"个大于2的素数");
- }
- }
JAVA50 根据周长求面积
- import java.util.Scanner;
-
- public class Main {
-
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- while (scanner.hasNextDouble()) {
- double s = scanner.nextDouble();
- // Circle和Square是需要你定义的类
- System.out.println(String.format("%.3f",new Circle(s).getArea()));
- System.out.println(String.format("%.3f", new Square(s).getArea()));
- }
- }
-
- }
-
- class Shape {
-
- private double s; // 周长
-
- public Shape(double s) {
- this.s = s;
- }
-
- public double getS() {
- return s;
- }
-
- }
-
- interface Area {
- double getArea(); // 面积
- }
-
- // 圆形
- class Circle extends Shape implements Area {
-
- //write your code here......
- public Circle(double s) {
- super(s);
- }
-
- public double getArea() {
- return getS() * getS() / (4 * Math.PI);
- }
-
- }
-
- // 方形
- class Square extends Shape implements Area {
-
- //write your code here......
- public Square(double s) {
- super(s);
- }
-
- public double getArea() {
- return getS() * getS() / 16;
- }
-
- }
JAVA51 冒泡排序
- import java.util.Arrays;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int[] arr = new int[7];
- for (int i = 0; i < arr.length; i++) {
- arr[i] = scanner.nextInt();
- }
- scanner.close();
-
- //write your code here......
- for (int j = arr.length - 1; j > 0; j--) {
- for (int i = j - 1; i >= 0; i--) {
- if (arr[i] > arr[j]) {
- int temp = arr[i];
- arr[i] = arr[j];
- arr[j] = temp;
- }
- }
- }
-
-
- for (int k = 0; k < arr.length; k++) {
- System.out.print(arr[k]+" ");
- }
- }
- }