Java 为什么可以';t子方法在主方法中查找符号?
新手进入编码领域,我目前正在做一个制作“约会网站”的小项目。我正在着手完成它,但是我决定编译我的代码,并且出现了一个错误,尽管已经创建了一个对象 仅供参考,我已经创建了一个Java 为什么可以';t子方法在主方法中查找符号?,java,object,methods,Java,Object,Methods,新手进入编码领域,我目前正在做一个制作“约会网站”的小项目。我正在着手完成它,但是我决定编译我的代码,并且出现了一个错误,尽管已经创建了一个对象 仅供参考,我已经创建了一个Practice类,如下所示: public class Practice { String name; short age; int height; public Practice ( String name, short age, int height ) { t
Practice
类,如下所示:
public class Practice {
String name;
short age;
int height;
public Practice ( String name, short age, int height ) {
this.name = name;
this.age = age;
this.height = height;
}
}
它说在我的
kathHeightDiff()
方法中找不到user1.height
和katherine.heigh
t。我有点困惑,因为当我创建Practice user1=new Practice(userName,(short)age,userHeight)
?时,我不是已经创建了它吗。因此,其范围是:主要方法
如果一个变量应该由多个方法使用,则必须将该变量作为参数传递给这些方法调用,或者必须将该局部变量转换为周围类的字段 无关:你应该每5分钟运行一次编译器(任何时候你添加了一些有意义的东西,你认为“现在应该编译了”)。尽可能多地运行编译器,并立即修复所有错误/警告。@GhostCat或,使用IDE并持续编译。dupe解释变量范围是什么,同样适用于方法。你不能在一个方法中声明一个变量(
main
,在这种情况下),然后期望它在其他地方都可以访问。Java中没有所谓的“子方法”。这可能会帮助您理解为什么它不起作用(与副本结合使用)。
import java.util.Scanner;
public class Dating {
public static void kathHeightDiff () {
if ( user1.height > katherine.height )
System.out.println("You are 5 taller");
else
System.out.println("You are 6 taller");
}
public static void main (String[] args) {
System.out.println("Hello kind sir. Are you interested in love? Press 1 for yes, other numbers for no");
int userAnswer = IO.readInt();
if ( userAnswer == 1 ) {
System.out.println("Nice! What is your name?");
String userName = IO.readString();
System.out.println("Cool. What is your age?");
Scanner age = new Scanner(System.in);
short userAge = age.nextShort();
System.out.println("What is your height in inches? 5'0 is 60, 5'6 is 66, 6'0 is 72");
int userHeight = IO.readInt();
Practice priya = new Practice( "Priya", (short)19, 62);
Practice katherine = new Practice ( "Katherine", (short)18, 63);
Practice jed = new Practice ("Jedidiah", (short)20, 67);
Practice yourMom = new Practice ("Your mom", (short)53, 61);
Practice umayma = new Practice ("Umayma", (short)19, 63);
Practice nawal = new Practice ("Nawal" , (short)20, 64);
Practice user1 = new Practice( userName, (short)userAge, userHeight);
System.out.println("Do you have a desired height? If so, press 1");
int hPreference = IO.readInt();
System.out.println();
if ( hPreference == 1 ) {
System.out.println("How tall in inches? 5'0 is 60, 5'6 is 66, 6'0 is 72 ");
int desiredHeight = IO.readInt();
System.out.println();
}
System.out.println("Which band member would you rather date?");
System.out.println();
System.out.println("A for drummer");
System.out.println("B for guitarist");
System.out.println("C for Singer");
System.out.println("D for piano guy");
System.out.println();
char bandMember = IO.readChar();
switch (bandMember) {
case 'A':
System.out.println("You picked A ( Drummer )");
System.out.println("You sure love your full-arms workout");
break;
case 'B':
System.out.println("You picked B ( Guitarist )");
System.out.println("All you need is your fingers");
break;
case 'C':
System.out.println("You picked C ( Singer )");
System.out.println("Maybe you like some mouth action");
break;
case 'D':
System.out.println("You picked D ( Piano Guy )");
System.out.println("Classic, I see");
break;
default:
System.out.println("No choice? I respect your choice");
break;
}
System.out.println();
System.out.println("What ball is life?");
System.out.println();
System.out.println("A for basketball");
System.out.println("B for Soccer");
System.out.println("My balls");
System.out.println("D for Football");
System.out.println();
char ball = IO.readChar();
switch (ball) {
case 'A':
System.out.println("You picked A ( Basketball )");
System.out.println("You're a baller too");
break;
case 'B':
System.out.println("You picked B ( Soccer )");
System.out.println("Messi > Ronaldo");
break;
case 'C':
System.out.println("You picked C ( Human balls )");
System.out.println("Nice");
break;
case 'D':
System.out.println("You picked D ( Football )");
System.out.println("Ehh");
break;
default:
System.out.println("Just pick something");
break;
}
System.out.println();
System.out.println("Vacation?");
System.out.println();
System.out.println("A for London?");
System.out.println("B for Seoul?");
System.out.println("C for Paris?");
System.out.println("D for Cancun?");
char trip = IO.readChar();
System.out.println();
switch (trip) {
case 'A':
System.out.println("You picked A ( London )");
System.out.println("Brexit or no?");
break;
case 'B':
System.out.println("You picked B ( Seoul )");
System.out.println("A complimentary visit to North Korea after");
break;
case 'C':
System.out.println("You picked C ( Paris )");
System.out.println("City of Love, just like my mattress");
break;
case 'D':
System.out.println("You picked D ( Cancun )");
System.out.println("Everyday is Spring Break");
break;
default:
System.out.println("Get out of the house");
}
System.out.println();
if ( bandMember == 'A' && ball == 'A' && trip == 'A' ) {
System.out.println("Your match is " + katherine.name + ". Press 1 for details, other to leave");
int details = IO.readInt();
if ( details == 1 ) {
System.out.println("Her name is " + katherine.name );
System.out.println("Her age is " + katherine.age );
System.out.println("Her height is " + katherine.height + " inches");
System.out.println();
kathHeightDiff();
}
} else {
System.out.println("OK BYE HAVE A WONDERFUL TIME");
}
}
}
}