下面的代码应该让用户输入:
程序应允许用户将其输入数组并循环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个变量甚至更长的话该怎么办?
此外,如何防止在显示表上显示“ null”。如果用户键入“ exit”,则循环应停止并显示已输入的数据(不包括空值)。
答案 0 :(得分:1)
这将做您想要的。但是您可以改善打印结果部分。
import java.util.ArrayList;
import java.util.Scanner;
public class pbclass {
public static void main(String[] args) {
pbclass objectt = new pbclass();
objectt.dentistpractice();
}
Scanner scan = new Scanner(System.in);
int array_length=10;
int array_length2=0;
String[] patientFamName = new String[array_length];
String[] patientFirstName = new String[array_length];
Integer[] PatientGoodTeeth = new Integer[array_length];
Integer[] PatientBadTeeth = new Integer[array_length];
public void dentistpractice() {
for (int i = 0; i < array_length; ++i) {
System.out.println("Enter surname name ");
patientFamName[i] = scan.nextLine();
if (patientFamName[i].equalsIgnoreCase("close")) {
break;
}
array_length2++;
System.out.println("Enter first name ");
patientFirstName[i] = scan.nextLine();
System.out.println("Enter good teeth ");
PatientGoodTeeth[i] = scan.nextInt();
scan.nextLine();
System.out.println("Enter score for away team: ");
PatientBadTeeth[i] = scan.nextInt();
scan.nextLine();
}
for(int i = 0; i < array_length2; ++i){
System.out.println("-----------------------");
System.out.println(" | " +patientFamName[i] + " | "+ patientFirstName[i] + " | " + PatientGoodTeeth[i] + " | "+ PatientBadTeeth[i]+ " | ");
}
System.out.println("-----------------------");
}
}