新手开始编写代码,目前正在从事制作“约会网站”的小型项目。我正在完成它,但是我决定编译我的代码,尽管已经创建了一个对象,但还是出现了一个错误?
仅供参考,我已经创建了一个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;
}
}
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");
}
}
}
}
它表示无法在我的user1.height
方法中找到katherine.heigh
和kathHeightDiff()
t。我有些困惑,因为我Practice user1 = new Practice(userName, (short)age, userHeight)
做完了还没有创建吗?
答案 0 :(得分:0)
您在main()方法内创建了一个局部变量。因此其范围是:主要方法。
如果一个变量应该被多个方法使用,则您必须将该变量作为这些方法调用的参数传递,或者必须将该 local 变量转换为周围的阶级。