我需要一些帮助来使这个票务机器代码工作。我可以使用if,else if和else,并且我已经看到它使用while循环完成。但是,无论何时我尝试将其作为对象运行,我都会得到错误,因为代码底部的打印票证,或者构建成功而没有票证。这是我的代码
package ticketmachine;
import javax.swing.JOptionPane;
public class TicketMachine {
private int price;
private int change;
private int balance;
public int getPrice(){
return price;
}
public int getBalance(){
return balance;
}
public TicketMachine (int ticketCost) {
price = ticketCost;
change = 0;
balance = 0;
}
public void insertMoney (int amount){
balance = balance + amount;
}
public static void main(String[] args){
String input1= JOptionPane.showInputDialog(null, " Insert a coin ");
int ticketCost=Integer.parseInt(input1);
public void printTicket(){
//Simulate the printing of a ticket
System.out.println("##################");
System.out.println("# Ticket");
System.out.println("# " + price + " pence.");
System.out.println("##################");
System.out.println();
}
}
}
答案 0 :(得分:1)
现在打印出来
import javax.swing.JOptionPane;
import java.sql.PreparedStatement;
public class TicketMachine {
private int price;
private int change;
private int balance;
public int getPrice(){
return price;
}
public int getBalance(){
return balance;
}
public TicketMachine (int ticketCost) {
price = ticketCost;
change = 0;
balance = 0;
}
public void insertMoney (int amount){
balance = balance + amount;
}
public static void main(String[] args) {
String input1 = JOptionPane.showInputDialog(null, " Insert a coin ");
int ticketCost = Integer.parseInt(input1);
TicketMachine ticketMachine = new TicketMachine(ticketCost);
ticketMachine.printTicket();
}
public void printTicket(){
//Simulate the printing of a ticket
System.out.println("##################");
System.out.println("# Ticket");
System.out.println("# " + this.price + " pence.");
System.out.println("##################");
System.out.println();
}
}
您应该尝试了解OOP的工作原理。人们说他们不了解你的需要,因为你甚至没有打电话给打印丛林的方法。
答案 1 :(得分:0)
最后两种方法的结束括号错误。尝试:
public static void main(String[] args){
String input1= JOptionPane.showInputDialog(null, " Insert a coin ");
int ticketCost=Integer.parseInt(input1);
} //Here
public void printTicket(){
//Simulate the printing of a ticket
System.out.println("##################");
System.out.println("# Ticket");
System.out.println("# " + price + " pence.");
System.out.println("##################");
System.out.println();
}
} //Closing of the class.