簡體   English   中英

為什么我的程序不通過控制台接受輸入? (JAVA)

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

我知道這是一個愚蠢的愚蠢的計划。 它只是練習構造函數。

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。 在程序詢問您是否要進行健康檢查后,我甚至無法在控制台中輸入任何內容。

問題來自這里

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

因為您在接受輸入的過程中已經打印出一些內容,所以您很可能還有一個額外的換行令牌,而您還沒有處理過。 在詢問“健康評論”問題之前,請輸入以下代碼。

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

這將確保您在繼續接受用戶輸入之前清除任何額外的令牌。

您可以在“如果您想要健康檢查”之后在控制台中輸入任何內容。 因為你的代碼只運行一個你應該把它放在一個循環中,使它運行不止一次

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM