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

时间:2022-05-18 11:50:42

(Sorry if this is so beginner. I'm in an intro class.) Why is Java telling me to declare and initialize 'piglatin' as a variable in the main method when it's a return value? I thought that once it gets initialized in pigLatinWord, it doesn't need to be reinitialized in main?

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

import java.util.*;

public class PigLatin {

//This program prints our the pig latin version of words.

public static void main(String[] args) {
    Scanner console = new Scanner(System.in);       
    String word;

    System.out.print("Enter a word: ");
    word = console.next();

    pigLatinWord(word);

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

    //translates words starting with vowels, "th," consonants into piglatin
    public static String pigLatinWord (String word) {

    char c = word.charAt(0);
      String piglatin;
    if (c == 'a' ||  c == 'e' || c == 'i' || 
                 c == 'o' || c == 'u'){  //if word starts with a vowel
                 piglatin = word + "-hay";      
              } else if (word.startsWith("th")) {  //if word starts with "th"
                  piglatin = word.substring(2) + "-thay";
              } else {
                 piglatin = word.substring(1) + "-" + word.charAt(0) + "ay";
              }
      return piglatin;
    }
}

3 个解决方案

#1


1  

Presumably when you write

大概是你写的时候

pigLatinWord(word);

You mean to assign the return value of this method to some variable. Specifically, you probably want:

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

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

Only then can you refer to such a variable in your main method.

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

#2


1  

When you are writing System.out.println(word + " in pig latin is " + piglatin); , compiler will expect piglatin variable to be declared inside main method but its not there. Hence its throwing error.

当你写System.out.println(猪拉丁语中的单词+“是+ + piglatin); ,编译器会期望在main方法中声明piglatin变量,但它不在那里。因此它的投掷错误。

You can write :

你可以写 :

String piglatin = pigLatinWord(word); 

#3


1  

You can print your returned pigLatinWord directly like this:

您可以直接打印返回的pigLatinWord,如下所示:

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

Reason behind the error: Scope of the variable 'piglatin' is limited to pigLatinWord (String word) method. Hence you can't use this variable outside of your pigLatinWord() method.

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

If you want to access the value returned by your pigLatinWord() method, you can declare a new variable within main() method to store the returned value and use it to print:

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

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

#1


1  

Presumably when you write

大概是你写的时候

pigLatinWord(word);

You mean to assign the return value of this method to some variable. Specifically, you probably want:

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

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

Only then can you refer to such a variable in your main method.

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

#2


1  

When you are writing System.out.println(word + " in pig latin is " + piglatin); , compiler will expect piglatin variable to be declared inside main method but its not there. Hence its throwing error.

当你写System.out.println(猪拉丁语中的单词+“是+ + piglatin); ,编译器会期望在main方法中声明piglatin变量,但它不在那里。因此它的投掷错误。

You can write :

你可以写 :

String piglatin = pigLatinWord(word); 

#3


1  

You can print your returned pigLatinWord directly like this:

您可以直接打印返回的pigLatinWord,如下所示:

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

Reason behind the error: Scope of the variable 'piglatin' is limited to pigLatinWord (String word) method. Hence you can't use this variable outside of your pigLatinWord() method.

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

If you want to access the value returned by your pigLatinWord() method, you can declare a new variable within main() method to store the returned value and use it to print:

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

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