使用system.out从循环JAVA内的数组输出数据

下面的代码应该接受用户输入:


姓名

坏/好牙

程序应该允许用户将其输入到一个数组中并循环 10 次,然后以表格格式输出该列表(您可以在代码底部找到该格式)


import java.util.Scanner;


public class pbclass;

{

    public static void main(String[] args)

    {

        pbclass objectt = new pbclass();

        objectt.dentistpractice();

    }


    public void dentistpractice()

    {

        Scanner scan = new Scanner(System.in);

        String[] patientFamName = new String[10];

        String[] patientFirstName = new String[10];

        String[] PatientGoodTeeth = new String[10];

        String[] PatientBadTeeth = new String[10];


        for(int i = 0; i < 10; ++i)

        {

            System.out.println("Enter surname name ");

            patientFamName[i] = scan.nextLine();

            if (patientFamName[i].equalsIgnoreCase("close") ==true)

            {

                break;

            }

            System.out.println("Enter first name ");

            patientFirstName[i] = scan.nextLine();

            System.out.println("Enter good teeth ");

            PatientGoodTeeth[i] = scan.nextLine();

            int numconverterForHomeTeam = Integer.parseInt(PatientGoodTeeth[i]);

            System.out.println("Enter score for away team: ");

            PatientBadTeeth[i] = scan.nextLine();

            int numconverterForAwayTeam = Integer.parseInt(PatientBadTeeth[i]);

    }

    System.out.println(patientFamName[0] + patientFirstName[0] + " ||||||| " +PatientGoodTeeth[0]  + PatientBadTeeth[0] );

    System.out.println(patientFamName[1] + patientFirstName[1] + " ||||||| " +PatientGoodTeeth[1]  + PatientBadTeeth[1] );


}

我面临的问题是我不知道如何以简单的方式输出数据。如果数组是 1000 个变量甚至更长怎么办?


此外,如何防止“空”显示在显示表上。如果用户键入“exit”,则循环应停止并显示已输入的数据,但不包括空值。


慕丝7291255
浏览 151回答 1
1回答

米脂

这会做你想做的。但是您可以改进打印结果部分。import java.util.ArrayList;&nbsp; &nbsp; import java.util.Scanner;&nbsp; &nbsp; public class pbclass {&nbsp; &nbsp; &nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pbclass objectt = new pbclass();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; objectt.dentistpractice();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; Scanner scan = new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int array_length=10;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int array_length2=0;&nbsp; &nbsp; &nbsp; &nbsp; String[] patientFamName = new String[array_length];&nbsp; &nbsp; &nbsp; &nbsp; String[] patientFirstName = new String[array_length];&nbsp; &nbsp; &nbsp; &nbsp; Integer[] PatientGoodTeeth = new Integer[array_length];&nbsp; &nbsp; &nbsp; &nbsp; Integer[] PatientBadTeeth = new Integer[array_length];&nbsp; &nbsp; &nbsp; &nbsp; public void dentistpractice() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < array_length; ++i) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter surname name ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; patientFamName[i] = scan.nextLine();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (patientFamName[i].equalsIgnoreCase("close")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array_length2++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter first name ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; patientFirstName[i] = scan.nextLine();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter good teeth ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PatientGoodTeeth[i] = scan.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scan.nextLine();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter score for away team: ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PatientBadTeeth[i] = scan.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scan.nextLine();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int i = 0; i < array_length2; ++i){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("-----------------------");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(" | " +patientFamName[i] + " | "+ patientFirstName[i] + " | " + PatientGoodTeeth[i] + " | "+ PatientBadTeeth[i]+ " | ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("-----------------------");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java