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");
}
}
}
}
謝謝。我只是想如何做到這一點,我現在正在研究如何更新.txt文件。 – theslurpeemaker