2017-10-21 2 views
0

아래에 작동하는 코드를 작성 했으므로 프로그램 내에서 외부로 내 메뉴 설정을 간단히 변경하고 싶습니다. 프로그램이 .txt 파일에서 가져올 프로그램 외부의 .txt 파일에서 sodaMachine 내용을 가져 오도록 요청 받았으며 "Take your drink (1.)"라고 표시됩니다..txt 파일에서 프로그램이 .txt 파일의 변수 및 문자열을 사용하여 업데이트합니다.

제 질문은 int가 정확한 프로그램을 얻고 명령에서 .txt 파일의 문자열을 얻으려면 무엇을 추가해야합니까? 그래서 "1"을 입력하면 "코카콜라"와 그 가격 (int) "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 

당신은 scan.next()를 사용하여, 1 워드 문자열로 파일의 요소에 액세스 한 다음 적절한 필요로 분석 한 다음 함수에 전달할 수 있습니다.

희망을 얻었습니다.

+0

감사합니다. 방금하는 방법을 알아 냈습니다. 현재 .txt 파일을 어떻게 업데이트합니까? – theslurpeemaker

관련 문제