對於我們java類中的最終項目,我們可以做任何事情。我正在做一個臉部創作者,我想知道我將如何製作它,所以來自Questions類的字符串可以移動到繪畫類。這是這樣我可以做,所以我可以做如果string ...然後繪製...如何將變量從類移動到類
public class FinalProjectQuestions
{
public static void main(String []args)
{
Scanner in = new Scanner(System.in);
System.out.println("We will design the stick face of your dreams");
System.out.print("Choose gender M for Male, or F for Female: ");
String gender = in.next();
String hair = "";
String haircolor = "";
String eyecolor = "";
String eyesize = "";
String mouthsize = "";
String m = "m";
String m2 = "M";
String femalehair = "";
String femalehaircolor = "";
String femaleeyecolor = "";
String femaleeyesize = "";
String femalemouthsize = "";
String femalemakeup = "";
if (gender.equals(m) || gender == "M")
{
System.out.print("Hair Length, Short, Mediuem, or Long: ");
hair = in.next();
System.out.print("Hair Color, Red, Brown, Blond, Blue, Black, or Purple: ");
haircolor = in.next();
System.out.print("Eye Color, Blue, Red, Brown, or Black: ");
eyecolor = in.next();
System.out.print("Eye size, Big, Small, or Mediuem: ");
eyesize = in.next();
System.out.print("Mouth Size, Big, Small, or Mediem: ");
mouthsize = in.next();
}
else
{
System.out.print("Hair Length, Short, Mediuem, or Long: ");
femalehair = in.next();
System.out.print("Hair Color, Red, Brown, Blond, Blue, Black, or Purple: ");
femalehaircolor = in.next();
System.out.print("Eye Color, Blue, Red, Brown, or Black: ");
femaleeyecolor = in.next();
System.out.print("Eye size, Big, Small, or Mediuem: ");
femaleeyesize = in.next();
System.out.print("Mouth Size, Big, Small, or Mediem: ");
femalemouthsize = in.next();
System.out.print("MakeUp, None, Little, Average, or Tons: ");
femalemakeup = in.next();
}
}
}
public class FinalProjectMale
{
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
Ellipse2D.Double faceshape = new Ellipse2D.Double(100,100,100,100);
g2.draw(faceshape);
}
}
哪裏是你的畫類? – SMA
實際上,最好的方法是創建一個通用的Person類,並將所有變量放入其中,然後爲所有變量創建set方法。在此之後,您可以創建兩個課程「男」和「女」,並從「人」類延伸。 – Laerte
不要在'gender ==「處用'=='做字符串比較M」'您可以在那個if語句中使用'gender.equalsIgnoreCase(「m」)''。 –