我正在嘗試爲自己編寫一個程序,該程序將允許我排序公司的ArrayLists信息。如何在運行程序時通過命令行調用java類的方法?
我不希望程序一直向我顯示所有信息,一次只能查看8個不同的信息,比如市場價值,股票價格,首席執行官等。我創建了一些常見的信息,但我想爲程序創建一種方法,允許用戶通過插入信息來創建自己的一組數據以供程序顯示。得到(無論你想要的命令)系統。
換句話說,我希望程序允許我在控制檯中輸入一個方法並讓它吐出這個方法。無論如何要做到這一點很簡單嗎?
編輯:我仍然遇到麻煩,以前的解決方案實際上並沒有工作。所以我要去告訴你在這裏到底是什麼進出口工作:
public ArrayList<Object> newtag = new ArrayList<Object>();
ArrayList tagname = new ArrayList();
double tagnum;
int e = 0;
public void printCreate() throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Scanner scan = new Scanner(System.in);
System.out.println("Insert the number of tags you wish to view up to 5.");
tagnum = scan.nextDouble();
if (tagnum > 5){
System.out.println("Please enter a value less than or equal to 5.");
tagnum = scan.nextDouble();
} else {
for (int a = 0; a < tagnum; ++a){
++e;
System.out.println(e+". Insert the tag which you would like to look up:");
String taglookup = br.readLine();
newtag.add(taglookup);
System.out.println("Type the name of this tag that you would like to be displayed:");
tagname.add(br.readLine());
}
int c = 0;
for(Company info: companies){
if (tagnum == 1){
++c;
System.out.println("-------------------------------------------------------------");
System.out.println();
System.out.format("#%s. %5s Last trade: %s Days Low-High: %s - %s volume:%s \n", c, info.getCompanyName(), info.getRTLastTrade(), info.getDaysLow(), info.getDaysHigh(), info.getVol());
System.out.println();
System.out.println(tagname.get(0) + " : " + newtag.get(0));
System.out.println();
} else if (tagnum == 2){
++c;
System.out.println("-------------------------------------------------------------");
System.out.println();
System.out.format("#%s. %5s Last trade: %s Days Low-High: %s - %s volume:%s \n", c, info.getCompanyName(), info.getRTLastTrade(), info.getDaysLow(), info.getDaysHigh(), info.getVol());
System.out.println();
System.out.println(tagname.get(0) + " : " + newtag.get(0) + " " + tagname.get(1) + " : " + newtag.get(1));
System.out.println();
我需要基本上有它,以便newtag.get(0)成爲無論從用戶的信息列表,所有有干將希望。我嘗試過使用克里斯坦方法,但是從公司類中調用不同的公司有一個問題。
這將是最好的選擇,但OP詢問是否字面上可以在要執行的命令行中寫入方法。 – Typo
Sry juan曼努埃爾,基督徒的答案基本上是什麼即時尋找,但是,唯一的問題是,我想能夠返回一個字符串中的新方法,所以本質上即時通訊將不得不列出一些30個不同的獲取如果方法很好,但不如我希望的那樣美觀。 = D謝謝克里斯蒂安我很欣賞它。 – WolVes
@ user1302551你是什麼意思*返回字符串中的新方法*? – Christian