在if语句后如何将字符串添加在一起

问题描述 投票:0回答:1

我创建了一个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循环,但不知道如何

java
1个回答
0
投票

这是问题的重要部分。 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();
© www.soinside.com 2019 - 2024. All rights reserved.