代码之家  ›  专栏  ›  技术社区  ›  Nash Read

使用变量调用访问器

  •  0
  • Nash Read  · 技术社区  · 9 年前

    我通过做一些基本练习来练习我的Java技能,让自己去尝试,我想你可以说,我正在尝试做一个愚蠢的简单“基于文本的比赛游戏”。唯一的逻辑是,无论哪辆车更强大,都会获胜,而我在这里试图学习的要点是OOP。我有4个其他类(Supra、RX7、MX5和S2000)并调用它们。在我的方法“race”中,我将用户选择的汽车设置为一个名为“userCar”的变量。我正在尝试调用userCar上的getHorsepower()访问器,以便计算谁会获胜。我的意图是,如果“userCar”是“supra”,那么“userCar.getHorsepower()”将与“supra.getHorse()”相同。但是,我收到一个错误,说它无法解析if语句中的getHorsepower方法。我相信这是因为我正在尝试调用varable上的访问者,如果是这样,我可以做什么来解决这个问题?或者这可能是一个不同的问题。

    非常感谢。 纳什

    精确错误: 错误:(100,20)java:找不到符号 符号:方法getHorsepower() 位置:java.lang.String类型的变量userCar

      import java.util.Scanner;
    
    public class Cars{
    
        private static Scanner in = new Scanner(System.in);
    
        private int horsepower;
        private int year;
        private String coo;
    
        public static void main(String args[])
        {
            Supra supra = new Supra();
            RX7 rx7 = new RX7();
            MX5 mx5 = new MX5();
            S2000 s2000 = new S2000();
    
            supra.specs();
            rx7.specs();
            mx5.specs();
            s2000.specs();
    
            System.out.println("Supra: " + supra.getHorsepower() + "HP");
            System.out.println("RX7: " + rx7.getHorsepower() + "HP");
            System.out.println("S2000: " + s2000.getHorsepower() + "HP");
            System.out.println("MX5 (Miata): " + mx5.getHorsepower() + "HP");
    
            race();
        }
    
        public void setHorsepower(int h)
        {
            this.horsepower = h;
        }
        public int getHorsepower()
        {
            return horsepower;
        }
    
    
        public static void race()
        {
            Supra supra = new Supra();
            RX7 rx7 = new RX7();
            MX5 mx5 = new MX5();
            S2000 s2000 = new S2000();
    
            supra.specs();
            rx7.specs();
            mx5.specs();
            s2000.specs();
    
            String userCar = "";
            String compCar = "";
    
            System.out.println("Choose a car to race");
            System.out.println("1) Supra | 2) RX7 | 3) S2000 | 4) MX5 (Miata)");
            int userChoice = in.nextInt();
            System.out.println("Choose a car to race against");
            int compChoice = in.nextInt();
    
            if(userChoice == 1)
                userCar = "supra";
            else if(userChoice == 2)
                userCar = "rx7";
            else if(userChoice == 3)
                userCar = "s2000";
            else if(userChoice == 4)
                userCar = "mx5";
    
            if(compChoice == 1)
                compCar = "supra";
            else if(compChoice == 2)
                compCar = "rx7";
            else if(compChoice == 3)
                compCar = "s2000";
            else if(compChoice == 4)
                compCar = "mx5";
    
            if (userCar.getHorsepower() > compCar.getHorsepower())
            {
                System.out.println("You won!");
            } else { System.out.println("That's embarrassing."); }
    
        }
    
    }
    
    2 回复  |  直到 9 年前
        1
  •  2
  •   Stack Exchange Supports Israel    9 年前

    无法使用字符串作为变量的名称,句号。

    但你可以这样做:

        Car userCar = null;
    
        if(userChoice == 1)
            userCar = supra;
        else if(userChoice == 2)
            userCar = rx7;
        else if(userChoice == 3)
            userCar = s2000;
        else if(userChoice == 4)
            userCar = mx5;
    

    同样 compCar .

        2
  •  2
  •   Kon    9 年前

    您正在尝试调用 Car 的实例上的类 java.lang.String 你的 userCar 变量是字符串。它应该是一个 汽车 汽车 子类。