gpt4 book ai didi

java - "cannot find symbol: variable input"错误

转载 作者:行者123 更新时间:2023-12-02 08:15:31 27 4
gpt4 key购买 nike

这是我遇到问题的代码:

import java.util.*;
public class Game {
public static final int ROCK = 1;
public static final int PAPER = 2;
public static final int SCISSORS = 3;

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String name = intro();
int numRounds = numRounds(name);
game(name, numRounds);
}

public static String intro() {
System.out.println("Welcome to Rock Paper Scissors. I, the computer, will be your opponent.");
System.out.print("Please type in your name and press return: ");
String name = input.nextLine();
return name;
}

public static int numRounds(String name) {
System.out.println("Welcome " + name + ".");
System.out.println("All right " + name + ". How many games would you like to play?");
System.out.print("Enter the number of rounds you want to play and press return: ");
int numRounds = input.nextInt();
return numRounds;
}

当我使用扫描仪获取用户姓名和他们想要玩的轮数的值时,我收到错误。我只想返回这些值以在主函数中使用。

如有任何帮助,我们将不胜感激。

最佳答案

在您想要的两个方法中没有 input 变量 - 将其作为方法参数传递:

public static String intro(Scanner input) { .. }
public static int numRounds(String name, Scanner input) { .. }

...

String name = intro(input);
int numRounds = numRounds(name, input);

除此之外,没有 game() 方法 - 定义它。

关于java - "cannot find symbol: variable input"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6526022/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com