2017-04-19 54 views
-2

我已經開始在我的空閒時間編寫自己的Java程序,名爲「Book A Ticket Machine」,它是一個沒有GUI的Java控制檯程序。它會詢問您的FullName,FrequentFlyer ID,Age,然後將您與您指定的航空公司和航班號相匹配。當你旅行的時候你的燃料會減少,當它着陸時燃料將會填滿(我將爲此創建一個填充方法)。我在調用範圍之外的方法時遇到問題。JAVA。如何用Java中另一個作用域的變量創建方法

目前我有兩個文件:

  1. Flights.java - >下水文件。與flightUserDatabase關聯。
  2. flightUserDatabase.java - >包含所有的方法和類/藍圖所有的用戶名,年齡,frequentFlyer等從Flights.java

代碼:

import java.io.Console; 

public class Flights { 

    public static void main (String[] args) { 
     Console console = System.console(); 
     //Book a Ticket Machine 
     //From Database otherwise Name not found on Database. Put Database in Another Class. Call it flightUserDatabase. 
     /* firstName: DONE 
     lastName: DONE 
     frequentFlyerID: Otherwise Invalid Number parseInt 
     Age: parseInt 
     FUEL MINUS AND FUEL ADD WHEN LAND. 
     */ 

     flightUserDatabase database = new flightUserDatabase(); 

     System.out.println("Enter Creditials: "); 
     database.getDatabase(); 

     String airline = console.readLine("ENTER YOUR AIRLINE: "); 
     String flightNumber = console.readLine("ENTER YOUR FLIGHT NUMBER: "); 
     String gate = "B7"; /* Declare data type String called "gate" */ 
    //Next Version, Generate Random Number 

     System.out.println("This is an automated system. Please Wait..."); 
     System.out.printf("%s %s is Departuring @ Gate:%s \n", airline, flightNumber, gate); /* Use printf from java.io.Console library, then output Gate and Flight Number */ 



     /* Notes: Data Types 
     > String name = "Ohm"; 
     > int age = 42; 
     > double score = 95.5; 
     > char group = 'F'; 
     */ 
    } 

} 

flightUserDatabase.java代碼:

import java.io.Console; 
    //Book a Ticket Machine 

class flightUserDatabase { 
    Console console = System.console(); 
    public String fullName; 

    public boolean getDatabase() { 
    boolean namesInDatabase; 
    do { 
    fullName = console.readLine("ENTER YOUR FULLNAME: "); 
    namesInDatabase = (fullName.equals("Ohm Passavudh") || fullName.equals("Matt")); 

    if (!namesInDatabase) { 
     console.printf("Sorry, that name is not in our database yet. \n"); 
    } 
    if (namesInDatabase) { 
     console.printf("Welcome, Mr. %s \n", fullName); 
    } 
    } while(!namesInDatabase); 
    return namesInDatabase; 
    } 
    //If Ohm: FFID = 1234569 
    //If Matt: FFID = 246810 
    //FFID == FrequentFlyerID 
    /* Get name from inside scope fullName namesInDatabase variable */ 


     public boolean frequentFlyerID() 


I HAVE PROBLEMS HERE!!! I WANT TO SET Ohm's FFID to 1234569. But how to I determine if the user enters Ohm or Matt. I cannot access the String fullName from the other scope. I hope you understand me. If there is any misunderstanding I can clarify. 
} 
+3

無法讀取的代碼。我建議作爲初學者,你更多地考慮風格,可讀性和分解。對象應該做好一件事。您的FlightDatabase不應執行I/O操作或提示用戶輸入。乘客在哪裏上課?您必須有一個。 – duffymo

回答

0

首先,請在您的格式上進行操作,閱讀該代碼非常糟糕。

您可以在flightUserDatabase中創建一個類字段和一個getter,這樣您可以在確定名稱位於數據庫中後獲取該名稱。

或者你可以用getDatabase返回它()

就像這個...

public String getDatabase() 
{ 
    String fullName; 
    ... 
    return fullName; 
} 

畢竟,你不使用布爾值。

...或...這

class flightUserDatabase 
{ 
    private String fullName = ""; 
    ... 
    public String getName() 
    { 
    return this.fullName; 
    } 
} 
0
  1. 您必須初始化變量public String fullName; - 上格式化代碼>public String fullName = "";
  2. 工作
  3. 名稱類從上符號開始在你的情況FlightUserDatabase
  4. 記住封裝(私有變量)
相關問題