首页 > 解决方案 > 在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 循环但不知道如何

标签: java

解决方案


这是问题的很大一部分。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();

推荐阅读