简体   繁体   English

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

[英]Why is my program not accepting input through the console? (Java)

I know it's a silly silly program. 我知道这是一个愚蠢的愚蠢的计划。 It's just to practice constructors. 它只是练习构造函数。

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);
        }




    }
  }
}

My program accepts input for String answer, but my program will not accept String answer1. 我的程序接受String answer的输入,但我的程序不接受String answer1。 I can't even type anything in the console after the program asks you if you would like a health review. 在程序询问您是否要进行健康检查后,我甚至无法在控制台中输入任何内容。

The issue comes from here 问题来自这里

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

Because you have printed something out in the middle of accepting input, you most likely have an extra newline token you have not dealt with. 因为您在接受输入的过程中已经打印出一些内容,所以您很可能还有一个额外的换行令牌,而您还没有处理过。 Prior to asking the "health review" question place the following code. 在询问“健康评论”问题之前,请输入以下代码。

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

This will make sure that you clear out any extra tokens before continuing to accept user input. 这将确保您在继续接受用户输入之前清除任何额外的令牌。

you can,t type anything in the console after "if you would like a health review." 您可以在“如果您想要健康检查”之后在控制台中输入任何内容。 because you code only runs ones you should put it in a loop to make it run more than once 因为你的代码只运行一个你应该把它放在一个循环中,使它运行不止一次

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM