Java 第28行的输入代码无效

Java 第28行的输入代码无效,java,input,Java,Input,第28行的输入代码不起作用,怎么了?当我运行时,它一直工作到第28行,然后程序退出。程序编译时没有错误。该程序会询问用户的年龄、性别、名字和姓氏,以及如果他们结婚,是否超过20岁。如果用户未满20岁,程序将不会询问他们是否结婚 import java.util.Scanner; class Gender { public static void main(String[] args) { Scanner Input = new Scanner(System.in);

第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 + ".");
        }
    }  
}
import java.util.Scanner;
阶级性别{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
//询问用户的年龄、姓名和性别
系统输出打印(“您的性别(男或女):”;
字符串性别=Input.nextLine();
系统输出打印(“名字:”);
String FirstName=Input.nextLine();
系统输出打印(“姓氏:”);
字符串LastName=Input.nextLine();
系统输出打印(“年龄:”);
int Age=Input.nextInt();
如果(年龄>=20){
系统输出打印(“你结婚了吗”+姓+“(Y或N):”;
字符串AreYouMarried=Input.nextLine();//
  • 在Java中,这不是比较字符串的正确方法:

    AreYouMarried == "Y"
    
    使用以下命令:

    "Y".equals(AreYouMarried) 
    
    性别比较也是如此

  • 不要在同一程序中混合调用
    nextInt
    nextLine

    这会导致更多问题。请坚持只使用其中一种
    方法在单个程序中

  • equals()
    方法替换
    ,要比较字符串内容的相等性,请使用
    equals()
    方法

    Input.nextLine();
    添加到
    Syso(“你结婚了吗”+FirstName+“(Y或N):”)之后;

    publicstaticvoidmain(字符串[]args){
    试一试{
    扫描仪输入=新扫描仪(System.in);
    //询问用户的年龄、姓名和性别
    系统输出打印(“您的性别(男或女):”;
    字符串性别=Input.nextLine();
    系统输出打印(“名字:”);
    String FirstName=Input.nextLine();
    系统输出打印(“姓氏:”);
    字符串LastName=Input.nextLine();
    系统输出打印(“年龄:”);
    int Age=Input.nextInt();
    如果(年龄>=20){
    系统输出打印(“你结婚了吗”+姓+“(Y或N):”;
    Input.nextLine();
    字符串AreYouMarried=Input.nextLine();
    如果(你结婚了吗?)等于(Y){
    if(性别平等(“M”)){
    System.out.println(“那么我就叫你先生”+FirstName+“+LastName+”);
    }else if(性别平等(“F”)){
    System.out.println(“那么我就叫你女士”+FirstName+“+LastName+”);
    }
    }否则{
    System.out.println(“XYZ msg”);
    }
    }
    if(年龄<20岁){
    System.out.print(“那么我就叫你“+名字+”+“+姓氏+”);
    }
    }捕获(例外e){
    //TODO:处理异常
    e、 printStackTrace();
    }
    }
    
    发生这种情况的原因是,在您的原因中,nextInt()无法读取最后按下的字符,请输入='\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
    
    班级性别{
    公共静态void main(字符串[]args){
    扫描仪输入=新扫描仪(System.in);
    //询问用户的年龄、姓名和性别
    系统输出打印(“您的性别(男或女):”;
    字符串性别=Input.nextLine();
    系统输出打印(“名字:”);
    String FirstName=Input.nextLine();
    系统输出打印(“姓氏:”);
    字符串LastName=Input.nextLine();
    系统输出打印(“年龄:”);
    int Age=Input.nextInt();
    如果(年龄>=20){
    系统输出打印(“你结婚了吗”+姓+“(Y或N):”;
    Input.nextLine();
    
    String AreYouMarried=Input.nextLine();//什么是“不起作用”以及哪一个是28?欢迎使用SO。请指出问题区域。哪一行是第20行??程序到达代码的那一部分时就会停止工作。@JamesNelson代码的哪一部分?@JamesNelson它不会停止工作,只是你的字符串
    =
    比较(以您编码它们的方式)可能都会失败。这可能是真的,但该程序甚至还没有达到这一步。它不接受输入。因此,在一些令人讨厌的角落案例中,你在教堂/登记处或离婚法庭运行该程序。@JamesNelson不要混合给nextLine和nextLine打电话。这会导致这个问题。请只使用其中一种方法。E.g.仅使用nextLine。@JamesNelson当您以这种方式查看时,编程过程本身就是关于“讨厌的”角落案例:)它不能解决问题。如果你运行这个程序可能会有帮助。@JamesNelson你能告诉我问题吗?你的代码在我这边运行得很好。它可能是我正在使用的IDE,你在使用什么?我在使用eclipse,但这不是IDE问题。请告诉我你在输入什么,如果你把它设为
    N
    ,它将不会显示任何内容,因为你不使用它对于
    M
    的andled条件,我曾经这样比较过字符串,这种情况在某种程度上有所不同吗?@JamesNelson它不是低效的,而是有缺陷的。
    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