小编ash*_*n33的帖子

覆盖接口中的方法是否有意义

我有一个接口A和B.A有一个叫做foo的(抽象)方法.B延伸A.

即使使用@Override,也可以在接口B中覆盖foo,但是在任何情况下都有意义吗?没有什么可以覆盖,因为这两种方法都必须是抽象的,没有正文.所以我猜没有合理的情况,对吧?

那么为什么可以在界面中覆盖呢?

java extends overriding interface

9
推荐指数
2
解决办法
142
查看次数

递归:如何尝试不同的整数1到9的组合,以及(部分)反向序列在错误的情况下重新开始?

语言: Java

目标: 一般:解决数独游戏

具体:做一个递归方法solve():

  • 检查数字是否与行,列或框中的其他数字冲突
  • 如果不是这样,则在给定的空白空间中填充[1-9]之间的整数并移动到下一个空白空间
  • (部分或全部)如果空间不能被[1-9]之间的整数填充而没有冲突,则会反转进度.然后再试一次,直到填满所有空格(并解决数独).

问题: 循环尝试填充整数n,但总是先尝试最小的数字.如果我使用递归,整数将始终是相同的.

问题: 1.如何使代码填充1到9之间的数字.

  1. 如何使用递归来部分或完全擦除进度并尝试不同的数字.

  2. (额外)我到目前为止构建的代码部分地解决了数独(直到空方块无法填充),但现在它甚至没有填充第一个方块,即使它之前做过.这不是我的主要问题,但如果有人注意到这个问题,我会非常感激,如果有人指出的话.

回顾: 我正在阅读关于递归的课程材料,但无法找到正确的信息.

免责声明: 方法printMatrix之外的所有println命令都用于测试

这是有问题的方法:

       // prints all solutions that are extensions of current grid
      // leaves grid in original state
void solve() {
//local variables
int[] currentSquare;
int currentRow;
int currentColumn;
boolean checkConflict;
currentSquare = new int[2];

//saftey limit for testing
int limit;
limit = 0;

while(findEmptySquare() != null){

  currentSquare = findEmptySquare().clone();
  currentRow = currentSquare[0];
  currentColumn = currentSquare[1];
  //System.out.println(" column c:"+currentColumn+" row r:"+currentRow); …
Run Code Online (Sandbox Code Playgroud)

java recursion

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

JavaScript ES6模板字符串的类型不一致

在测试JavaScript ES6的新模板字符串时(在Firefox中,如果它很重要),我注意到它们的类型有些不一致.

我定义了一个自定义函数,如下所示:

function f(a) {
    console.log(typeof(a));
    console.log(a);
}
Run Code Online (Sandbox Code Playgroud)

首先,我使用模板字符串周围的括号测试了"正常"功能.

f(`Hello, World!`)
Run Code Online (Sandbox Code Playgroud)

正如所料,这产生了一种类型stringHello, World!输出到控制台.

然后我调用函数速记,没有括号,并且发生了不一致.

f`Hello, World!`
Run Code Online (Sandbox Code Playgroud)

类型变为object,并Array [ "Hello, World!" ]输出到控制台.

使用第二种方法时,为什么模板字符串包含在数组中?这只是Firefox中的一个错误(毕竟ES6 一个新标准)还是出于某种原因预期会出现这种情况?

javascript ecmascript-6 template-strings

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

检查EOF和fgetc()错误的更好方法是什么?

我总是使用这种方法

int c;
while ((c = fgetc(fp))!=EOF)
{
    printf("%c", c);
}
Run Code Online (Sandbox Code Playgroud)

因为在我看来,它更具可读性和强大性.但是对于我的链接答案,chux评论说

if(feof(fp))比int c更健壮; while((c = fgetc(fp))!= EOF)

    while(1)
    {
        c = fgetc(fp);
        if ( feof(fp) )
        {
            break ;
        }
        printf("%c", c);
    }
Run Code Online (Sandbox Code Playgroud)

比第一个版本更强大.那么我应该使用什么版本?请解释一下为什么这个版本更好.

编辑

问题为什么"while(!feof(file))"总是错的?有人问为什么控制循环中的feof()总是错误的.但feof()以适当的方式检查条件是否总是错误的?解释很明显.

c fgetc

7
推荐指数
2
解决办法
3738
查看次数

为什么C++需要运算符同义词?

在维基百科上查找C++的运算符列表时,我发现了一篇关于运算符同义词的文章:

C++定义[6]关键字作为许多运算符的别名:和(&&),bitand(&),and_eq(&=),或(||),bitor(|),or_eq(| =),xor (^),xor_eq(^ =),不是(!),not_eq(!=)和compl(〜).这些可以与它们替换的标点符号完全相同的方式使用,因为它们在不同名称下不是相同的运算符,而是相应运算符的名称(字符串)的简单标记替换.这意味着表达式(a> 0和flag)和(a> 0 && flag)具有相同的含义.这也意味着,例如,bitand关键字不仅可以用来替换按位运算符和运算符地址,还可以用来指定引用类型(例如,int bit和ref = n) .ISO C规范允许将这些关键字作为头文件iso646.h中的预处理器宏.为了与C兼容,C++提供了头文件ciso646,其中包含无效.

然后我想知道:为什么我们需要这些operator synonyms?如果有人提供一些用例,那就太好了.

c++ operators

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

Java填充从文件读取的字符串

好吧,我有一个文件,从一个看起来像这样的文件读取整数. 123456-1324563.

该文件将这些数字作为字符串读取,我试图弄清楚如何创建一个方法,将数字附加0到正在读取的数字的哪一侧小于另一侧.

例如,如果运算符左侧的数字少于右侧的数字,则会向字符串添加0,以便两个数字变为偶数并返回新的字符串.所以我需要使用方法将String 123456789-123456转换为123456789-000123456.但它需要确定哪一侧更短并且在它前面填0,但仍然返回整个字符串.

编辑:

这是我使用的这个方法的最新版本,我正在使用,当+传递运算符时,我得到了一个ArrayIndexOutOfBoundsException.但它与-运营商完美匹配.

public String pad(String line, String operator){
    String str[] = line.split(Pattern.quote(operator));

    StringBuilder left = new StringBuilder(str[0]);
    StringBuilder right = new StringBuilder(str[1]);
    left = left.reverse();
    right = right.reverse();
    int len1 = left.length();
    int len2 = right.length();
    if(len1>len2){
        while(len1!=len2){
            right.append("0");
            len1--;
        }
    }else{
        while(len1!=len2){
            left.append("0");
            len2--;
        }
    }
    return left.reverse().toString()+operator+right.reverse().toString();
}
Run Code Online (Sandbox Code Playgroud)

java string

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

这个程序怎么可能有意义?

该程序既不是有效的C程序,也不是无效的C程序.但是,根据我的理解,它甚至不能被定义为处理单元计算的指令序列.

代码是:

That's it. The author alleges:

Some C compilers will compile an empty file into a program that does nothing. But even if your compiler can't, the build instructions supplied with this entry will produce an executable file. On most systems, the stdout from the executable will exactly match original source.

I don't buy it. The compilation will succeed, yielding no instructions. However, the linking will fail as there is no main符号可以调用.如果它可以运行,那么技巧将起作用,因为它不输出任何东西,源代码实际上什么都不包含.

那么,如何(字面上和在哪种环境下)将program其视为有效的自我复制程序?

注意:IOCCC已经接受了它.

c

4
推荐指数
2
解决办法
93
查看次数

如何计算二叉树中的节点总数

我需要计算二叉树中的节点总数.当我执行此代码时,问题就出现了,它为节点总数提供了垃圾值.我的程序输出就像993814.应该是7.

如何解决这个问题?

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

struct binarytree
{
    int data;
    struct binarytree * right, * left;
};

typedef struct binarytree node;

void insert(node ** tree, int val)
{
    node *temp = NULL;
    if(!(*tree))
    {
        temp = (node *)malloc(sizeof(node));
        temp->left = temp->right = NULL;
        temp->data = val;
        *tree = temp;
        return;
    }

    if(val < (*tree)->data)
    {
        insert(&(*tree)->left, val);
    }
    else if(val > (*tree)->data)
    {
        insert(&(*tree)->right, val);
    }

}

void print_preorder(node * tree)
{
    if (tree)
    {
        printf("%d\n",tree->data);
        print_preorder(tree->left); …
Run Code Online (Sandbox Code Playgroud)

c binary-tree visual-studio-2010 nodes

4
推荐指数
2
解决办法
2万
查看次数

在循环内递增变量

我是C语言的新手,已经在这里做了一个小时,但我发现这个代码循环迭代的奇怪行为

int main(int argc, char** argv)
{
    int c;
    int big;
    int small;
    while(c=getchar())
    {
        if(c>='A' && c<='Z');
        big++;
        printf("%d",big);
    }
    printf("end");
}
Run Code Online (Sandbox Code Playgroud)

只要有大写字符,就应该增加int大.但它总是打印像5452这样的大数字等等,我是否想念c语言中的迭代或我的简单代码中有错误?

c

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

Android:如何查找选项卡布局中哪个选项卡处于活动状态?

我目前正在开发一个简单的 Android 应用程序。

\n\n

有一个登录页面,并且可以作为驾驶员或乘客登录,因此该页面中有不同的选项卡。

\n\n

我必须使用相同的“电子邮件”和“密码字段 a,以及相同的登录按钮。基本上唯一改变的是副标题

\n\n

http://i.imgur.com/LhAZABk.png

\n\n

我需要弄清楚按下登录按钮时哪个选项卡处于活动状态,以便使用正确的登录选项并将用户发送到正确的位置。

\n\n

以下是部分代码:

\n\n

登录布局:

\n\n
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"\n    xmlns:tools="http://schemas.android.com/tools"\n    android:layout_width="match_parent"\n    android:layout_height="match_parent"\n    xmlns:app="http://schemas.android.com/apk/res-auto"\n    android:paddingLeft="@dimen/activity_horizontal_margin"\n    android:paddingRight="@dimen/activity_horizontal_margin"\n    android:paddingTop="@dimen/activity_vertical_margin"\n    android:orientation="vertical"\n    android:paddingBottom="@dimen/activity_vertical_margin"\n    tools:context="com.domain.myname.schoolproject.Login_Screen">\n\n    <TextView\n        android:layout_width="wrap_content"\n        android:layout_height="wrap_content"\n        android:textAppearance="?android:attr/textAppearanceLarge"\n        android:text="@string/Nom_Application"\n        android:id="@+id/txtTitle"\n        android:textAlignment="center"\n        android:textColor="#010101"\n        android:textSize="45sp"\n        android:textStyle="bold"\n        android:autoText="false"\n        android:layout_alignParentTop="true"\n        android:layout_centerHorizontal="true" />\n\n    <TextView\n        android:layout_width="wrap_content"\n        android:layout_height="wrap_content"\n        android:textAppearance="?android:attr/textAppearanceMedium"\n        android:text="@string/Connexion"\n        android:id="@+id/txtSubtitle"\n        android:layout_marginTop="20dp"\n        android:textColor="#010101"\n        android:textSize="18sp"\n        android:textIsSelectable="false"\n        android:textStyle="bold"\n        android:layout_below="@+id/txtTitle"\n        android:layout_centerHorizontal="true" />\n\n    <ImageView\n        android:layout_width="120dp"\n        android:layout_height="wrap_content"\n        android:id="@+id/imgLeft"\n        android:src="@drawable/carleft"\n        android:layout_alignParentLeft="true"\n        android:layout_alignParentStart="true"\n        android:layout_marginTop="70dp"\n        android:contentDescription="@string/Description_ic\xc3\xb4ne" />\n\n    <ImageView\n        android:layout_width="120dp"\n        android:layout_height="wrap_content"\n        android:id="@+id/imgRight"\n        android:src="@drawable/carright"\n        android:layout_alignParentRight="true"\n        android:layout_alignParentEnd="true"\n        android:layout_alignBottom="@+id/imgLeft"\n        android:contentDescription="@string/Description_ic\xc3\xb4ne"\n        android:layout_alignParentStart="false" />\n\n    <android.support.v7.widget.Toolbar\n        android:id="@+id/toolbar"\n        android:layout_width="match_parent"\n …
Run Code Online (Sandbox Code Playgroud)

java android android-layout android-fragments

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