java - (Java)如何让这些循环每次都生成一个新字符串?
问题描述
好的,所以hoehe
和breite
是之前从控制台输入中获取的两个整数。这完美无缺。现在我希望它生成一个随机字符串,长度为 breite 的值,将其打印出来,然后执行 hoehe 次。字符串可以包含的唯一字符是/
和\
。现在它会生成一次字符串,然后将其打印出 hoehe 次。
int i = 0;
int y = 0;
double rand;
String output = "";
String case1 = "/";
String case2 = "\\";
while (y < hoehe) {
while (i < breite) {
rand = Math.random();
if (rand < 0.5) {
output += case1;
}
else {
output += case2;
}
i++;
}
System.out.println(output);
y++;
}
解决方案
int i = 0;
int y = 0;
double rand;
String output = "";
String case1 = "/";
String case2 = "\\";
while (y < hoehe) {
while (i < breite) {
rand = Math.random();
if (rand < 0.5) {
output += case1;
}
else {
output += case2;
}
i++;
}
i = 0; ////// add this
System.out.println(output);
y++;
}
推荐阅读
- blockchain - 如何创建只能由资产发行人交易的智能资产?我正在使用 RIDE 语言
- eclipse - 无法启动 CDT:java.lang.ClassNotFoundException:org.eclipse.core.runtime.adaptor.EclipseStarter
- ios - NetworkExtension 监控 WiFi 网络变化,即使应用程序未运行
- javascript - Firebase Cloud Functions:承诺无法解决
- python - while循环最终没有中断
- c# - OxyPlot:如何将所有 OxyColors 绑定为 WPF 组合框的 itemsource?
- java - PreparedStatement 类型中的方法 setString(int, String) 不适用于参数 (int, String[])
- c# - 如何在 C# 中将数组作为未知类型的参数传递?
- python-3.x - 执行列表创建的 Python 3 无法正常工作
- google-cloud-platform - BigQuery 数据集维护