简体   繁体   中英

How to replace an integer with a character, arrays, loops, Java

I'm new to Java, currently, I'm learning arrays and loops. I have a task, and I don't know what to do, I would use some help.

Write a Stars class.
In this class, declare a count field of type int - the number of stars.

Redefine the toString method in the Stars class. It should return the number of stars in the format accepted by the Intergalactic Guild of Spacewalkers.

1000 stars - X character,
100 stars - Y character,
10 stars - Z character,
1 star - *.

A few examples:

1001 stars - X*,
576 stars - YYYYYZZZZZZZ******,
The minimum number of characters must be used. That is, for example, 101 stars must be represented as Y*, but not as ZZZZZZZZZZ*.

I made a template, but unfortunately, I don't know how to make all the calculations.

public class Stars {

    int count;

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }
    
    @Override
    public String toString() {  
        return Integer.toString(count);
    }
    
    public static void main(String[] args) {
        Stars stars = new Stars();
        stars.setCount(2253); // ZZZ***
        System.out.println(stars);
        System.out.println(stars.getCount());
    }
}

I had an idea to create an array of numbers 1000, 100, 10, 1, then count how many of them are used.

Unfortunately, I have no idea how to turn 1000 into XI have no idea, whether 2 in **.

Please check my draft, maybe you can give me some advice or hint.

public class Test {

    public static void stars(int amount)
    {
        int[] number = new int[]{ 1000, 100, 10, 1 };
        int[] numberCounter = new int[4];
      
        for (int i = 0; i < 4; i++) {
            if (amount >= number[i]) {
                numberCounter[i] = amount / number[i];
                amount = amount - numberCounter[i] * number[i];
            }
        }
        for (int i = 0; i < 4; i++) {
            if (numberCounter[i] != 0) {
                System.out.print(numberCounter[i]);
            }
        }
    }
     
    public static void main(String argc[]){
        int amount = 2253; // should be : XXYYZZZZZ***
        stars(amount);
    }
}
// Or maybe I can use this formula, thank you
//int units = count % 10;
//int tens = (count / 10) % 10;
//int hundreds = (count / 100) % 10;
//int thousands = (count / 100) % 10;

This would work:

 int x = 101;
StringBuilder builder= new StringBuilder();
    while (x>1000) {
        builder.append("X");
        x-=1000;
    }
    while (x<1000&&x>100) {
        builder.append("Y");
        x-=100;
    }
    while (x<100&&x>10) {
        builder.append("Z");
        x-=10;
    }
    while (x<10 && x!=0) {
        builder.append("*");
        x-=1;
    }
        System.out.println(builder.toString());

the point is as long the number of stars is in an interval append a character to the string and then reduce the number.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM