gpt4 book ai didi

Java outputStream 无法正确打印

转载 作者:行者123 更新时间:2023-12-01 05:27:56 25 4
gpt4 key购买 nike

我正在为类制作一个简单的 Java 程序,该程序应该将变量 petName、petType 和 numVisits 输出到名为“PatientData.txt”的 txt 文件。我正确打印了 petType 和 numVisits,但没有正确打印 petName。我几乎肯定它与我的第一个垃圾语句有关,因为 petType 是唯一必须捕获 2 个以上单词的字符串。这是我的代码:

import java.util.Scanner;
import java.io.*;
public class AcmeClinic
{
public static void main(String[] args )
{
Scanner keyboard = new Scanner(System.in);
PrintWriter outputStream = null;

try
{
outputStream = new PrintWriter(new FileOutputStream("PatientData.txt"));
}

catch(FileNotFoundException e)
{
System.out.println("Unable to create the output file.");
System.exit(0);
}

System.out.println("Enter the number of pets to store information for:");
int amount = keyboard.nextInt();
String [] petNames = new String [amount];
String [] petTypes = new String [amount];
int [] numVisits = new int [amount];
int index;
String junk;
outputStream.println("Patient Data:");
outputStream.println("Pet Name Pet Type Number of Visits");
if (amount >= 1)
{
for (index = 0; index < amount; index++)
{
System.out.println("Type the pet name, then press Enter:");
petNames[index] = keyboard.nextLine();
junk = keyboard.nextLine();
System.out.println("Type the animal type (dog, cat, bird, rodent), then press Enter:");
petTypes[index] = keyboard.nextLine();
System.out.println("Type the number of visits last year, then press Enter:");
numVisits[index] = keyboard.nextInt();
outputStream.printf("%8s %-8s %-8d%n",petNames[index], petTypes[index],numVisits[index]);
}
}

outputStream.close();
}
}

输入示例:

Enter the number of pets to store information for:
4
Type the pet name, then press Enter:
Champ
Type the animal type (dog, cat, bird, rodent), then press Enter:
dog
Type the number of visits last year, then press Enter:
8
Type the pet name, then press Enter:
Bob
Type the animal type (dog, cat, bird, rodent), then press Enter:
cat
Type the number of visits last year, then press Enter:
3
Type the pet name, then press Enter:
Mickey
Type the animal type (dog, cat, bird, rodent), then press Enter:
rodent
Type the number of visits last year, then press Enter:
1
Type the pet name, then press Enter:
Polly
Type the animal type (dog, cat, bird, rodent), then press Enter:
bird
Type the number of visits last year, then press Enter:
6

输出示例:(PatientData.txt)

Patient Data:
Pet Name Pet Type Number of Visits
dog 8
cat 3
rodent 1
bird 6

最佳答案

nextInt() 导致立即发生nextLine(),因此避免使用它。这也适用于 2 个或更多单词...

System.out.println("Enter the number of pets to store information for:");
int amount = Integer.parseInt(keyboard.nextLine());
String [] petNames = new String [amount];
String [] petTypes = new String [amount];
int [] numVisits = new int [amount];
outputStream.println("Patient Data:");
outputStream.println("Pet Name Pet Type Number of Visits");

for (int index=0;index < amount; index++) {
System.out.println("Type the pet name, then press Enter:");
petNames[index] = keyboard.nextLine();
System.out.println("Type the animal type (dog, cat, bird, rodent), then press Enter:");
petTypes[index] = keyboard.nextLine();
System.out.println("Type the number of visits last year, then press Enter:");
numVisits[index] = Integer.parseInt(keyboard.nextLine());
outputStream.printf("%8s %-8s %-8d%n", petNames[index], petTypes[index], numVisits[index]);
}

正如前面提到的,您不必在这里使用数组。你可以这样做...

System.out.println("Enter the number of pets to store information for:");
int amount = Integer.parseInt(keyboard.nextLine());
outputStream.println("Patient Data:");
outputStream.println("Pet Name Pet Type Number of Visits");

String petName = new String();
String petType = new String();
int numVisit = 0;

for (int index = 0; index < amount; index++) {
System.out.println("Type the pet name, then press Enter:");
petName = keyboard.nextLine();
System.out.println("Type the animal type (dog, cat, bird, rodent), then press Enter:");
petType = keyboard.nextLine();
System.out.println("Type the number of visits last year, then press Enter:");
numVisit = Integer.parseInt(keyboard.nextLine());
outputStream.printf("%8s %-8s %-8d%n", petName, petType, numVisit);
}

关于Java outputStream 无法正确打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9491944/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com