这是我正在为样本考试工作的一个问题,但无法弄清楚我在搞乱代码的地方。显然它没有运行。任何提示都会很好。我是java新手。
import java.util.Scanner;
public class TicketProgram {public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int totalTickets = 100;
int ticketsPerPerson = 4;
do {
System.out.println("Please enter the total amount of tickets you wish to purchase: (No more than 4) ");
int ticketsSold = in.nextInt();
if (ticketsSold > 4){
System.out.println("Max of 4 ticket. Please reenter the amount of tickets you want to purchase: ");
}
else {
System.out.println("You have chosen to purchase " + ticketsSold + " tickets.");
int ticketsLeft = (totalTickets - ticketsSold);
System.out.println("There are " + ticketsLeft + " left");
while (ticketsLeft > 0); {
if (ticketsLeft == 0)
System.out.println("There are no tickets left. ");}
}
}
编译此程序时会出现编译时错误
线程中的异常" main" java.lang.RuntimeException:无法编译 源代码 - 错误的树类型:at TicketProgram.main(TicketProgram.java:19) C:\ Users \用户isaacbrekke \应用程序数据\本地\的NetBeans \缓存\ 8.1 \执行人-片段\ run.xml:53: Java返回:1 BUILD FAILED(总时间:0秒)
答案 0 :(得分:1)
这看起来像是一个支架问题。 您需要关闭else语句和do循环中的括号。 另外,最后两行不应该有围绕它们的括号。
do {
System.out.println("Please enter the total amount of tickets you wish to purchase: (No more than 4) ");
int ticketsSold = in.nextInt();
if (ticketsSold > 4){
System.out.println("Max of 4 ticket. Please reenter the amount of tickets you want to purchase: ");
}
else {
System.out.println("You have chosen to purchase " + ticketsSold + " tickets.");
int ticketsLeft = (totalTickets - ticketsSold);
System.out.println("There are " + ticketsLeft + " left");
}
} while (ticketsLeft > 0);
if (ticketsLeft == 0)
System.out.println("There are no tickets left. ");
答案 1 :(得分:1)
试试这个
public class TicketProgram {
static void main(String[] args) {
Scanner in = new Scanner(System.in);
int totalTickets = 100;
int ticketsPerPerson = 4;
int ticketsLeft = 0;
do {
System.out.println("Please enter the total amount of tickets you wish to purchase: (No more than 4) ");
int ticketsSold = in.nextInt();
if (ticketsSold > 4){
System.out.println("Max of 4 ticket. Please reenter the amount of tickets you want to purchase: ");
}
else {
System.out.println("You have chosen to purchase " + ticketsSold + " tickets.");
ticketsLeft = (totalTickets - ticketsSold);
totalTickets = ticketsLeft;
System.out.println("There are " + ticketsLeft + " left");
}}
while (ticketsLeft > 0); {
if (ticketsLeft == 0)
System.out.println("There are no tickets left. ");
}
}
}
答案 2 :(得分:1)
在阅读你的作业时,看起来你正在跳过关键部分。作业要求您跟踪100张门票的买家数量。在这里的代码中,我刚刚添加了一个变量来跟踪买家的数量,我只使用一个变量来跟踪你剩下的票数。无需跟踪剩余的总票数和目前购买的总票数。祝你好运!
import java.util.Scanner;
public class TicketSeller {
public static void main(String[] args) {
int tickets = 100;
int buyers = 0;
Scanner in = new Scanner(System.in);
while(tickets > 0) {
System.out.println("There are " + tickets + " tickets available.");
System.out.print("How many would you like to purchase? ");
int purchase = in.nextInt();
if (purchase > 0 && purchase <= 4) {
tickets -= purchase;
buyers += 1;
}
else {
System.out.println("Sorry, invalid number. Choose between 1 and 4.");
}
}
System.out.println("There were " + buyers + " buyers for the 100 tickets.");
}
}
答案 3 :(得分:0)
括号混乱,代码几乎没有我为你修复的逻辑错误。
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int totalTickets = 100;
while (totalTickets > 0) {
System.out.println("Please enter the total amount of tickets you wish to purchase: (No more than 4) ");
int ticketsSold = in.nextInt();
if (ticketsSold > totalTickets)
{
System.out.println("Not enough tickets available. Please reduce and re-enter the amount");
}
else if (ticketsSold > 4) {
System.out.println("Max of 4 ticket. Please reenter the amount of tickets you want to purchase: ");
} else {
System.out.println("You have chosen to purchase " + ticketsSold + " tickets.");
totalTickets = (totalTickets - ticketsSold);
System.out.println("There are " + totalTickets + " left");
}
}
if (totalTickets == 0)
System.out.println("There are no tickets left. ");
in.close();
}
答案 4 :(得分:0)
您有几个问题:
ticketsLeft
。 (在括号后更正)main(String[] args)
丑陋)如果您还没有使用IDE,我建议您使用IDE(如NetBeans)。你的许多问题已经存在。 (根据您的评论,我发现您使用的是NetBeans。然后使用code formatting
功能,可能通过执行 Alt + Shift + F < / kbd>,我不记得确切的快捷方式)
这是一个有效的例子:
import java.util.Scanner;
public class TicketProgram {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int totalTickets = 100;
int ticketsPerPerson = 4;
int ticketsLeft = totalTickets;
do {
System.out.println("Please enter the total amount of tickets you wish to purchase: (No more than 4) ");
int ticketsSold = in.nextInt();
if (ticketsSold > 4){
System.out.println("Max of 4 ticket. Please reenter the amount of tickets you want to purchase: ");
}
else {
System.out.println("You have chosen to purchase " + ticketsSold + " tickets.");
ticketsLeft = (totalTickets - ticketsSold);
System.out.println("There are " + ticketsLeft + " left");
}
} while (ticketsLeft > 0);
if (ticketsLeft == 0) {
System.out.println("There are no tickets left. ");
}
}
}
答案 5 :(得分:0)
此代码将帮助您的程序正常运行,并且您可以改进此程序以获得进一步的逻辑来处理类似的事情,&#39;客户试图在仅剩下3个时购买4张票,等等。
import java.util.Scanner;
public class TicketProgram {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int totalTickets = 100;
int ticketsPerPerson = 4;
int ticketsLeft = totalTickets;
do {
System.out.println("Please enter the total amount of tickets you wish to purchase: (No more than 4) ");
int ticketsSold = in.nextInt();
if (ticketsSold > ticketsPerPerson){
System.out.println("Max of 4 ticket. Please reenter the amount of tickets you want to purchase: ");
}
else {
System.out.println("You have chosen to purchase " + ticketsSold + " tickets.");
ticketsLeft = (ticketsLeft - ticketsSold);
System.out.println("There are " + ticketsLeft + " left");
}
if (ticketsLeft == 0){
System.out.println("There are no tickets left. ");
}
}while (ticketsLeft > 0);
}
}
答案 6 :(得分:0)
使用IDE有助于解决编码时的编译错误。
您的代码中还缺少一个逻辑。如果购买的门票超过剩余的门票,那么你的逻辑就会失败。以下是您的问题的解决方案
import java.util.Scanner;
public class TicketProgram {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int totalTickets = 100;
int ticketsPerPerson = 4;
int ticketsLeft = totalTickets;
do {
System.out.println("Please enter the total amount of tickets you wish to purchase: (No more than 4) ");
int ticketsSold = in.nextInt();
if (ticketsSold > 4){
System.out.println("Max of 4 ticket. Please reenter the amount of tickets you want to purchase: ");
}
else {
if(ticketsLeft >= ticketsSold){
System.out.println("You have chosen to purchase " + ticketsSold + " tickets.");
ticketsLeft = (ticketsLeft - ticketsSold);
System.out.println("There are " + ticketsLeft + " left");
}else{
System.out.println("You have been allocated only " + ticketsLeft + " tickets.");
ticketsLeft = 0;
}
}
} while (ticketsLeft > 0);
if (ticketsLeft == 0) {
System.out.println("There are no tickets left. ");
}
}
}