2014-02-16 16 views
-5

我必須製作一個代碼,使得星球大戰名稱生成器和它要求的名稱,但只需要從名稱的某些字母拿出星球大戰的名字,我的程序不會編譯任何人都可以看到什麼即時做錯了:星球大戰名字發生器代碼

import java.util.Scanner; 

public class StarWarsName { 
    public static void main(String[] args) { 
     System.out.printf("Enter your first name: "); 
     firstname = input.nextLine(); 
     first = first.substring(0,3); 

     System.out.printf("Enter your last name: "); 
     lastname = input.nextLine(); 
     last = last.substring(0,2); 

     System.out.printf("Enter your mother's maiden name: "); 
     mothersname = input.nextLine(); 
     mother = mother.substring(0,2); 

     System.out.printf("Enter the name of the city in which you were born: "); 
     cityname = input.nextLine(); 
     city = city.substring(0,3); 

     StarWarsName = first +" "+ last +" "+ mother +" "+ city + " of " + last +" "+$ 
     System.out.println("May the force be with you, " + StarWarsName + "May the fo$ 
    } 

} 
+2

編譯錯誤發生在哪裏? – Smutje

+0

什麼是「第一」,什麼是「最後」,什麼是「城市」?你在哪裏實例化這些變量? – GameDroids

+0

最好的問題! +1 – PeeHaa

回答

2

這一行開始:

firstname = input.nextLine(); 

提示:

1)凡firstname聲明?

2)閱讀編譯錯誤信息!

3)當你問別人一個關於編譯錯誤的問題時,你需要說出編譯錯誤是什麼以及它發生了什麼。

並考慮這一行:

StarWarsName = first +" "+ last +" "+ mother +" "+ city + " of " + last +" "+$ 

4)$在到底是不是有效的Java。它看起來像複製和粘貼錯誤。如果Java程序中的某行太長而無法放在您的(預期)顯示設備上,則可以使用的常見做法來分割它;例如

StarWarsName = first +" "+ last +" "+ mother +" "+ city + " of " + 
      last + " " + and + " " + the + " " + rest; 
0

您有幾個未聲明的變量,例如firstname。我認爲也有一些邏輯錯誤,因爲你不使用firstname等等。