2017-10-21 51 views
0

我已經在下面編寫了一個功能代碼,並且我想簡單地將我的菜單設置從程序內更改爲外部。我被要求從程序外部的.txt文件中獲取sodaMachine內容,程序將從.txt文件中取出並顯示「Take your drink(1.)」。從.txt文件讀取,以便程序使用.txt文件中的變量和字符串來更新

我的問題是我添加什麼來獲得精確的程序,需要int和字符串.txt文件上的命令。因此,如果我輸入「1.)」,程序會提取「可口可樂」及其價格(一個整數)「150」。

import java.lang.Math; 
import java.util.Scanner; 

public class CST1201 { 

    private static Scanner input = new Scanner(System.in); 

    public static void main(String[] args) { 
     int coinTotal = 0; 
     int coinTotal2 = 0; 
     int coinTotal3 = 0; 
     int runAgain = 1; 

     while(runAgain == 1) 
      { 
      while (coinTotal < 100){ 
       System.out.println("Please insert a coin "); 
       coinTotal2 = input.nextInt(); 
       if (coinTotal2 != 25 && coinTotal2 != 5 && coinTotal2 != 10) 
       { 
        coinTotal2 = 0; 
        while (coinTotal2 != 25 && coinTotal2 != 5 && coinTotal2 != 10) 
        { 
         System.out.println("Please insert a coin(5,10,25)"); 
         coinTotal2 = input.nextInt(); 
         if (coinTotal2 != 25 && coinTotal2 != 5 && coinTotal2 != 10) 
         { 
          coinTotal2 = 0; 
         } 
         else 
         { 
          coinTotal = coinTotal + coinTotal2; 
          coinTotal3 = coinTotal + coinTotal2; 
         } 
        } 
       } 
       else 
       { 
        coinTotal = coinTotal + coinTotal2; 
       } 
       System.out.println("Total: " + coinTotal); 
      } 
      int choice = 0; 
      System.out.println("What would you like? \n1.Coca-Cola \n2.Pepsi \n3.Orange Soda \n4.Grape Soda \n5.Mountain Dew \n6.Water"); 
      choice = input.nextInt(); 
      if (choice ==1) { 
       System.out.println("Please take your Coca-Cola"); 
      } else { 
       if(choice == 2) { 
        System.out.println("Please take your pepsi"); 
       } else { 
        if (choice == 3) { 
         System.out.println("Please take your Orange Soda"); 
        } else { 
         if (choice == 4) { 
          System.out.println("Please take your Grape Soda"); 
         } else { 
          if (choice == 5) { 
           System.out.println("Please take your Mountain Dew"); 
          } else { 
           if (choice == 6) { 
            System.out.println("Please take your water"); 
           } 
           } 
           } 
          } 
          } 
          } 
      int change; 
      if (coinTotal > 100){ 
       change = coinTotal - 100; 
       System.out.println("Please take your change: " + change); 
      } 
       System.out.println("Would you like to buy another drink?"); 
       runAgain = input.nextInt(); 
       if (runAgain == 1) 
       { 
        coinTotal = 0; 
       } 
       else 
       { 
        runAgain = 0; 
        System.out.println("Thank you"); 
       } 

      } 
    } 
} 

回答

0

我假設你想利用作爲輸入coinTotal,coinTotal2,coinTotal3,選擇,並從.txt文件runagain並運行程序多次爲給定的輸入?

如果是這種情況,您可以將整個方法放入帶有上述參數的函數中,並通過.txt輸入文件循環提供參數。 您可以使用File和Scanner從文件中讀取內容,具體取決於它在文件中的排列方式。

Scanner scan = new Scanner(new File("FileName")); // this gets you your file 

您可以訪問文件的元素作爲1字,其使用scan.next(),然後將其解析到你需要合適的,然後把它傳遞給你的函數。

希望有幫助。

+0

謝謝。我只是想如何做到這一點,我現在正在研究如何更新.txt文件。 – theslurpeemaker