小编bla*_*sel的帖子

"M"去了哪里?

我在String Buffer中玩了一下,注意到混合字符和字符串是一个坏主意.我希望我的下面的代码可以打印"Main",但是只是得到了一个"ain".

很明显,word是使用String Buffer构造函数的char版本初始化的,但是我测试了几个方法,比如toString或getIndex(),但是在"ain"旁边找不到任何东西 - 这让我想知道:构造函数做了什么?它有用吗?可以通过单词再次检索"M"吗?

import java.util.Random;

public class OrNotPublicClass {
    private static Random rnd = new Random();

    public static void main(String[] args) {
        StringBuffer word = null;
        switch (rnd.nextInt(2)) {
        case 1:
            word = new StringBuffer('P');
        case 2:
            word = new StringBuffer('G');
        default:
            word = new StringBuffer('M');
        }
        word.append("ain");
        System.out.println(word);
    }
}
Run Code Online (Sandbox Code Playgroud)

java

3
推荐指数
2
解决办法
231
查看次数

我不明白指针,地址和范围

以下是我编写的代码片段,目前正在努力解决我的打印问题.

在我的main方法中,我insertPoint使用有效输入调用该函数两次,例如:insertPoint(42); insertPoint(56);并获得以下输出:

A.1 42
B.3 2686700
Run Code Online (Sandbox Code Playgroud)

但是在B.3,我希望它也能返回值42,但事实并非如此.我假设2686700指的是内存中的某个地址.

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

struct point {
    int value;
    struct point* next;
};

struct point *root;

int insertPoint(int value) {            
    // Graph is empty, set new root 
    if(root == 0){
        struct point newRoot;
        newRoot.value = value;

        root = &newRoot;
        (*root).value = value;
        printf("A.1 %d \n", (*root).value); // "A.1 42"
        return value;
        }
    printf("B.3 %d \n", (*root).value); // "B.3 2686700"

    /* rest of code here; irrelevant since related variables …
Run Code Online (Sandbox Code Playgroud)

c pointers

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

标签 统计

c ×1

java ×1

pointers ×1