Java 如何将具有不同名称的字符串添加到一起
我已经创建了ticketAgent和ticketType类,但我很难添加字符串。我尝试过使用包装器类,但不确定这是否是正确的方法。我分别粘贴了代码和提示符。谢谢你的帮助。我是第一次贴海报,所以我希望我遵循了张贴指南 提示: 编写一个名为TicketBooth的程序,在3个不同的票证代理上进行实例化。TicketBooth程序应提示用户从其希望购买车票的票务代理处购买车票,然后显示可用的车票类型和价格列表。TicketBooth程序应允许用户选择要购买的车票。选择票证类型后,TicketBooth程序应显示: •该票务代理的总销售额 •票务代理销售的总票数 •所有票务代理销售的票务总数 TicketBooth程序应继续提示用户从其中一个票证代理处购买票证,直到用户退出Java 如何将具有不同名称的字符串添加到一起,java,string,Java,String,我已经创建了ticketAgent和ticketType类,但我很难添加字符串。我尝试过使用包装器类,但不确定这是否是正确的方法。我分别粘贴了代码和提示符。谢谢你的帮助。我是第一次贴海报,所以我希望我遵循了张贴指南 提示: 编写一个名为TicketBooth的程序,在3个不同的票证代理上进行实例化。TicketBooth程序应提示用户从其希望购买车票的票务代理处购买车票,然后显示可用的车票类型和价格列表。TicketBooth程序应允许用户选择要购买的车票。选择票证类型后,TicketBooth
package Exercises;
import java.util.Scanner;
public class TicketBooth {
private static Scanner input;
public static void main(String[] args) {
input = new Scanner(System.in);
String ticketAgent = "sam", "nicole" , "alex";
String ticketType = "one";
int t = Integer.parseInt(ticketAgent);
int s = Integer.parseInt(ticketType);
int sum;
System.out.println("Please select your ticket agent: ");
ticketAgent = input.next();
System.out.println("Please select your ticket type or press -1 to quit: ");
ticketType = input.next();
sum = t +s;
System.out.println("The total tickets that were sold were: " + sum);
}
}
package exercises;
public class TicketAgent {
private static int numOfTicksPerAgent;
private String agentName;
private int numSold = 0;
private double totalSales = 0.0;
public TicketAgent(String agent) {
agentName = agent;
numSold = 0;
totalSales = 0.0;
numOfTicksPerAgent += 1;
}
public void sellTicket(TicketType tt) {
numSold++;
totalSales = totalSales + tt.getticketPrice();
numOfTicksPerAgent++;
}
public double gettotalSales() {
return totalSales;
}
public double getnumSold() {
return numSold;
}
public double getnumOfTicksPerAgent() {
return numOfTicksPerAgent;
}
}
package exercises;
public enum TicketType
{
CHILD (6.50),
ADULT (9.50),
SENIOR (6.50);
private double tikPrice;
TicketType(double price)
{
tikPrice = price;
}
public double getticketPrice()
{
return tikPrice;
}
}
问题是,这似乎是你的家庭作业 没有人愿意为你做家务 编辑:我看到你刚才添加了你的类。 您所要做的就是添加do while循环 并以正确的方式实例化您的代理。 你的问题到底出在哪里?所以我们可以更好地帮助你 我的第一个方法是,简单一点, 要编写TicketAgent类和TicketType类
TicketAgent agentSam = new TicketAgent("Sam", new ExpensiveTicket());
用一种方式写票证,你可以把
将代理和类型输入到构造函数中
TicketAgent agentSam = new TicketAgent("Sam", new ExpensiveTicket());
添加setter和getter方法
TicketAgent agentSam = new TicketAgent("Sam", new ExpensiveTicket());
TicketType需要一个float值作为价格,可能还需要一个字符串描述
或者类似的
完成后,返回主课堂。
打开一个do while循环,并将一个布尔值设置为循环参数
用户中止的值,在开始和结束时为false
如果用户在quit命令中键入,则更改
在循环之前,用票据实例化代理
显示名称和价格。展示门票。数一数。请求中止
希望这有点帮助。
对不起,我的英语不是我的母语
additional:
private boolean userWantsToQuit = false;
private List<TicketAgent> avaibleAgents = new ArrayList<TicketAgent>();
private List<TicketAgent> usedAgents= new ArrayList<TicketAgent>();
private List<TickeType> boughtTickets= new ArrayList<TicketType>();
main(...){
agents.add(new TicketAgent("Sam"));
agents.add(new TicketAgent("wise"));
agents.add(new TicketAgent("gamgee"));
do{
usedAgents.add(askUserForAgent()); //extra static method
boughtTickets.add(askUserForType(agent)); //extra static method
userWantsToQuit = askUserToQuit();//extra static method
}while(!userWantsToQuit);
displayAgentSummary(usedAgents);
displayTicketSummary(boughtTickets);
}
[...]
public static displayAgentSummary(List<TicketAgent> agents)
{
for(TicketAgent agent : agents)
{
System.out.println("Served by: " + agent.getName());
}
}
public static displayTicketSummary(List<TicketType> tickets)
{
float totalPrice = 0;
for(TicketType ticket : tickets)
{
totalPrice =+ ticket.getPrice();
System.out.println("You bought a " + ticket.getName() + " for " + ticket.getPrice()";
}
System.out.println("You bought tickets in total for " + totalPrice + " dollar".
}
附加:
私有布尔userWantsToQuit=false;
private List availablegents=new ArrayList();
private List usedAgents=new ArrayList();
private List boughtTickets=new ArrayList();
主要(…){
代理。添加(新票务(“Sam”);
代理。添加(新票务(“wise”);
代理。添加(新票务(“gamgee”);
做{
add(askUserForAgent());//额外的静态方法
add(askUserForType(agent));//额外的静态方法
userWantsToQuit=askUserToQuit();//额外的静态方法
}而(!userWantsToQuit);
显示代理摘要(usedAgents);
显示票证摘要(购买票证);
}
[...]
公共静态显示代理摘要(列出代理)
{
对于(票务代理:代理)
{
System.out.println(“服务对象:“+agent.getName());
}
}
公共静态显示票证摘要(列出票证)
{
浮动总价=0;
对于(票证类型票证:票证)
{
totalPrice=+ticket.getPrice();
System.out.println(“您为“+ticket.getPrice()”购买了“+ticket.getName()+”;
}
System.out.println(“您总共以“+totalPrice+dollar”购买了门票。
}
sam或“nicole”是一个合理的整数吗?(我很确定alex是一个浮点数。)你的TicketAgent
和TicketType
类在哪里?或者你指的是你的(坏掉的)TicketAgent
和TicketType
字符串实例吗?@DaveNewton我很确定“一个”是一个整数,但不在此上下文中。