如何用字符、数组、循环、Java 替换整数
How to replace an integer with a character, arrays, loops, Java
我是 Java 的新手,目前,我正在学习数组和循环。我有一个任务,我不知道该做什么,我会寻求帮助。
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*.
我制作了一个模板,但不幸的是,我不知道如何进行所有计算。
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());
}
}
我想创建一个数字数组 1000、100、10、1,然后计算使用了多少个。
不幸的是,我不知道如何将 1000 变成 X 我不知道 2 是否在 ** 中。
请查看我的草稿,也许你可以给我一些建议或提示。
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;
这行得通:
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());
重点是只要星星的数量在一个区间内,在字符串后面追加一个字符,然后减少数量。
我是 Java 的新手,目前,我正在学习数组和循环。我有一个任务,我不知道该做什么,我会寻求帮助。
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*.
我制作了一个模板,但不幸的是,我不知道如何进行所有计算。
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());
}
}
我想创建一个数字数组 1000、100、10、1,然后计算使用了多少个。
不幸的是,我不知道如何将 1000 变成 X 我不知道 2 是否在 ** 中。
请查看我的草稿,也许你可以给我一些建议或提示。
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;
这行得通:
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());
重点是只要星星的数量在一个区间内,在字符串后面追加一个字符,然后减少数量。