如果它是Java中的返回值,为什么我需要在main中声明一个变量?

时间:2015-10-12 05:26:31

标签: java variables return-value variable-declaration

(对不起,如果这是初学者。我参加了一个介绍课程。)为什么Java告诉我申报和初始化' piglatin'当它是一个返回值时,作为主方法的变量?我认为一旦它在pigLatinWord中初始化,它不需要在main中重新初始化?

def heatmap_seaborn():
    na_lr_measures = [50, 50, 50, 49, 49, 49, 48, 47, 47, 47, 46, 46, 46, 46, 45, 45, 45, 45, 45, 45, 45, 45, 45, 43, 43, 43, 43, 42, 42, 42, 41, 41, 41, 41, 41, 41, 41, 40, 40, 40, 40, 40, 40, 40, 40, 39, 39, 37, 37, 36, 36, 36, 36, 35, 35, 35, 35, 35, 34, 34, 34, 33, 33, 33, 32, 32, 31, 30, 30, 30, 29, 29]
    na_lr_labels = ('bi2e', 'bi21', 'bi22', 'si21', 'si22', 'si2e', 'si11', 'bi11', 'bi1e', 'si1e', 'bx21', 'ti22', 'bx2e', 'si12', 'ti1e', 'sx22', 'ti21', 'bx22', 'sx2e', 'bi12', 'ti11', 'sx21', 'ti2e', 'ti12', 'sx11', 'sx1e', 'bxx2', 'bx1e', 'bx11', 'tx2e', 'tx22', 'tx21', 'sx12', 'six1', 'six2', 'sixe', 'sixx', 'tx11', 'bx12', 'bix2', 'bix1', 'tx1e', 'bixe', 'bixx', 'bxxe', 'sxx2', 'tx12', 'tixe', 'tix1', 'sxxe', 'sxx1', 'si1x', 'tixx', 'bxx1', 'tix2', 'bi2x', 'sxxx', 'si2x', 'txx1', 'bxxx', 'txxe', 'ti2x', 'sx2x', 'bx2x', 'txxx', 'bi1x', 'tx1x', 'sx1x', 'tx2x', 'txx2', 'bx1x', 'ti1x')
    na_lr_labelcategories = ["TF", "IDF", "Normalisation", "Regularisation", "Acc@161"]


    measures = na_lr_measures
    labels = na_lr_labels
    cats = na_lr_labelcategories


    new_measures = defaultdict(list)
    new_labels = []
    #cats = ["TF", "Normalisation", "Acc@161"]
    for i,c in enumerate(labels):
        c=c[0]+c[2]
        new_labels.append(c)
        m = measures[i]
        new_measures[c].append(m)
    labels = list(set(new_labels))
    measures = []
    for l in labels:
        m = np.mean(new_measures[l])
        measures.append(m)





    df = pd.DataFrame(
                  {cats[0]:pd.Categorical([a[0] for a in labels]), 
                   #cats[1]:pd.Categorical([a[1] for a in labels]), 
                   cats[2]:pd.Categorical([a[1] for a in labels]), 
                   #cats[3]:pd.Categorical([a[3] for a in labels]), 
                   cats[4]:measures})
    print df


    df = df.pivot(cats[0], cats[2], cats[4])
    sns.set_context("paper",font_scale=2.7)
    fig, ax = plt.subplots()
    ax = sns.heatmap(df)
    plt.show()

3 个答案:

答案 0 :(得分:1)

大概在你写的时候

pigLatinWord(word);

您的意思是将此方法的返回值分配给某个变量。具体来说,您可能想要:

String piglatin = pigLatinWord(word); //Creates a String variable reference to the return value of this method

只有这样才能在main方法中引用这样的变量。

答案 1 :(得分:1)

当你编写System.out.println(word + " in pig latin is " + piglatin);时,编译器会期望在main方法中声明piglatin变量,但它不在那里。因此它的投掷错误。

你可以写:

String piglatin = pigLatinWord(word); 

答案 2 :(得分:1)

您可以直接打印退回的pigLatinWord:

System.out.println(word + " in pig latin is " + pigLatinWord(word));

错误背后的原因:   变量范围' piglatin'仅限于pigLatinWord(String word)方法。因此,您不能在pigLatinWord()方法之外使用此变量。

如果要访问pigLatinWord()方法返回的值,可以在main()方法中声明一个新变量来存储返回的值并使用它来打印:

String translatedPigLatinWord = pigLatinWord(word);
System.out.println(word + " in pig latin is " + translatedPigLatinWord);