这个问题基本上类似于这里发布的内容一个不能成为For循环的While循环的例子,除了没有一个这样的例子,其中使用while循环的某个程序不能被for循环替换,因为它的限制.
一个例子是
i=0;
while(i<10)
{
continue;
i=i+2;
}
and
for(i=0;i<10;i++)
{
continue;
i++;
}
Run Code Online (Sandbox Code Playgroud)
在for循环中,continue不会停止增量.我想看到不同的东西.
你如何在C编程中使用嵌套循环?
编写一个程序,要求用户输入一个数字,然后显示从0到他们输入的数字的所有数字的乘法表.这应该使用嵌套的for循环来完成.例如,如果用户输入3,他们应该看到:
0 0 0 0
0 1 2 3
0 2 4 6
0 3 6 9
对不起,我是编程方面的新手.我想请求帮助,我想显示从200到400的范围编号,但它不应显示数字250.
这就是我所做的.
int main () {
for (int i=200; i<=400; i++) {
std::cout << "value of i: " << i << endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我很成功地显示了范围号码,但它显示了所有号码.
我在这里写了这段代码.它应该打印出iteNum.length由用户输入确定的(数组),但它只进行一次迭代然后停止.我无法弄清楚为什么.
for (int i = 0; i < iteNum.length; i++) {
System.out.print("Num:" + (i+1) + " ");
for (i = 0; i < cMiles.length; i++) {
System.out.print(" (sc" + (i+1) + ":)" + cRandom[i] + " (tsc" + (i+1) + ":)" + df.format(cTimes[i]) + " ");
}
for (i = 0; i < fMiles.length; i++){
System.out.print(" (sf"+ (i+1) + ":)" + df.format(fRandom[i]) + " (tsf" + (i+1) + ":)" + df.format(fTimes[i])+ " " );
}
System.out.print("(cT:)" + df.format(cSum) …Run Code Online (Sandbox Code Playgroud) 我编写了以下代码来获取从0到1000的所有数字,它们是三的倍数:
public class Hi {
public static void main(String[] args) {
for(int i = 0; i<1000; i++)
if(i % 3 == 0)
System.out.println(i);
}
}
Run Code Online (Sandbox Code Playgroud)
现在我想将这些数字加在一起并在循环后打印结果.
我在下面的代码中给出了一个字符串名称字符串.此字符串包含一个短语,即由单个空格分隔的一个或多个单词.该程序计算并返回此短语的首字母缩写词.
import java.math.*;
import java.util.*;
import static java.util.Arrays.*;
import static java.lang.Math.*;
public class Initials {
public String getInitials(String s) {
String r = "";
for(String t:s.split("\\s+")){
r += t.charAt(0);
}
return r;
}
void p(Object... o) {
System.out.println(deepToString(o));
}
}
Run Code Online (Sandbox Code Playgroud)
示例:"john fitzgerald kennedy"
返回:"jfk"
这是什么意思?
for(Ship s:p.ships)
Ship是类,s是类Ship的对象,p是播放器.
这些是来自游戏Battleship的命令.
我有这个程序:
print()
print ('------MENU------')
print ('1. Welcome to Python')
print ('2. Python is fun')
print ('3. This could be a challenge')
print ('4. Exit')
print()
choice = int(input('please enter a choice between 1 to 4: '))
for choice in (1,5):
if choice ==1:
print ('Welcome to python')
elif choice == 2:
print ('Python is fun')
elif choice == 3:
print ('This could be a challenge')
else:
break
Run Code Online (Sandbox Code Playgroud)
它应该首先打印MENU然后要求输入一个整数.我的问题是为什么每次输入1到3之间的整数时它会打印两次?
我尝试了这些代码行,发现令人震惊的输出。我期待一些与初始化相关的原因,无论是一般还是for循环。
1.)
int i = 0;
for(i++; i++; i++){
if(i>10) break;
}
printf("%d",i);
Run Code Online (Sandbox Code Playgroud)
输出-12
2.)
int i;
for(i++; i++; i++){
if(i>10) break;
}
printf("%d",i);
Run Code Online (Sandbox Code Playgroud)
输出-1
我希望语句“ int i = 0”和“ int i”相同。它们之间有什么区别?
这是我的代码
在这里,我做了一个循环,当 x 的值等于随机数时,它应该会中断
public class Bark {
public static void main(String[] args) {
for (double x = 0 ; x>-1 ; x++) {
System.out.print(x + " ");
if( x == Math.random()) {
break;
}
}
}}
Run Code Online (Sandbox Code Playgroud)