如何控制用户输入

发布于 2024-10-08 02:26:29 字数 550 浏览 0 评论 0原文

我的程序首先显示两个选项,并要求用户选择一个选项,问题是我的程序忽略扫描仪的输入,即使条件为真也不会进入块,所以我的问题是什么!

import java.util.Scanner;

public class TestS {

    public static void main(String[] args) {

        Scanner kb=new Scanner(System.in);
        String s;
        System.out.println("Choose an operation to apply :\n1.Een\n2.De");
        s=kb.nextLine();
        System.out.println(s);
        if(s=="1")
        {
            System.out.print(5454545);
        }

        else if(s=="2")
        {
            System.out.print(2);
        }


    }
}

my program first shows two options and asks the user to choose one choice the problem is my program ignores the input of the scanner and doesn't enter the block even if the condition is true so what the problem i !!!

import java.util.Scanner;

public class TestS {

    public static void main(String[] args) {

        Scanner kb=new Scanner(System.in);
        String s;
        System.out.println("Choose an operation to apply :\n1.Een\n2.De");
        s=kb.nextLine();
        System.out.println(s);
        if(s=="1")
        {
            System.out.print(5454545);
        }

        else if(s=="2")
        {
            System.out.print(2);
        }


    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

給妳壹絲溫柔 2024-10-15 02:26:29

切勿使用 == 来比较字符串。使用 .equals 方法(或者有时使用 .equalsIgnoreCase 方法):

if(s.equals("1"))
    // ...

由于您使用的是扫描仪并且似乎需要数字作为输入,因此有更好的方法:

import java.util.Scanner;

public class Test {

    public static void main(String[] args) {

        Scanner kb = new Scanner(System.in);

        System.out.println("Choose an operation to apply :\n1.Een\n2.De");

        while (!kb.hasNextInt())
            System.out.println(kb.next() + " is not a number. Try again...");

        int choice = kb.nextInt();
        System.out.println(choice);

        switch (choice) {
        case 1: System.out.print(5454545); break;
        case 2: System.out.print(2); break;
        default: System.out.println("Invalid choice."); break;
        }
    }
}

You should never compare strings using ==. Use the .equals method (or sometimes the .equalsIgnoreCase method):

if(s.equals("1"))
    // ...

Since you're using a scanner and you seem to require numbers as input, there is a better way though:

import java.util.Scanner;

public class Test {

    public static void main(String[] args) {

        Scanner kb = new Scanner(System.in);

        System.out.println("Choose an operation to apply :\n1.Een\n2.De");

        while (!kb.hasNextInt())
            System.out.println(kb.next() + " is not a number. Try again...");

        int choice = kb.nextInt();
        System.out.println(choice);

        switch (choice) {
        case 1: System.out.print(5454545); break;
        case 2: System.out.print(2); break;
        default: System.out.println("Invalid choice."); break;
        }
    }
}
晨曦慕雪 2024-10-15 02:26:29

问题在于等于运算符。请记住,== 比较内存引用而不是对象值。我们知道 String 有一个字符串池,但在本例中并没有使用它。

尝试使用 .equals() 代替 ==

The problem is the equals operator. Remember that == compare memory references and not object values. We know that String have a String pool, but in this case it's not used.

try to use .equals() instead ==

天邊彩虹 2024-10-15 02:26:29

改变你如果

if(s == null ? "1" == null : s.equals("1"))
{
    System.out.print(5454545);
}

change you if

if(s == null ? "1" == null : s.equals("1"))
{
    System.out.print(5454545);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文