java用户输入需要打印而不需要数组

时间:2015-07-05 01:04:25

标签: java loops user-input

嘿大家我正试图让用户输入一遍又一遍地打印。它只打印我需要在没有数组时执行此操作的最后一个用户输入。我试过循环,但我认为我的知识有限,因为我对java很新,如果有人可以提供帮助那就太棒了

import java.util.Scanner;

public class test{

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    Scanner sc = new Scanner(System.in);
    String selection;
    String malesName = null; 
    double  malesAge = 0;
    String femalesName = null; 
    double femalesAge = 0; 
    do
    {
    System.out.println("Enter part Guests");
    System.out.println("------------------------------------------------");
    System.out.println("\n");

    System.out.println("A - Male guest has arrived");
    System.out.println("B - Female Guest has arrived");
    System.out.println("X - There are no more guests");
    System.out.println("\n");

    System.out.println("Enter your selection:");
    selection = sc.nextLine();
        if (selection.length() !=1)
        {
            System.out.println("Response must be a single character!");
        }
        else
        {   
            switch (selection)
            {
               case "A":
               case "a":

                  System.out.print("Enter males name:");
                  malesName = input.nextLine();
                  System.out.print("Males ages:");
                  malesAge = input.nextDouble();
                  String input4 = input.nextLine();
                  break;
               case "B":
               case "b":
                  System.out.print("Enter females name:");
                  femalesName = input.nextLine();
                  String input5 = input.nextLine();
                  System.out.print("Females age:");
                  femalesAge = input.nextDouble();
                  String input6 = input.nextLine();
                  break;
               case "X":
               case "x":

                  System.out.println("Time to party...");
                  break;

               default:
                  System.out.println("Error - invalid selection!");

            }
         }




      } while (selection.equals("X") == false);



    String maleTotal =" - " + malesName + "(" + malesAge + ")"; 
    String femaleTotal = " - " + femalesName + "(" + femalesAge + ")" ;




    System.out.println("Male details:");
    System.out.print("\n");
    System.out.println( maleTotal );
    System.out.println("Female details:");
    System.out.print("\n");
    System.out.print( femaleTotal );
/*
 * it should be printing
 * 
 * Male details:
 * -dan(22)
 * -sam(23)
 * Female details:
 * -samantha(32)
 * -julie(25)
 * 
 * 
 *but its only printing the last user input
 */


 }
 }

1 个答案:

答案 0 :(得分:0)

问题是你只计算一次总数,因为在循环结束后你有这些语句。你应该在循环中进行连接,然后能够打印它们,然后在循环开始之前声明变量。

String maleTotal = ""; 
String femaleTotal = "";

do {

    // rest of the code

    maleTotal += (" - " + malesName + "(" + malesAge + ")\n"); 
    femaleTotal += (" - " + femalesName + "(" + femalesAge + ")\n");

} while (!selection.equalsIgnoreCase("X"));

我还建议切换到使用StringBuilder s。