【问题标题】:How do I return an int data from a static void method to main method?如何将 int 数据从静态 void 方法返回到 main 方法?
【发布时间】:2015-10-03 17:07:47
【问题描述】:

所以一切正常,但我唯一的问题是我不确定如何从我的speakLine() 方法返回一个数据。我正在尝试打印我从一组问题中得到的正确答案的数量。

类似这样的事情:你在 3 个中得到了“2”个正确。

Public static void main(String[] args) throws Exception {

    int cal;
    int i;
    for (i = 0; i < 3; i++) {

        int num1 = randomA();

        int num2 = randomB();

        cal = num1 + num2;

        speakLines(num1, num2, cal);
    }

我需要 speakLine 方法()中的“计数”int:

    System.out.println("You got " + count + "of " + i + "right");
} // end main()

这是方法

public static void speakLines(int num1, int num2, int cal) {
    int count = 0;

    Scanner scanner = new Scanner(System.in);

    Voice voice;

    // set up a Voicemanager object and use it to link voice with a                     particular voice
    VoiceManager voiceManager = VoiceManager.getInstance();
    voice = voiceManager.getVoice("kevin16");

    // load the selected voice
    voice.allocate();

    // begin speaking the text
    System.out.println("what is " + num1 + " + " + num2 + ":");
    voice.speak("what is " + num1 + " + " + num2 + ":");

    //talk
    System.out.println("Please enter answer");
    int answer = scanner.nextInt();

    //talk
    if (answer == cal) {
        System.out.println("That's right");
        voice.speak("That's right");
        count += 1;

"Count +=1" above ^ -- 我需要从 main() 方法打印这个 count int。这是我试图返回到主要方法的数据“计数”。这告诉我用户答对了多少。

    } else {
        System.out.println("Sorry, the answer is" + cal);
        voice.speak("Sorry, the answer is " + cal);
    }

} // end speakLines()

public static int randomA() {
    int A; 
    A = 1 + (int) (Math.random() * 10);
    return A;

}

public static int randomB() {
    int B;
    B = 1 + (int) (Math.random() * 5);
    return B;
}
// end randomB()
} // end class 

现在我知道静态 void 方法不返回数据。但我知道应该有一种方法可以返回特定数据。

【问题讨论】:

  • 从外观上看,您可能想阅读this
  • speakLines 必须是static void,还是只是static
  • @frenchDolphin 我想你在问问题,OP 无法回答 :)
  • @turing85 好的,谢谢你的帮助。

标签: java methods int return-value


【解决方案1】:

声明一个类变量public static int count;来替换speakLines方法中的局部变量count

这样做之后,你应该可以在main方法中访问和打印score了。

【讨论】:

  • 是的。这有效,但它为我提供了每个循环的输出。我只想打印 "System.out.println("You got " + count + " of " + index + "right.");"程序结束后一次。有什么想法吗?
  • 我有另一个想法,反映在我编辑的答案中。让我知道它是否有效。
  • “类变量”是指在主方法之外?如果是这样,“不能从静态上下文中引用非静态变量分数”我从“分数 = 计数;”中得到这个错误嗯?
  • 您是否尝试将分数设为静态?
【解决方案2】:

返回码可能不是你想要的。您可能想写出标准输出的答案。这样一些其他程序就可以接收它。 System.out 上有多种方法可以写入标准输出。

如果您想使用 java 程序作为程序链的一部分,您可以使用标准输入和标准输出。

System.out.println 导致程序发送输出。这可以被另一个程序读取。例如,您可以运行它并将其输出重定向到文件:java -jar yourjar &gt; out.txt

您还可以使用管道字符将此输出发送到另一个程序:

java -jar yourJar | grep of 

我不知道你具体想对输出做什么。

【讨论】:

  • 我只想从 main 方法中打印出 speakLine() 方法中的“count”。这就是我所需要的。
  • 你为什么不直接从通话线路调用 System.out.println?您可以在任何程序中的任何位置进行打印。
  • 我只想在程序完成后打印“count”,而不是每个循环。不过我能感觉到你。
【解决方案3】:

您可以在包含类(主函数上方的那个)上使用静态字段,然后在 speakLines 的开头重置它。

通常你不需要 static ,但由于 main 是一个静态方法,它不能访问包含类的非静态字段。

使用将保存 int 值的类参数,并在 speakLines 的开头重置该值。

【讨论】:

  • 我认为这是一个测试问题之类的,因为肯定最好的方法是返回一个值而不是使用 void。
【解决方案4】:

向@DizzyCode 寻求帮助,怎么样-
1.在第2行声明public static int count
2.在第9行初始化变量count
3. 在方法“speakLines”中注释局部变量“count”的初始化

//******************************
public static int count;

Public static void main(String[] args) throws Exception {

    int cal;
    int i;
//set count to 0 before the loop starts
    count=0;
    for (i = 0; i < 3; i++) {

        int num1 = randomA();

        int num2 = randomB();

        cal = num1 + num2;

        speakLines(num1, num2, cal);
    }

        System.out.println("You got " + count + "of " + i + "right");
} // end main()


public static void speakLines(int num1, int num2, int cal) {
//**************************************
//make this count invalid and create a public static count variable before main method
//    int count = 0;

    Scanner scanner = new Scanner(System.in);

    Voice voice;

    // set up a Voicemanager object and use it to link voice with a                     particular voice
    VoiceManager voiceManager = VoiceManager.getInstance();
    voice = voiceManager.getVoice("kevin16");

    // load the selected voice
    voice.allocate();

    // begin speaking the text
    System.out.println("what is " + num1 + " + " + num2 + ":");
    voice.speak("what is " + num1 + " + " + num2 + ":");

    //talk
    System.out.println("Please enter answer");
    int answer = scanner.nextInt();

    //talk
    if (answer == cal) {
        System.out.println("That's right");
        voice.speak("That's right");
        count += 1;} 
        else {
        System.out.println("Sorry, the answer is" + cal);
        voice.speak("Sorry, the answer is " + cal);
    }

} // end speakLines()

public static int randomA() {
    int A; 
    A = 1 + (int) (Math.random() * 10);
    return A;

}

public static int randomB() {
    int B;
    B = 1 + (int) (Math.random() * 5);
    return B;
}
// end randomB()
} // end class 

【讨论】:

  • 是的!我现在看到了。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-10-22
  • 1970-01-01
  • 2011-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-24
相关资源
最近更新 更多