Java 编译器说找不到符号
编译器抛出一个异常,说明: 找不到符号p和x 该程序接受用户输入,并根据循环条件打印。编译时,它会抛出一个找不到符号的错误Java 编译器说找不到符号,java,arrays,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(
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;
}
}
}
您的程序应使用以下代码:
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;
}
}
}
您试图在函数hammer中使用变量“p”,但它在main方法中是堆栈变量。您需要将其作为类的属性。hammer和jammer方法不知道p和XY,您使用局部变量作为全局变量。您需要将P和X声明为全局变量,或者在hammer中声明为局部变量。提示:不要这样做:不要编写100行代码来运行编译器。只需写一些你认为应该编译的小部分。然后运行编译器。然后仔细阅读它的错误消息。Java编译器消息非常好,大多数情况下,它们准确地告诉您发生了什么。然后修复错误,再编写几行代码。提示:你的名字真的很糟糕。变量的名称应该告诉您变量是什么。p和x。。。什么也别说。方法的名称也一样。别开玩笑了。没有人喜欢写得不好的代码。只是一个细节:你可以用一些文本替换+一些文本+,甚至可以省略这个。。