• java小算法复习


    package com.bshinfo.bm.util;

    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Map;
    import java.util.Scanner;

    public class Test {

    //菱形
    public void test1(){
    for (int i = 0; i < 5; i++) {//行数
    for (int j = 0; j <5-i-1; j++) {//空格
    System.out.print(" ");
    }
    for (int k = 0; k < 2*(i+1)-1; k++) {//星号
    System.out.print("*");
    }
    System.out.println("");
    }
    for (int i = 5; i > 0; i--) {//行数
    for (int j =5-i+1; j >0; j--) {//空格
    System.out.print(" ");
    }
    for (int k = 2*(i-1)-1; k >0; k--) {//星号
    System.out.print("*");
    }
    System.out.println("");
    }
    }
    //菱形
    public void test2(int n){
    for(int i = 0; i < n - 1; i++){
    for(int x = i + 1; x < n; x++){
    System.out.print(" ");
    }
    for(int y = 0; y < (i + 1) * 2 - 1; y++){
    System.out.print("*");
    }
    System.out.println();
    }
    for(int i = 0; i < n; i++){
    for(int x = 0; x < i; x++){
    System.out.print(" ");
    }
    for(int y = i; y < 2 * n - i - 1; y++){
    System.out.print("*");
    }
    System.out.println();
    }
    }

    //三角形
    public void test3(){
    for (int i = 0; i < 5; i++) {
    for (int j = 0; j < 5-i-1; j++) {
    System.out.print(" ");
    }
    for (int j = 0; j < i+1; j++) {
    System.out.print("*");
    }
    System.out.println("");
    }
    }

    //九九乘法表
    public void test4(){

    for (int i = 1; i < 10; i++) {
    for (int j = 1; j < i+1; j++) {
    System.out.print(j+"*"+i+"="+(i*j)+" ");
    }
    System.out.println("");
    }
    }
    //统计字符串中字符出现的个数
    public void test5(){
    String str = "asdfsadfewrtewfxzfagres";
    Map<String,Integer> map =new HashMap<String,Integer>();
    int count = str.length();
    for (int i = 0; i < count; i++) {
    if(map.containsKey(str.substring(i, i+1))){
    int temp = map.get(str.substring(i, i+1));
    temp++;
    map.put(str.substring(i, i+1), temp);
    }else{
    map.put(str.substring(i, i+1),1);
    }
    }
    for (String key:map.keySet()) {
    int value = map.get(key);
    System.out.println("字符 "+key+" 出现的次数是"+value);
    }
    }
    //判断是否输入的是水仙花数
    public void test6(){
    Scanner sc = new Scanner(System.in);
    String str = sc.nextLine();
    int count = str.length();
    if(count%2!=0){
    int temp = 0;
    for (int i = 0; i < count/2; i++) {
    int temp1 = Integer.parseInt(str.substring(i, i+1));
    int temp2 = Integer.parseInt(str.substring(count-i-1, count-i));
    if(temp1!=temp2){
    System.out.println(str +" 不是水仙花数!");
    return;
    }else{
    temp++;
    }
    }
    if(temp == (count/2)){
    System.out.println(str +" 是水仙花数!");
    }
    }

    }
    public static void main(String[] args) {

    Test test = new Test();
    test.test1();
    test.test2(5);
    test.test3();
    test.test4();
    test.test5();
    test.test6();
    }

    }

  • 相关阅读:
    js字符串转数组,转对象方法
    react执行yarn eject后配置antd的按需加载
    DOM对象与jquery对象区别
    vscode使用git管理代码
    使用vscode编辑器编辑CPU100%使用率问题
    Java 多态
    1,随机生成一个500m的txt,填充内容为小写的26个字母。生成后,查找abc字符,打印出其数量和位置(越快越好)
    bat 文件
    word2Html
    生成压缩文件
  • 原文地址:https://www.cnblogs.com/mybug/p/7053562.html
Copyright © 2020-2023  润新知