Java 我想获取字符串输入以继续条件分支

Java 我想获取字符串输入以继续条件分支,java,string,Java,String,我想从键盘上输入字符串。但是当我运行这个程序时,没有选择接受字符串输入。我不知道我的错是什么 我的代码是: import java.util.Scanner; public class Input_Program { public static void main(String args[]) { Scanner in = new Scanner(System.in); int a,b; System.out.println("Enter the first number

我想从键盘上输入字符串。但是当我运行这个程序时,没有选择接受字符串输入。我不知道我的错是什么

我的代码是:

import java.util.Scanner;
public class Input_Program {
public static void main(String args[])
{
    Scanner in = new Scanner(System.in);
    int a,b;
    System.out.println("Enter the first number :");
    a=in.nextInt();
    System.out.println("Enter the second number :");
    b=in.nextInt();


    System.out.println("Value of first number:"+a);
    System.out.println("Value of second number:"+b);

    System.out.println("Do you want Add two numbers");
    System.out.println("To Continue: Type Yes");

    String S=in.nextLine();

    if("Yes".equals(S)||"yes".equals(S))
    {   
    int sum=a+b;
    System.out.println("Summation :"+sum);  
    }

}  
}
我想从这个代码中获取一个输入。但它不起作用

    String S=in.nextLine();

    if("Yes".equals(S)||"yes".equals(S))
    {   
    int sum=a+b;
    System.out.println("Summation :"+sum);  
    }  
该代码的结果是:

run:
Enter the first number :
2
Enter the second number :
3
Value of second number:3
Do you want the Summation of two numbers
To Continue: Type Yes
生成成功(总时间:9秒)

1)nextLine()根据“将此扫描仪推进当前行,并返回跳过的输入”,这不是您要查找的行为

2) 您似乎是java新手,并且有命名约定。请不要以大写字母开头变量名

3) 我修正了你的缩进,并且对输入进行了广义化,以接受任何以y开头的内容。将来一定要用这种方式编写代码,因为这样更容易判断if语句和循环等中的代码行

import java.util.Scanner;
public class Input_Program {
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter the first number :");
        int a = in.nextInt();
        System.out.println("Enter the second number :");
        int b = in.nextInt();


        System.out.println("Value of first number:" + a);
        System.out.println("Value of second number:" + b);

        System.out.println("Do you want Add two numbers");
        System.out.println("To Continue: Type Yes");

        String s = in.next();

        if(s.toLowerCase().contains("y")){   
            int sum=a+b;
            System.out.println("Summation :"+sum);  
        }

    }  
}
1) nextLine()根据“将此扫描仪推进当前行并返回跳过的输入”,这不是您要查找的行为

2) 您似乎是java新手,并且有命名约定。请不要以大写字母开头变量名

3) 我修正了你的缩进,并且对输入进行了广义化,以接受任何以y开头的内容。将来一定要用这种方式编写代码,因为这样更容易判断if语句和循环等中的代码行

import java.util.Scanner;
public class Input_Program {
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter the first number :");
        int a = in.nextInt();
        System.out.println("Enter the second number :");
        int b = in.nextInt();


        System.out.println("Value of first number:" + a);
        System.out.println("Value of second number:" + b);

        System.out.println("Do you want Add two numbers");
        System.out.println("To Continue: Type Yes");

        String s = in.next();

        if(s.toLowerCase().contains("y")){   
            int sum=a+b;
            System.out.println("Summation :"+sum);  
        }

    }  
}

请尝试next()而不是nextLine()。根据扫描器文档,nextLine()“使扫描器前进到当前行,并返回跳过的输入。”它还返回相同的结果。我运行了它,它对meThis有效,与此重复。您应该阅读它,因为它会对您有所帮助。可能会重复使用try next()而不是nextLine()。根据扫描器文档,nextLine()“使扫描器前进到当前行,并返回跳过的输入。”它还返回相同的结果。我运行了它,它对meThis有效,与此重复。您应该阅读它,因为它会对您有所帮助。所有编程语言都有可能重复2种命名约定。当然,如果(s.toLowerCase().包含(“y”),请您解释这一行!s、 toLowerCase()接受字符串s并将其转换为所有小写字母。然后,调用.contains(“y”)意味着如果字符串中包含字母y,则返回true。但是,如果您想更具体一些,请尝试.startsWith('y'),如果字符串以字母y开头,则返回true。如果您想了解有关字符串的更多信息,请查看此页面:一般来说,如果您想知道如何使用某个内容,请搜索它,然后将word文档添加到搜索中。oracle页面将告诉您它是什么,以及可以调用哪些方法。我知道了。谢谢#2所有编程语言的命名约定都是相同的。如果(s.toLowerCase().包含(“y”),请您解释一下这一行好吗!s、 toLowerCase()接受字符串s并将其转换为所有小写字母。然后,调用.contains(“y”)意味着如果字符串中包含字母y,则返回true。但是,如果您想更具体一些,请尝试.startsWith('y'),如果字符串以字母y开头,则返回true。如果您想了解有关字符串的更多信息,请查看此页面:一般来说,如果您想知道如何使用某个内容,请搜索它,然后将word文档添加到搜索中。oracle页面将告诉您它是什么,以及可以调用哪些方法。我知道了。谢谢