我正在尝试开发一个简单的程序,它将从用户那里获取输入1-11并在类中消化它们并输出一个干净的打印语句,该语句将根据这些输入进行更改。
以下是我目前的一些代码。
public static void main(String[] args) {
mainInputMenu();
}
private static void mainInputMenu() {
System.out.println ( "=======================================\n"
+ "Welcome to the Ticket Counter\n"
+ "1. Approach first machine.\n"
+ "2. Approach Second machine\n"
+ "3. Exit." );
Scanner keyboard = new Scanner ( System.in );
String ticketCounter;
int counter = 0;
//I have a do while statement which confirms input but irrelevant
while ( counter == 1 ) {
machineOne ( counter );
}
while ( counter == 2 ) {
machineTwo ( counter );
}
while ( counter == 3 ) {
System.exit(0);
}
}
public static void machineOne(int counter) {
Scanner keyboard = new Scanner ( System.in );
String ticketInput;
int choice = 0;
TheMachine machineOne = new TheMachine();
TheMachine.writeOutput();
switch ( choice ) {
case 1:
machineOne.option = 1;
break;
case 2:
machineOne.option = 2;
break;
case 3:
machineOne.option = 3;
}
}
这是主程序,目前最多只有3个选项。现在进入我不了解的......我可以尝试通过一个方法来实现这一点,我认为可能会创建这个类的子组件来完成工作并在writeOutput中打印这些变量( )。导致非静态方法的结果无法从下面的静态上下文错误中引用。
public class TheMachine {
public static void writeOutput() {
System.out.println ( " ------------------------------------------\n");
System.out.println ( "Adults: " + getadultSum() ); //Simplified to show the issue
public int getadultSum () {
switch ( option ) {
case 3:
adultSum = adultSum + 1;
return adultSum;
case 4:
adultSum = adultSum - 1;
return adultSum;
}
或者我的第一个尝试解决方案是根据选项变量进行切换以更改这些不同的变量。其中一些包括成人数,儿童数,输入的美元数等等。
static void writeOutput()
{
int childTickets = 0;
int adultTickets = 0;
String route = "";
int childSum = 0;
int adultSum = 0;
int creditSum = 0;
switch (choice) {
case 1:
route = "Uptown";
break;
case 2:
route = "Downtown";
break;
case 3:
adultSum = adultSum + 1;
break;
default:
break;
}
但是我遇到这个问题,我的意思是它只能运行一次,但每次输入新输入时,变量都会重置为0。
如果有人花时间阅读这篇文章并能为我提供帮助,请祝福你。我感谢你所有的时间和精力。