小编dje*_*lin的帖子

多态性涉及继承

为什么多态性依赖于继承?代码示例?

polymorphism

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

为android写一个超时事件

我有一个小问题,我无法弄清楚.对于我的程序,我基本上想要执行一些代码,如果用户没有对应用程序做任何事情5分钟(说退出).

我怎么能这样做?我在检测到用户没有做任何事情时迷路了,然后在用户触摸平板电脑或其他东西后重置计数.有人可以给我一些指示吗?

提前致谢!

java android

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

C++ - cout将两个整数的商打印为零

我在一个文件中读到ifstream,并且我想写出cout已经读取了多少文件,以百分比表示.

long length = 0, now = 0;
int i = 0;
double d = 0;

file.seekg( 0, std::ios::end );
length = file.teelg();
file.seekg( 0, std::ios::beg );

while ( std::getline( file, buffer ) ) {
    now = file.teelg;

    i = now / length * 100;
    d = now / length * 100;

    std::cout << length << " "      // working
              << now << " "     // working
              << ( now / length * 100 ) << …
Run Code Online (Sandbox Code Playgroud)

c++ integer numbers cout

0
推荐指数
2
解决办法
501
查看次数

正确使用for循环

在以下代码中,永远不会执行for循环.我试图用断点和手表解决问题.返回密文的正确长度,但for循环直到没有增加int i >= ciphertext.length().事实上,似乎没有任何东西执行过'Debug'消息.

private void decrypt_btnActionPerformed(java.awt.event.ActionEvent evt) {                                            


    String alphabet= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    String ciphertext="fRcXFBxXTRJ";


    status_label.setText( "Decryption has begun" );

    JOptionPane.showMessageDialog(null,"ciphertext-length: " + ciphertext.length() + "\n" + ciphertext,"Debug", JOptionPane.INFORMATION_MESSAGE);

    for (int i = 0; i>=ciphertext.length(); i--){

        System.out.println("inc:" + i);

        String cipher_current_char = getLetterAtIndex(ciphertext, i);
        int pos_char_in_alphabet = getIndexAtLetter(alphabet, cipher_current_char);


        if(pos_char_in_alphabet-2<0){

            plain_ta.setText(getLetterAtIndex(alphabet, (alphabet.length()+(pos_char_in_alphabet -2)+1 ) ));

            status_label.setText( 100/i + "%");



        }else{

            cipher_current_char = getLetterAtIndex(ciphertext, i);

            pos_char_in_alphabet = getIndexAtLetter(alphabet, cipher_current_char);

            plain_ta.setText(getLetterAtIndex(alphabet, (pos_char_in_alphabet-2)));

            status_label.setText( 100/i + "%");

        }
    }
}                          
Run Code Online (Sandbox Code Playgroud)

java string loops for-loop

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

运营商|| 不能应用于bool和int类型的操作数

我刚刚遇到一个奇怪的问题,这让我想知道为什么突然发生这种类型的错误,例如看看这段代码:

    if(age == 0 || age == 47 || age = 99)
    {
        name = "Those are lucky numbers!";
    }
    else
    {
        name = "Try again...";
    }
Run Code Online (Sandbox Code Playgroud)

错误是:错误CS0019:运算符'||' 不能应用于'bool'和'int'类型的操作数.

什么......?呵呵......我的意思是类型'bool'和'int'是我们想要用于这种操作的最常见的类型.他们为什么不工作?

此外,我确信我之前已经完成了数千次,我不明白为什么今天它会发生......或者我可能用Ruby语言做到了这一点.我仍然真的有C#以前的感受.

有人有合理的解释吗?我今天想念的东西?:d

c# logical-operators

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

使用XOR交换两个指针

我有一个关于使用XOR两个交换两个字符串文字的快速问题.

所以我有以下内容:

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


void intSwap(int *a, int *b){
    *a=*a^*b;
    *b=*a^*b;
    *a=*a^*b;
}


void swapString(char **a, char **b){
    char *temp=*a;
    *a=*b;
    *b=temp;
}

void main(){
    char *s= "ha";
    char *t= "Oh";

    printf("%s and %s \n",s,t); // prints ha Oh
    swapString(&s,&t);
    printf("%s and %s \n",s,t); // prints Oh ha


    int a=10;
    int b=5;
    printf("%d %d\n",a,b); //print 10 5
    intSwap(&a,&b);
    printf("%d %d\n",a,b); //print 5 10
}
Run Code Online (Sandbox Code Playgroud)

如您所见,我使用二进制运算XOR来表示intSwap.但是,当我尝试使用swapString做同样的事情时,它不起作用.

我收到错误消息说:二进制操作数无效^(有'char*'和'char*')

你知道如何使用XOR交换两个字符串文字吗?在C中有可能吗?谢谢!

c swap xor

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

为什么我的表名出现了?

我在R中运行了这个:

a <- factor(c("A","A","B","A","B","B","C","A","C"))
Run Code Online (Sandbox Code Playgroud)

然后我做了一张桌子

results <- table(a)
Run Code Online (Sandbox Code Playgroud)

但是当我跑

> attributes(results)
$dim
[1] 3

$dimnames
$dimnames$a
Run Code Online (Sandbox Code Playgroud)

但我很困惑为什么会a出现在我的属性中?我以前用Java编程,我认为变量名不应该出现在你的函数中.

attributes r function

0
推荐指数
2
解决办法
126
查看次数

lisp如何决定是评估列表还是将其用作数据?

如果您编写类似的东西(+ 2 3),则将+其视为函数,如果+没有与之关联的任何函数定义,则会发生错误.

如果你写的东西(let (a b c)),(a b c)读作数据.

lisp如何决定?

lisp elisp

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

为什么funcall会忽略词法范围?

我被这个旧答案中的代码所激怒,解释了为什么需要尖锐的引用,但我不明白为什么funcall似乎跳过了通常的范围规则.

(defun test () 'red)

(flet ((test () 'green))
  (list (funcall 'test)
        (funcall #'test))) => (red green)
Run Code Online (Sandbox Code Playgroud)

我是否应该将Common Lisp理解为具有由let-family函数设置的"本地"词法范围符号绑定和由de-family函数设置的全局范围变量符号绑定?

lisp common-lisp

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

在Matlab中为矩阵中的每个值添加一个

说我想要

1 2 3 
4 5 6
7 8 9
Run Code Online (Sandbox Code Playgroud)

成为

2 3 4
5 6 7
8 9 10
Run Code Online (Sandbox Code Playgroud)

说第一个矩阵是mat.我认为 mat.+1会工作,但这给了

意外的MATLAB运算符.

有没有办法做到这一点?

matlab matrix addition

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

可以使用Guava Lists.transform或Iterables.transform转换器吗?

通常Guava Lists.transform看起来像这样:

Lists.transform(new ArrayList<String>("1", "2", "3"),
    new Function<String, Integer>() {
      @Override
      Integer apply(String str) {
        return Integer.valueOf(str);
      }
    });
Run Code Online (Sandbox Code Playgroud)

一个Converter<A, B>定义doForward,并doBackward从去的方法ABBA分别.逻辑上它是一对Function.看起来没有任何简单的方法

  • 从a ConverterFunction描述它的那对,或者
  • 输入a Converter并向方法说出方向Lists.

这些是不兼容的还是除了手动编写胶水代码之外还有其他方法吗?

手动胶水代码看起来像

final Converter<A, B> myConverter = /*...*/;
Lists.transform(myList, new Function<A, B>() {
  @Override
  B apply(A a) {
    return myConverter.doForward(a);
  }
});
Run Code Online (Sandbox Code Playgroud)

java functional-programming guava

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

不正确地使用SetPrecision

使用以下2个函数关注具有*注释的行.当cout <<语句执行时,没有显示小数位.3021680380/10000000变为302.而不是302.1680.

void convert(){
    setprecision(4);   //************************
    newFileTime = new double[numRec];   //***********
    newOffset = new int[numRec];
    newSize = new int[numRec];
    newNum = new int[numRec];
    newType = new int[numRec];
    newTime = new int[numRec];

    for(int i = 0; i<numRec; i++){
        newFileTime[i] = fileTime[i] / 10000000; //**********
        newOffset[i] = offset[i] / 512;
        newSize[i] = fileSize[i] / 512;
        newNum[i] = 0;
        if(type[i] == "Write"){
            newType[i] = 0;
        }else{
            newType[i] = 1;
        }
        newTime[i] = responseTime[i] / 10000000;
    }
}

void exports(){
    setprecision(4);  //**************
    ofstream fout; …
Run Code Online (Sandbox Code Playgroud)

c++ precision double-precision

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

异或两个字符串输出空字符串

违规代码

string foo(string a, string k) {
   string output;

   for (int i = 0; i < a.size(); i++) {
      output[i] = a[i] ^ k[i]; 
   }

   return output;
}
Run Code Online (Sandbox Code Playgroud)

c++ string char

-3
推荐指数
1
解决办法
64
查看次数