编译器说无法找到符号

时间:2016-07-06 07:37:40

标签: java arrays

编译器抛出异常说:

  

找不到符号p和x

该程序根据循环条件获取用户输入和打印。编译时会抛出一个无法找到符号的错误。

import java.util.Scanner;
class Puppy{
    String name;
    String breed;
    String gender;
    int weight;
    int age;
    int loud;

    void hammer()
    {
        System.out.println("Enter your Dogs name :");
        p[x].name = user_input.next();
        System.out.println("Enter your Dogs Breed :");
        p[x].breed = user_input.next();
        System.out.println("Enter your Dogs Gender :");
        p[x].gender = user_input.next();
        System.out.println("Enter your Dogs weight in Kg:");
        p[x].weight = user_input.nextInt();
        System.out.println("Enter your Dogs age :");
        p[x].age = user_input.nextInt();
        System.out.println("Rate your Dogs Loudness out of 10 :");
        p[x].loud = user_input.nextInt();       
    }

    void jammer()
    {
        System.out.println("Hello my name is" + " " + p[x].name);
        System.out.println("I am a" + " " + p[x].breed);
        System.out.println("I am" + " " + p[x].age + " " + "years old" + " " + p[x].gender);
        System.out.println("I weigh around" + " " + p[x].weight + " " + "kg's");
        System.out.println("I sound this loud" + " " + p[x].loud);  
    }
}

class PuppyTestDrive
{
    public static void main(String []args)
    {
        Scanner user_input = new Scanner(System.in);
        int x = 0;
        Puppy[] p = new Puppy[4];

        while(x < 4)
        {
            p[x] = new Puppy();
            p[x].hammer();
            p[x].jammer();
            x = x + 1;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您的程序应使用以下代码:

import java.util.Scanner; 

class Puppy{

    String name;
    String breed;
    String gender;
    int weight;
    int age;
    int loud;

    void hammer()
    {
        Scanner user_input = new Scanner(System.in);
        System.out.println("Enter your Dogs name :");
        this.name = user_input.next();
        System.out.println("Enter your Dogs Breed :");
        this.breed = user_input.next();
        System.out.println("Enter your Dogs Gender :");
        this.gender = user_input.next();
        System.out.println("Enter your Dogs weight in Kg:");
        this.weight = user_input.nextInt();
        System.out.println("Enter your Dogs age :");
        this.age = user_input.nextInt();
        System.out.println("Rate your Dogs Loudness out of 10 :");
        this.loud = user_input.nextInt();       
    }

    void jammer()
    {
        System.out.println("Hello my name is" + " " + this.name);
        System.out.println("I am a" + " " + this.breed);
        System.out.println("I am" + " " + this.age + " " + "years old" + " " + this.gender);
        System.out.println("I weigh around" + " " + this.weight + " " + "kg's");
        System.out.println("I sound this loud" + " " + this.loud);  
    }
}

class PuppyTestDrive {

    public static void main(String []args)
    {                
        int x = 0;
        Puppy[] p = new Puppy[4];

        while(x < 4)
        {
            p[x] = new Puppy();
            p[x].hammer();
            p[x].jammer();
            x = x + 1;
        }
    }
}