在 for 循环中用字符串替换 int 或 long?
Replace a int or long with a string in for loop?
我需要打印最多 50 个数字的斐波那契数列,我最终弄明白了,但我还需要用某些单词替换某些可被某个数字整除的数字。我有点明白了。我得到了要说的话,但我无法替换数字。我尝试使用 String.valueOf 和 replaceAll,但数字仍然不断出现。我在评论中留下了那部分代码,因为它不起作用。我将 post 我的代码如下:
public static void main(String[] args) {
long n = 50;
System.out.print("1 ");
long x = 0;
long y = 1;
String mult3 = "cheese";
String mult5 = "cake";
String mult7 = "factory";
String mult2 = "blah";
for (long i = 1; i < n; i++) {
long forSum = x + y;
x = y;
y = forSum;
if(forSum==89){
System.out.println("");
}
if(forSum==10946){
System.out.println("");
}
if(forSum==1346269){
System.out.println("");
}
if(forSum==165580141){
System.out.println("");
}
if(forSum %3 == 0){
//String mult3 = String.valueOf(forSum);
//String mult4 = "cheese";
//String mult3cheese = mult3.replaceAll(mult3, mult4);
//System.out.print(mult3cheese);
System.out.print(mult3);
}
if(forSum %5 == 0){
System.out.print(mult5);
}
if(forSum %7 == 0){
System.out.print(mult7);
}
if(forSum %2 == 0){
System.out.print(mult2);
}
System.out.print(forSum + " ");
}//for loop for forSum
}//public static void main
您需要放置一些 "continue" 语句以跳过打印数字的行,如果您匹配了 "replace the number" 条件:
public static void main(String[] args) {
long n = 50;
System.out.print("1 ");
long x = 0;
long y = 1;
String mult3 = "cheese";
String mult5 = "cake";
String mult7 = "factory";
String mult2 = "blah";
for (long i = 1; i < n; i++) {
long forSum = x + y;
x = y;
y = forSum;
if(forSum==89){
continue;
}
if(forSum==10946){
continue;
}
if(forSum==1346269){
continue;
}
if(forSum==165580141){
continue;
}
if(forSum %3 == 0){
//String mult3 = String.valueOf(forSum);
//String mult4 = "cheese";
//String mult3cheese = mult3.replaceAll(mult3, mult4);
//System.out.print(mult3cheese);
System.out.print(mult3 + " ");
continue
}
if(forSum %5 == 0){
System.out.print(mult5 + " ");
continue;
}
if(forSum %7 == 0){
System.out.print(mult7 + " ");
continue;
}
if(forSum %2 == 0){
System.out.print(mult2 + " ");
continue;
}
System.out.print(forSum + " ");
}//for loop for forSum
}//public static void main
"continue" 表示 "skip the rest of the code inside this loop and do the next iteration of the loop"
我需要打印最多 50 个数字的斐波那契数列,我最终弄明白了,但我还需要用某些单词替换某些可被某个数字整除的数字。我有点明白了。我得到了要说的话,但我无法替换数字。我尝试使用 String.valueOf 和 replaceAll,但数字仍然不断出现。我在评论中留下了那部分代码,因为它不起作用。我将 post 我的代码如下:
public static void main(String[] args) {
long n = 50;
System.out.print("1 ");
long x = 0;
long y = 1;
String mult3 = "cheese";
String mult5 = "cake";
String mult7 = "factory";
String mult2 = "blah";
for (long i = 1; i < n; i++) {
long forSum = x + y;
x = y;
y = forSum;
if(forSum==89){
System.out.println("");
}
if(forSum==10946){
System.out.println("");
}
if(forSum==1346269){
System.out.println("");
}
if(forSum==165580141){
System.out.println("");
}
if(forSum %3 == 0){
//String mult3 = String.valueOf(forSum);
//String mult4 = "cheese";
//String mult3cheese = mult3.replaceAll(mult3, mult4);
//System.out.print(mult3cheese);
System.out.print(mult3);
}
if(forSum %5 == 0){
System.out.print(mult5);
}
if(forSum %7 == 0){
System.out.print(mult7);
}
if(forSum %2 == 0){
System.out.print(mult2);
}
System.out.print(forSum + " ");
}//for loop for forSum
}//public static void main
您需要放置一些 "continue" 语句以跳过打印数字的行,如果您匹配了 "replace the number" 条件:
public static void main(String[] args) {
long n = 50;
System.out.print("1 ");
long x = 0;
long y = 1;
String mult3 = "cheese";
String mult5 = "cake";
String mult7 = "factory";
String mult2 = "blah";
for (long i = 1; i < n; i++) {
long forSum = x + y;
x = y;
y = forSum;
if(forSum==89){
continue;
}
if(forSum==10946){
continue;
}
if(forSum==1346269){
continue;
}
if(forSum==165580141){
continue;
}
if(forSum %3 == 0){
//String mult3 = String.valueOf(forSum);
//String mult4 = "cheese";
//String mult3cheese = mult3.replaceAll(mult3, mult4);
//System.out.print(mult3cheese);
System.out.print(mult3 + " ");
continue
}
if(forSum %5 == 0){
System.out.print(mult5 + " ");
continue;
}
if(forSum %7 == 0){
System.out.print(mult7 + " ");
continue;
}
if(forSum %2 == 0){
System.out.print(mult2 + " ");
continue;
}
System.out.print(forSum + " ");
}//for loop for forSum
}//public static void main
"continue" 表示 "skip the rest of the code inside this loop and do the next iteration of the loop"