我是一个初学者,我必须制作一个程序,该程序允许用户选择多种类型的票证和数量,然后打印出来。我尝试使用类,但无法识别类的实例。如果您还对如何制定更好的票务计划有任何建议,请告诉我,因为我也很难理解解决此问题的最佳方法。
import hsa.Console;
import java.awt.*;
import javax.imageio.*;
import java.io.*;
public class raps
{
static Console c;
static Image image;
public static void main(String[] args)
{
c = new Console();
image = null;
//Copy and paste this below every time you want to put in a new picture
{
ticket first = new ticket();
ticket.selection = 'A';
ticket.ticketType = "Courtside Prime";
ticket.price = 360.00;
ticket second = new ticket();
ticket.selection = 'B';
ticket.ticketType = "Baseline Prime";
ticket.price = 106.50;
ticket third = new ticket();
ticket.selection = 'C';
ticket.ticketType = "Box Seat";
ticket.price = 97.00;
ticket fourth = new ticket();
ticket.selection = 'D';
ticket.ticketType = "Lower Bowl";
ticket.price = 94.50;
ticket fifth = new ticket();
ticket.selection = 'E';
ticket.ticketType = "Upper Bowl";
ticket.price = 91.00;
ticket fifth = new ticket();
ticket.selection = 'F';
ticket.ticketType = "Drake Zone";
ticket.price = 50.00;
/*c.println(" A | Courtside Prime | $360.00 ");
c.println(" B | Baseline Prime | $106.50 ");
c.println(" C | Box Seat | $97.00 ");
c.println(" D | Lower Bowl | $94.50 ");
c.println(" E | Upper Bowl | $91.00 ");
c.println(" F | Drake Zone | $50.00 ");
c.println("Enter the letter of the seat section you would like to select");
*/
class ticket{
char selection; //The Letter of Selection The User will choose ex. A, B, C etc.
String ticketType; //The type of ticket the user will choose ex. Courtside Prime, Baseline Prime etc.
double price; //The price of each ticket
}
}
}
}
Java
我认为您如何设置它有两个基本问题。我倾向于将Ticket类移出该方法。
public class Raps {
static Console c;
static Image image;
static class Ticket {
char selection; //The Letter of Selection The User will choose ex. A, B, C etc.
String ticketType; //The type of ticket the user will choose ex. Courtside Prime, Baseline Prime etc.
double price; //The price of each ticket
}
public static void main(String[] args) {
...
您的代码使用的是类名而不是变量。
Ticket first = new Ticket();
first.selection = 'A';
first.ticketType = "Courtside Prime";
first.price = 360.00;
也是最佳做法:以大写字母开头的类。这样可以消除类和变量之间的某些混淆。
避免在静态方法中放入太多代码。稍后,当您开始使用继承(扩展)时,将无法覆盖静态方法,并且您很有可能必须进行一些重构。
将main更改为类似
private Console c;
private Image image;
public static void main (String[] args) {
Raps raps = new Raps();
raps.createTickets();
}
private void createTickets() {
// all of your code here
}
因为它是static
方法,所以将其读取为过程代码。直到到达此行ticket first = new ticket();
时,ticket
才知道class ticket{
是什么。