小编aja*_*jay的帖子

请解释以下C程序中的嵌套switch-case语句和相应的输出

在编译和运行下面的代码时,我们得到了所述的输出.请解释输出.case 2是嵌套的,case 0所以程序根本不应该打印任何东西.

#include <stdio.h>

int main() {
    int i=5;
    switch ( 2 ) {
        case 0:
            for (  i=0; i<10; i++ ) {
        case 1:
            printf("A i=%d\n",i);
        case 2:
            printf("B i*i=%d\n",i*i);
            };
        case 3:
            printf("done");
            break;
    }

    return 0;
}

/* OUTPUT
B i*i=25
A i=6
B i*i=36
A i=7
B i*i=49
A i=8
B i*i=64
A i=9
B i*i=81
done
*/
Run Code Online (Sandbox Code Playgroud)

c

1
推荐指数
1
解决办法
2373
查看次数

iostream错误地打印椭圆

我有一个简单的示例代码

#include <string>
#include <stdio.h>
#include <iostream>

int main ()
{
    std::cout << "Connecting to hello world server…" << std::endl;
    printf ("Connecting to hello world server...\n");

    while(true)
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

在控制台窗口中,第一行将椭圆打印为"a"字符,其上方有一个波浪号,第二行按预期打印.

有人可以解释为什么会这样吗?

c++ stdout visual-studio-2010

1
推荐指数
1
解决办法
143
查看次数

迭代Iterator <T>类型的对象

在阅读维基百科的文章时 Generators,我发现以下Java实现迭代泛型类型会Iterator<Integer>产生无限的斐波纳契数列

Iterator<Integer> fibo = new Iterator<Integer>() {
    int a = 1;
    int b = 1;
    int total;

    @Override
    public boolean hasNext() {
        return true;
    }

    @Override
    public Integer next() {
        total = a + b;
        a = b;
        b = total;
        return total;
    }

    @Override
    public void remove() {
        throw new UnsupportedOperationException();
    }
}
// this could then be used as...
for(int f: fibo) {
    System.out.println("next Fibonacci number is " + f);
    if (someCondition(f)) break;
} …
Run Code Online (Sandbox Code Playgroud)

java iteration iterator

1
推荐指数
1
解决办法
86
查看次数

类变量是实例方法还是类方法的范围?

  • 类变量是否属于为Python类定义的方法的范围?
  • 如何在方法(类方法或实例方法或静态方法)中访问类变量并可能更改它?
  • 什么是最好的方式?例如,我可能想要计算已经创建了多少个类的实例.

在这里读到类变量在方法参数列表的范围内.要在那里使用类变量,我应该引用它counter而不是MyClass.counter因为类正处于定义的中间,但是我可以像MyClass.counter__init__方法中那样访问它.是否意味着在__init__方法内部完全定义了类?

编辑:我也想知道为什么counter在方法的参数列表范围内但不在方法体中?

class MyClass(object):
    counter = 0

    def __init__(self): # counter is in scope here. can be used as default argument
        counter += 1  # Counter not in scope. Throws UnboundLocalError

    def printCount(self):
        print counter  # Counter not in scope. Throws UnboundLocalError
Run Code Online (Sandbox Code Playgroud)

python methods scope class

0
推荐指数
1
解决办法
168
查看次数

带有2D数组的Java程序

我正在写一个程序,决定数字X是否在数字的输入nxn矩阵的矩阵中,该矩阵已经存储在2D数组的存储器中,并且每个单独的行从左到右增加; 从上到下的列.我输入矩阵n的大小,内容和要搜索的数字X; 我还在屏幕上显示我的搜索结果.在测试我的程序时,输入按2D数组排序.

这是我的代码,它显示了以下编译错误

import java.util.*;

public class matrix
{
    public static void main (String[] args){

        int search(int mat[4][4]; int n; int x)
        {
            int i = 0, j = n-1;  //set indexes for top right element
            while ( i < n && j >= 0 )
            {
                if ( mat[i][j] == x )
                {
                    printf("\n Found at %d, %d", i, j);
                    return 1;
                }
                if ( mat[i][j] > x )
                    j--;
                else //  if mat[i][j] < x
                    i++;
            } …
Run Code Online (Sandbox Code Playgroud)

java arrays

0
推荐指数
1
解决办法
216
查看次数

如何将字符串分配给函数C++

所以使用一堆if语句,我认为有人可以用更好的方式帮助我.

这就是我想要做的.假设我有3个字符串,如果找到该字符串,我想为该字符串分配一个函数...

目前我正在做的一个基本的例子:

if(findStr(string1)) {
    function1(perams);
}
else if (findStr(string2)) {
    function2(perams);
}
else if (findStr(string3)) {
    function3(perams);
}
Run Code Online (Sandbox Code Playgroud)

我正在做更大规模的类似事情,10个不同的字符串,每个字符串对应它自己的功能.

我愿意接受可能涉及我的字符串结构的选项吗?

我想只做一个if语句,即使涉及一个循环.我不想要调用"function1","function2","function3"我希望它以某种方式与字符串相关联.

这可以干净利落吗?或者if语句是最干净的方法吗?

多谢你们

c++ string if-statement function

0
推荐指数
1
解决办法
130
查看次数