为什么我的程序不通过控制台接受输入? (JAVA)

时间:2015-12-30 21:19:11

标签: java input constructor

我知道这是一个愚蠢的愚蠢程序。它只是练习构造函数。

public class PetRecord {
private String name = "Bob";
private int age;
private double weight;

public PetRecord(String initialName, int initialAge, double initialWeight) {
    name = initialName;

    if(initialAge < 0 || weight < 0) {
        System.out.println("Error: Age or weight cannot be negative");
        System.exit(0);
    }
        else {
            age = initialAge;
            weight = initialWeight;

        }
    }


public PetRecord(String initialName) {
    name = initialName;
}
public void output() {
    System.out.println("Name: " + name);
    System.out.println("Age: " + age);
    System.out.println("Weight: " + weight);
}
public void setAll(String newName, int newAge, double newWeight) {
    name = newName;
    if ((newAge < 0) || (newWeight < 0)) {
    System.out.println("Error: Negative age or weight.");
    System.exit(0);
    }
    else { 
    age = newAge;
    weight = newWeight;
        }
    }
public void review() {
    if(age < 8 && weight < 8) {
        System.out.println("Your pets weight and age are healthy.");
    }
    if(age >= 8 && weight >=8) {
        System.out.println("Your pets weight and age are unhealthy.");
    }
    else {
        System.out.println("Come to my office for a proper assessment.");
    }
 }

}

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    PetRecord d = new PetRecord("Bob");
    System.out.println("Please check if our current records are correct.");
    d.output();

    System.out.println("Are they correct?");
    String answer = input.nextLine();
    String yes = "Yes";
    String no = "No";
    if((answer.equals(yes))) {
        System.exit(0);
    }
    if(answer.equals(no)) {
        System.out.println("Please enter your pets name.");
        String correctName = input.nextLine();
        System.out.println("Age?");
        int correctAge = input.nextInt();
        System.out.println("Weight?");
        double correctWeight = input.nextDouble();

        d.setAll(correctName, correctAge, correctWeight);
        System.out.println("Your updated records say: ");
        d.output();

        System.out.println("Would you like a health review?");
        String answer1 = input.nextLine();

        String yes1 = "yes";
        String no1 = "no";

        if(answer1.equals(yes1)) {
            d.review();
        }
        if(answer1.equals(no1)) {
            System.out.println("Thank you for using PetSystem. Goodbye.");
            System.exit(0);
        }




    }
  }
}

我的程序接受String answer的输入,但我的程序不接受String answer1。在程序询问您是否要进行健康检查后,我甚至无法在控制台中输入任何内容。

2 个答案:

答案 0 :(得分:1)

问题来自这里

 System.out.println("Your updated records say: ");
    d.output();

因为您在接受输入的过程中已经打印出一些内容,所以您很可能还有一个额外的换行令牌,而您还没有处理过。在询问“健康评论”问题之前,请输入以下代码。

while (input.hasNextLine()) {
    input.nextLine();
}

这将确保您在继续接受用户输入之前清除任何额外的令牌。

答案 1 :(得分:0)

你可以在&#34之后在控制台中输入任何内容;如果你想进行健康检查。&#34;因为你的代码只运行一个你应该把它放在一个循环中,使它运行多次