java - 在if语句中如何将字符串添加在一起
问题描述
我创建了一个 if 语句,如果按下某个键,它会创建一个字符串变量。我想最后添加所有创建的变量,以便可以输出,因为它是一个票务程序。我也需要帮助,因为我怎样才能让字符'X'停止while循环我基本上想要做的就是允许用户选择一个部分,然后打印出他们选择的部分以及总有。请帮忙。
/* Name:
* Date:
* Course:
* Description:
*/
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
{
String a1;
String b1;
String z1;
String d1;
String e1;
String f1;
double a = 0.0;
double b = 0.0;
double z = 0.0;
double d = 0.00;
double e = 0.0;
double f = 0.0;
ticket first = new ticket();
first.selection = 'A';
first.ticketType = "Courtside Prime";
first.price = 360.00;
ticket second = new ticket();
second.selection = 'B';
second.ticketType = "Baseline Prime";
second.price = 106.50;
ticket third = new ticket();
third.selection = 'C';
third.ticketType = "Box Seat";
third.price = 97.00;
ticket fourth = new ticket();
fourth.selection = 'D';
fourth.ticketType = "Lower Bowl";
fourth.price = 94.50;
ticket fifth = new ticket();
fifth.selection = 'E';
fifth.ticketType = "Upper Bowl";
fifth.price = 91.00;
ticket sixth = new ticket();
sixth.selection = 'F';
sixth.ticketType = "Drake Zone";
sixth.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 ");
char choice;
choice = c.readChar();
while(choice != 'X') {
c.println("Enter the letter of the seat section you would like to select");
choice = c.readChar();
if (choice == 'A');{
a = first.price;
a1 = first.ticketType;
}
if (choice == 'B');{
b = second.price;
b1 = second.ticketType;
}
if (choice == 'C');{
z = third.price;
z1 = third.ticketType;
}
if (choice == 'D');{
d = fourth.price;
d1 = fourth.ticketType;
}
if (choice == 'E');{
e = fifth.price;
e1 = fifth.ticketType;
}
if (choice == 'F');{
f = sixth.price;
f1 = sixth.ticketType;
}
}
double total = (a + b + z + d + e + f);
String totalText = (a1 + b1 + z1 + d1 e1 +f1);
}
}
}
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
}
它最终会打印出累积总数和他们在点击 x 后选择的门票部分,我想结束 while 循环但不知道如何
解决方案
这是问题的很大一部分。if 后面的分号使它成为一个空的条件块。剩余的代码总是被执行。这只是一个例子,每个 if 语句都有这个问题。
if (choice == 'A');{
a = first.price;
a1 = first.ticketType;
}
应该是这样的
if (choice == 'A') {
a = first.price;
a1 = first.ticketType;
}
您可能不希望第一行要求用户输入。
char choice;
// choice = c.readChar();
while(choice != 'X') {
c.println("Enter the letter of the seat section you would like to select");
choice = c.readChar();
推荐阅读
- php - Wordpress 开发:为资产请求设置缓存标头
- spring - 多次提交后如何手动确认偏移量
- python - 如何在文本中获取匹配的 n-gram 的偏移量
- java - 环境变量 JAVA_STARTED_ON_FIRST_THREAD 即使指定也返回 null
- ios - 应用商店网址在 safari 中打开,但未在 chrome 中打开。可能是什么问题?
- python - UnboundLocalError:分配前引用的局部变量“a1”
- javascript - Safari 剪贴板错误“TypeError: undefined is not an object”
- json - 如何使用机器人框架代码从列表中获取第一个键?
- java - 只想从我的手机图库中添加图片到数据库中,但在两者之间遇到了一些问题
- python - 没有名为 azure.cosmos.cosmos_client 的模块