【问题标题】:Input code on line 28 will not work第 28 行的输入代码不起作用
【发布时间】:2015-01-09 18:24:35
【问题描述】:

第 28 行的输入代码不起作用,这是怎么回事?当我运行时,它一直运行到第 28 行,然后程序退出。程序编译没有错误。该程序会询问用户的年龄、性别、名字和姓氏,以及他们是否已婚,是否超过 20 岁。如果用户未满 20 岁,程序将不会询问他们是否已婚。

import java.util.Scanner;

class Gender {
    public static void main(String[] args) {
        Scanner Input = new Scanner(System.in);

        //asking the users age, name, and gender 
        System.out.print("What is your gender (M or F): ");
        String gender = Input.nextLine();
        System.out.print("First name: ");
        String FirstName = Input.nextLine();
        System.out.print("Last name: ");
        String LastName = Input.nextLine();
        System.out.print("Age: ");
        int Age = Input.nextInt();

        if (Age >= 20) {  
            System.out.print("Are you married " + FirstName + " (Y or N): ");
            String AreYouMarried = Input.nextLine(); // << PROBLEM

            if (AreYouMarried == "Y") {
                if(gender == "M") {
                    System.out.println("Then I shal call you Mr." + FirstName + " " + LastName + ".");
                }
                else if(gender == "F") {
                    System.out.println("Then I shal call you Ms." + FirstName + " " + LastName + ".");
                }
            }
        }
        if(Age < 20) {
            System.out.print("Then I shall call you " + FirstName + " " + LastName + ".");
        }
    }  
}

【问题讨论】:

  • “不会工作”是什么意思,哪个是28?
  • 欢迎来到 SO。请指出问题区域。 20号线是哪个??
  • 程序一到达代码的那部分就停止工作。
  • @JamesNelson 代码的哪一部分?
  • @JamesNelson 它不会停止工作,只是你的字符串 == 比较(以你编码的方式)可能都会失败。

标签: java input


【解决方案1】:
  1. 这不是在 Java 中比较字符串的正确方法:

    AreYouMarried == "Y"
    

    使用以下内容:

    "Y".equals(AreYouMarried) 
    

    这同样适用于性别比较。

  2. 不要在同一个程序中混合调用 nextIntnextLine
    这会导致更多问题。坚持只使用其中一种
    单个程序中的方法。

【讨论】:

  • 这可能是真的,但程序甚至还没有到那一步。它不会接受输入
  • 此外,还有一些令人讨厌的极端情况,您在教堂/登记处或离婚法庭运行程序。
  • @JamesNelson 不要混合调用 nextInt 和 nextLine。这导致了这个问题。只坚持其中一种方法。例如。仅使用 nextLine。
  • @JamesNelson 当你这样看时,编程过程本身就是关于“讨厌”的极端情况:)
【解决方案2】:

发生这种情况的原因是 nextInt() 无法读取按下的最后一个字符,原因是 ENTER = '\n'。 要解决它,只需在询问 mariage 之前调用另一个 nextLine() 即可。会是这样的。。

class Gender {

public static void main(String[] args) {
    Scanner Input = new Scanner(System.in);

    //asking the users age, name, and gender 
    System.out.print("What is your gender (M or F): ");
    String gender = Input.nextLine();
    System.out.print("First name: ");
    String FirstName = Input.nextLine();
    System.out.print("Last name: ");
    String LastName = Input.nextLine();
    System.out.print("Age: ");
    int Age = Input.nextInt();

    if (Age >= 20) {  
        System.out.print("Are you married " + FirstName + " (Y or N): ");
        Input.nextLine();   
        String AreYouMarried = Input.nextLine(); // << PROBLEM
//HERE CONTINUE CODING

并且不要忘记将“==”更改为 .equals()。 你不能像那样比较字符串。

【讨论】:

  • 我以前也这样比较过字符串,这种情况有什么不同吗?
  • @JamesNelson 这不是效率低下,而是有问题。
【解决方案3】:

equals()方法替换"",比较字符串内容是否相等使用equals()方法

Syso("Are you married " + FirstName + " (Y or N): ");之后添加Input.nextLine();

public static void main(String[] args){

    try {
        Scanner Input = new Scanner(System.in);

        //asking the users age, name, and gender 
        System.out.print("What is your gender (M or F): ");
        String gender = Input.nextLine();
        System.out.print("First name: ");
        String FirstName = Input.nextLine();
        System.out.print("Last name: ");
        String LastName = Input.nextLine();
        System.out.print("Age: ");
        int Age = Input.nextInt();

        if(Age >= 20){  

            System.out.print("Are you married " + FirstName + " (Y or N): ");
            Input.nextLine();
            String AreYouMarried = Input.nextLine(); 

            if(AreYouMarried.equals("Y")){
                if(gender.equals("M") ){
                    System.out.println("Then I shal call you Mr." + FirstName + " " + LastName + ".");
                }else if(gender.equals("F") ){
                    System.out.println("Then I shal call you Ms." + FirstName + " " + LastName + ".");
                }
            }else{
                System.out.println("XYZ msg.");
            }
        }

        if(Age < 20){
            System.out.print("Then I shall call you " + FirstName + " " + LastName + ".");
        }
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }

}

【讨论】:

  • 它不能解决问题。如果您运行该程序可能会有所帮助。
  • @JamesNelson 你能说说问题吗?你的代码在我的最后工作正常。
  • 可能是我用的IDE,你用的是什么?
  • 我用的是eclipse,但不是IDE问题。
  • 请告诉我你在输入什么,如果你把结婚设置为N,它不会显示任何内容,因为你没有处理M的条件
猜你喜欢
  • 2020-04-28
  • 2023-04-08
  • 1970-01-01
  • 2022-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多