我正在练习算法,假设我们有一个包含元素的数组2, 3, 9, 12, 7, 18,然后我只想打印 18 ,因为它是 9 的两倍。当我打印结果时,它总是显示更多的行,但是,打印的数字(如果有的话)很好。如何管理正确显示结果?(仅使用数组。)我知道这是一个愚蠢的问题,但我尝试了很多方法,结果更糟。
现在,它向我展示了这一点(140 是正确的,但不需要另一行):
The following number is doubled of another number from the array: 0
The following number is doubled of another number from the array: 0
The following number is doubled of another number from the array: 140
The following number is doubled of another number from the array: 0
The following number is doubled of another number from the array: 0
Run Code Online (Sandbox Code Playgroud)
我的代码:
public class DoubleNums …Run Code Online (Sandbox Code Playgroud) 我正在用 Java 解决一个问题,它在 Netbeans 中完美运行,没有任何错误消息,但是我上传代码的网站编译器给我带来了以下错误:
Compile error: Main.java:16: error: cannot find symbol
String finalString = newString.repeat(n + 1);
^
symbol: method repeat(int)
location: variable newString of type String
Run Code Online (Sandbox Code Playgroud)
我的代码:
Scanner sc = new Scanner(System.in);
String myString = sc.nextLine();
int n = sc.nextInt();
String finalString = "";
for (int i = 0; i < myString.length(); i++) {
String newString = myString.substring(n + 1, myString.length());
finalString = newString.repeat(n + 1);
}
System.out.println(finalString);
}
}
Run Code Online (Sandbox Code Playgroud)
示例输入:
你好
2
示例输出:
lololo