nycbdwss 发表于 2018-3-14 16:09

java自增自减运算符神坑笔试题

本帖最后由 nycbdwss 于 2018-3-14 16:18 编辑

问:下面程序的运行结果是什么?
int count = 0;
for(int i=0;i<100;i++){
   count = count++;
}
System.out.println("count="+count);
答:运行结果是 count = 0。

首先 count++ 是一个有返回值的表达式,返回值是 count 自加前的值,Java 对自加处理的流程是先把 count 的值(不是引用)拷贝到一个临时变量区,然后对 count 变量加1,接着返回临时变量区的值。
所以上面代码块中第一次循环的执行步骤是 JVM 把 count 值(0)拷贝到临时变量区,然后 count 值加 1,这时 count 的值是 1,接着返回临时变量区的值(值是 0),最后返回值赋值给 count,此时 count 值被重置成 0;所以上面代码语句 count = count++; 可以按照如下代码来理解:int autoAdd(int count) {
int temp = count;
count = count +1;
return temp;
}
所以第一次循环后 count 的值还是 0,其他 99 次的循环也是一样的,最终导致 count 的值始终没有改变,仍然保持着最初的状态;如果想要打印结果为 100 则需要修改 count = count++; 语句为 count++; 即可。因此对于 ++/-- 运算在 java 中一定要警惕这个陷阱(-- 运算符也一样存在这个问题),不过这个问题在不同的语言环境中的实现是不同的,在 C++ 中 count = count++; 与 count++ 是等效的,而在 java 等语言中 count = count++; 与 count++ 是不等效的,区别如这道题。

Leslie-Cheung 发表于 2018-3-15 14:54

本帖最后由 Leslie-Cheung 于 2018-3-15 17:16 编辑

羊毛丶 发表于 2018-3-14 21:49
为什么这样就不行呢,想用指针,第二个元素就读不进去了


前面要加&啊
#include <stdio.h>
#define N 3
struct std
{
    intnum;
    char name;
    char sex;
    intage;
};
int main()
{
    void input(struct std *p,int n);
      
    struct std *p ;
    inputa(p,N);
   
    int i;
    for(i = 0;i < N;i++)
      printf("\n%d %s %c %d",(p+i)->num,(p+i)->name,(p+i)->sex,(p+i)->age);
    return 0;
}

void input(struct std *p,int n)
{
    int i;
    printf("请输入学生信息:\n学号姓名性别年龄 \n");
    for(i=0;i<n;i++)
      scanf("%d%s %c%d",&(p+i)->num,(p+i)->name ,&(p+i)->sex ,&(p+i)->age);
}

Leslie-Cheung 发表于 2018-3-14 17:46

羊毛丶 发表于 2018-3-14 16:25
借楼 问问怎么C
怎么吧几组数据输入到结构体数组中,
要求是输入函数


这样应该是可以的:
#include <stdio.h>
#include <stdlib.h>
#define N 3
struct std
{
    intnum;
    char name;
    char sex;
    intage;
};
int main()
{
    void input(struct std p[],int n);

    struct std stu;
    input(stu,N);
   
    for(int i = 0;i < N;i++){
            printf("%d %s %c %d\n",stu.num,stu.name,stu.sex,stu.age);
    }
    return 0;
}

void input(struct std p[],int n)
{
    printf("请输入学生信息:\n学号姓名性别年龄 \n");
        for(int i = 0;i < n;i++)
      scanf("%d%s %c%d",&p.num,p.name,&p.sex,&p.age);
}

qwedc911 发表于 2018-3-14 16:16

不知道是题目本身就是这么写的还是楼主写错了
神他喵每行一个单词

nycbdwss 发表于 2018-3-14 16:17

qwedc911 发表于 2018-3-14 16:16
不知道是题目本身就是这么写的还是楼主写错了
神他喵每行一个单词

哈哈乱了 现在编辑好了

羊毛丶 发表于 2018-3-14 16:25

    借楼 问问怎么C
怎么吧几组数据输入到结构体数组中,
要求是输入函数
#include <stdio.h>
#define N 3
struct std
{
        intnum;
        char name;
        char sex;
        intage;
};
int main()
{
        void input(struct std p[],int n);

        struct std stu;
        input(stu,N);
        return 0;
}
void input(struct std p[],int n)
{
        int i;
        printf("请输入学生信息:\n学号姓名性别年龄 \n");
//        for(i=0;i<n;i++)
                scanf("%d%s%c%d",????);
}

Palantir 发表于 2018-3-14 16:26

java的一个深坑

nddb 发表于 2018-3-14 17:12

问题应该是出现在++上面!一般来如果是 变量=变量++的话是先赋值在最++的!你如果吧++改成+1看看,或者把count++改成++count试试!

空气中我最帅 发表于 2018-3-14 17:39

先赋值,在加加

Leslie-Cheung 发表于 2018-3-14 17:43

学Java这么久了,我竟然都没注意过这些问题,感觉白学了。

羊毛丶 发表于 2018-3-14 21:49

Leslie-Cheung 发表于 2018-3-14 17:46
这样应该是可以的:
#include
#include


为什么这样就不行呢,想用指针,第二个元素就读不进去了

void input(struct std *p,int n)
{
        int i;
        printf("请输入学生信息:\n学号姓名性别年龄 \n");
        for(i=0;i<n;i++)
                scanf("%d %s %c %d",(p+i)->num,(p+i)->name ,(p+i)->sex ,(p+i)->age );
}
页: [1] 2
查看完整版本: java自增自减运算符神坑笔试题