我已在下面编写了一个正常运行的代码,我只想将我的菜单设置从程序中更改为外部。我被要求从程序外部的.txt文件中获取.txt文件中的sodaMachine内容,该程序将从.txt文件中获取并显示"带上你的饮料(1。)"。
我的问题是我要添加什么来获取一个准确接受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 :(得分:0)
我假设你想从.txt文件中输入coinTotal,coinTotal2,coinTotal3,choice和runagain,并为给定的输入多次运行你的程序?
如果是这种情况,您可以将整个方法放入具有上述参数的函数中,并通过.txt输入文件循环提供参数。 您可以使用文件和扫描程序从文件中读取内容,具体取决于文件在文件中的排列方式。
Scanner scan = new Scanner(new File("FileName")); // this gets you your file
您可以使用scan.next()将文件元素作为1个字符串进行访问,然后将其解析为适当的需要,然后将其传递给您的函数。
希望有所帮助。