是否可以在开关盒环路中放置一个功能?因为我试过这个只是为了探索更多这个循环.虽然我尝试了其他方法,但我仍然存在问题.谁能帮我?
#include <stdio.h>
int main(void)
{
int choice;
switch(choice);
{
case 1:
{
int GetData()
{
int num;
printf("Enter the amount of change: ");
scanf("%d%*c", &num);
return (num);
}
int getChange (int change,int fifty,int twenty,int ten,int five)
{
int num = change;
fifty = num/50;
num %= 50;
twenty = num/20;
num %= 20;
ten = num/10;
num %= 10;
five = num/5;
num %= 5;
return (fifty, twenty, ten, five);
}
int main()
{
int change, fifty, twenty, ten, …
Run Code Online (Sandbox Code Playgroud) 为什么在检查条件之后而不是在整个循环之后,While循环后递增(或后递减)变量?
如:
int x = 0;
while (x++ < 5)
Console.WriteLine(x);
Run Code Online (Sandbox Code Playgroud)
输出是:1 2 3 4 5,当我认为它应该是0 1 2 3 4.它似乎它立即检查条件 - 真实 - inrements.这是正常的行为,因为我最近一直在练习普通C,这完全不同.
我是否应该更加明确地使用此选项?
while (x < 5)
Console.WriteLine(x);
++x;
Run Code Online (Sandbox Code Playgroud) 我有一个ArrayList,我需要在相反的方向重写它.怎么做?
List<Posts> viewList = new ArrayList<Posts>();
viewList = viewLogic.getPostsList(username);
Run Code Online (Sandbox Code Playgroud) 我想知道设置这样的代码之间的真正区别
public boolean stringE(String str) {
int count = 0;
for (int i =0; i < str.length(); i++) {
if (str.charAt(i) == 'e') {
count += 1;
}
}
return (count >=1 && count<=3 );
}
Run Code Online (Sandbox Code Playgroud)
还有这个
public boolean stringE(String str) {
for (int i =0; i < str.length(); i++) {
int count = 0;
if (str.charAt(i) == 'e') {
count += 1;
}
}
return (count >=1 && count<=3 );
}
Run Code Online (Sandbox Code Playgroud)
我知道第一个是正确的但是通过在"for loop"中设置"int count = 0"会有什么不同.难道它还不会在count = 0中加1吗?
我是Java新手,我不太了解while循环.我想说'如果你不想选择Warrior类,那么回到字符选择框.
while (chooseCharacter = true){
String[] Character= {"Kitty", "Archer","Tank","Battlemage","Warrior"};
int characterSelect = JOptionPane.showOptionDialog (null, "Choose your class", "Class Selection", JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE, null, Character, "Warrior");
if(characterSelect == 4){
JOptionPane.showMessageDialog(null, "Warrior" + "\n" + "Attack 1: Slash" + "\n" + "10 Damage :: 98% Accuracy" + "\n" + "Attack 2: Spin Attack" + "\n" + "25 Damage :: 67% Accuracy");
String[] warriorChar= {"No", "Yes"};
int warrior = JOptionPane.showOptionDialog(null, "Would you like to choose Warrior as your class?", "Choose this class?", JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE, …
Run Code Online (Sandbox Code Playgroud) 我的代码:
num = 5
if num > 2:
print(num)
num -= 1
print(num)
Run Code Online (Sandbox Code Playgroud)
预期产量:
5,4,3,2
Run Code Online (Sandbox Code Playgroud)
实际产量:
5,4
Run Code Online (Sandbox Code Playgroud)
问题:为什么我会收到实际输出而不是预期输出?
为什么这不打印5?它打印10?
tot = 0
for i in range(5):
tot = tot + i
print tot
Run Code Online (Sandbox Code Playgroud) 我希望找到一种方法来有效地模仿while
循环之间相同的可能行为for
,使循环“停滞”,或者在满足条件的情况下退一步。这样做的原因是,我正在尝试执行具有大量迭代的计算,并且for
循环比while
循环快4倍%%timeit
。
%%timeit
n = 0
while n < 1e7:
n += 1
# 1.96 s +- 211 ms per loop
%%timeit
for i in range(int(1e7)):
pass
# 399 ms +- 28.1 ms per loop
Run Code Online (Sandbox Code Playgroud)
对于那些提到这是“不公平”比较的人,while
没有n += 1
语句,循环不能循环,而for
循环可以。因此该行对于while循环是必需的,对于for循环是不必要的,因此这不是“不公平”的比较。
我的特定问题/代码:
def euler_differentiate_mod(w, bounds = None, delta = 1e-3, itern = 1e3,
force = False,
tols = [10, 0.1], step_mults = [0.1, 10]): …
Run Code Online (Sandbox Code Playgroud) 我的数学查询是(1 + 5 + 10 + 15 + 20 + 25 =?)
我如何使用for循环来获得添加结果?
我想为后续的js代码创建一个循环.请帮我.
function fade90() { document.getElementById("myDiv").style.opacity="0.90"; setTimeout("fade80()", 100); }
function fade80() { document.getElementById("myDiv").style.opacity="0.80"; setTimeout("fade70()", 100); }
function fade70() { document.getElementById("myDiv").style.opacity="0.70"; setTimeout("fade60()", 100); }
function fade60() { document.getElementById("myDiv").style.opacity="0.60"; setTimeout("fade50()", 100); }
function fade50() { document.getElementById("myDiv").style.opacity="0.50"; setTimeout("fade40()", 100); }
function fade40() { document.getElementById("myDiv").style.opacity="0.40"; setTimeout("fade30()", 100); }
function fade30() { document.getElementById("myDiv").style.opacity="0.30"; setTimeout("fade20()", 100); }
function fade20() { document.getElementById("myDiv").style.opacity="0.20"; setTimeout("fade10()", 100); }
function fade10() { document.getElementById("myDiv").style.opacity="0.10"; setTimeout("hide()", 100); }
Run Code Online (Sandbox Code Playgroud)
我写这个.它是否正确?如果没有,请解决此问题.
function cls_msg(){
for (var i=1;i<10;i++)
{
setTimeout(document.getElementById("myDiv").style.opacity=100-(i*10), 100);
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢
我在Android应用程序中有一个简单的for循环,但它似乎并没有在其中执行代码.我已经删除了循环中的代码并用toast方法替换它用于调试目的但它仍然没有执行,我无法解决原因,我的循环的语法必定是错误的,但是eclipse显示没有错误和java编程相当新我无法发现我出错的地方.
代码如下:
public void calcAverage() {
for (int i = 0; i > counter; i++) {
toastMsg = "for loop";
toast();
}
}
Run Code Online (Sandbox Code Playgroud)
calcAverage()
只需按一下按钮即可调用该方法.
第一个代码:工作正常,成功时间为0秒
int main()
{
int n=100000;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
{}
cout<<"ffdfdf";
}
Run Code Online (Sandbox Code Playgroud)
第二个代码:超出时间限制
int main()
{
int n=100000;
bool **a=new bool*[n];
for(int i=0;i<n;i++)
{
bool[i]=new bool[n];
for(int j=0;j<n;j++)
{
bool[i][j]=1;
}
}
cout<<"ffdfdf";
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以解释为什么这两个代码片段有很大的时间差异.我不理解它.
我在Java中使用List集合,我使用迭代集合来处理已创建的整数值列表.
为什么这段代码会无限循环?
int i = 0;
int j = 0;
List<Integer> li = new ArrayList<Integer>();
for (i = 0; i < 10; i++) {
li.add(i);
}
do {
System.out.println(li.get(j));
} while(j < li.size());
Run Code Online (Sandbox Code Playgroud) loops ×13
for-loop ×5
java ×5
python ×3
while-loop ×3
android ×1
arraylist ×1
c ×1
c# ×1
c++ ×1
case ×1
function ×1
if-statement ×1
javascript ×1
math ×1
memory ×1
php ×1
python-2.x ×1
range ×1
timeout ×1