我正在使用方法和两个独立的Java类制作程序,一切似乎都进行得很顺利,但是代码需要使用$ 1,$ 2和每次按数字四分之一来存储付款与该选择相关的是,我尝试将数字递增到无用
主要
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Press 1) to Walk up to the Machine1!");
System.out.println("Press 2) to Walk up to Machine2!");
System.out.println("Press 3) to Walk away!");
int choice = input.nextInt();
int train;
train id = new train();
if (choice != 3) {
do{
System.out.println(id);
System.out.print(
"1) Add Adult \n" +
"2) Remove Adult \n" +
"3) Add Child \n" +
"4) Remove Child \n" +
"5) Add Quarter \n" +
"6) Add Loonie \n" +
"7) Add Toonie \n" +
"8) Print Ticket\n" +
"9) Exit\n"+
"Your Choice: "
);
train = input.nextInt();
System.out.println("Option " + train + " selected!");
if (train == 1) id.adultUp();
else if (train == 2) id.adultDown();
else if (train == 3) id.childUp();
else if (train == 4) id.childDown();
else if (train == 5)id.quarterUp();
else if (train == 6)id.loonieUp();
else if (train == 7)id.toonieUp();
}while(train != 9);
System.out.println("Have a Nice Day and Enjoy Chicago!");
System.out.println("Option " + train + " selected!");
}else if (choice == 2){
do{
System.out.println(id);
System.out.print(
"1) Add Adult \n" +
"2) Remove Adult \n" +
"3) Add Child \n" +
"4) Remove Child \n" +
"5) Add Quarter \n" +
"6) Add Loonie \n" +
"7) Add Toonie \n" +
"8) Print Ticket\n" +
"11)Exit\n"+
"Your Choice: "
);
train = input.nextInt();
System.out.println("Option " + train + " selected!");
if (train == 1) id.adultUp();
else if (train == 2) id.adultDown();
else if (train == 3) id.childUp();
else if (train == 4) id.childDown();
else if (train == 5)id.quarterUp();
else if (train == 6)id.loonieUp();
else if (train == 7)id.toonieUp();
}while(train != 11);
System.out.println("Have a Nice Day and Enjoy Chicago!");
System.out.println("Option " + train + " selected!");
}
if (choice == 3){
System.out.println("Have a Nice Day and Enjoy Chicago!");
}
}
}
培训班
public class train {
int aTicket = 0;
int cTicket = 0;
String ticket = "";
double q = 0.25;
double loonie = 1.00;
double toonie = 2.00;
double credit = 0;
public void adultUp(){this.aTicket++;}
public void adultDown(){this.aTicket--;}
public void childUp(){this.cTicket++;}
public void childDown(){this.cTicket--;}
public void quarterUp(){this.credit++;}
public void loonieUp(){this.credit++;}
public void toonieUp(){this.credit++;}
public String toString()
{
// output the current channel and volume
return "\n" +
"**************************************** \n" +
"**Destination: Wrigley Field\n"+
"**Ticket Prices:"+" "+"Adult Ticket: $3.00"+" "+"Child Ticket: $1.50\n"+
"* Adult: " + this.aTicket + " \n" +
"* Child: " + this.cTicket + " \n" +
" *Credit: "+"$" + this.credit + " \n"+
"****************************************";
}
}