即使有一个带有特定代码的if语句用于我的输入,我的程序也会在我输入输入时停止[关闭]

Posted

技术标签:

【中文标题】即使有一个带有特定代码的if语句用于我的输入,我的程序也会在我输入输入时停止[关闭]【英文标题】:My program just stops when I enter input even though there is an if statement with specific code for my input [closed] 【发布时间】:2022-01-21 15:21:51 【问题描述】:

我刚刚了解了您可以在 java 中使用字符串执行的操作,因此我决定创建一个程序,您可以在其中输入字符串,然后选择如何处理它

package com.company;
import java.util.Scanner;

public class Main 

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

        System.out.println("In this program you will write a piece of text and you can get information about that piece of text, or you can change it");



        System.out.println("What is the piece of text:");
        String text = scanner.nextLine();



        System.out.println("What would you like to do with this text");
        System.out.println("You can check if it is the same as another text");
        System.out.println("You can check it's length");
        System.out.println("You can check what letter is at what index or what is the index of a letter");
        System.out.println("You can check if it's empty");
        System.out.println("You can also change it to all upper case or all lower case");
        System.out.println("You can trim the empty space added");
        System.out.println("and last, you can replace a specific letter with another one\n");

        System.out.println("So what would you like to do:");
        String ans = scanner.nextLine();


        String text2;
        boolean resulta;


    


    public static void main2(Scanner scanner, String text2, String text, boolean resulta, String ans)
        if (ans == "check if it is the same as another text" || ans == "Compare with text" || ans == "compare") 
            System.out.println("What is the other text:");
            text2 = scanner.nextLine();

            resulta = text.equalsIgnoreCase(text2);
            if(resulta == true)
                System.out.println(text+"is the same as"+text2);
            
            else
                System.out.println(text+"is not the same as"+text2);
            

        
    




我测试了我的程序,当我输入“比较”或“与文本比较”时,程序就停止了, 没有错误没有任何东西,它只是停止

【问题讨论】:

你永远不会打电话给main2(...) 另外,不要使用==!= 比较字符串。请改用equals(...)equalsIgnoreCase(...) 方法。了解== 检查两个 对象引用 是否相同,这不是您感兴趣的。另一方面,这些方法检查两个字符串是否以相同的顺序具有相同的字符,这就是这里的重点。 插入一些调试System.out.println()行,这样就可以判断程序在做什么了 【参考方案1】:

根据您所展示的代码,您必须解决一个问题才能让您的程序按预期工作。

第一件事,@Hovercraft 在 cmets 中指出了这一点,你永远不会调用你的 main2 方法,它会执行你期望发生的所有逻辑,但不会因为该方法再次被调用.

为此,您的代码需要如下所示:

package com.company;
import java.util.Scanner;

public class Main 

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

        System.out.println("In this program you will write a piece of text and you can get information about that piece of text, or you can change it");



        System.out.println("What is the piece of text:");
        String text = scanner.nextLine();



        System.out.println("What would you like to do with this text");
        System.out.println("You can check if it is the same as another text");
        System.out.println("You can check it's length");
        System.out.println("You can check what letter is at what index or what is the index of a letter");
        System.out.println("You can check if it's empty");
        System.out.println("You can also change it to all upper case or all lower case");
        System.out.println("You can trim the empty space added");
        System.out.println("and last, you can replace a specific letter with another one\n");

        System.out.println("So what would you like to do:");
        String ans = scanner.nextLine();

        String text2;
        boolean resulta;
        main2(scanner, text2, text, resulta, ans);


    


    public static void main2(Scanner scanner, String text2, String text, boolean resulta, String ans)
        if (ans.equals("check if it is the same as another text") || ans.equals("Compare with text") || ans.equals("compare")) 
            System.out.println("What is the other text:");
            text2 = scanner.nextLine();

            resulta = text.equalsIgnoreCase(text2);
            if(resulta == true)
                System.out.println(text+"is the same as"+text2);
            
            else
                System.out.println(text+"is not the same as"+text2);
            

        
    




这将使它起作用,但是您需要在代码中练习和质疑几件事。首先,为什么要将 text2 和 resulta 作为方法参数发送给 main2 方法?其次,在比较 String 时,请始终使用 .equals 方法,除非您尝试比较 String 的引用,因为您可以拥有两个值相同但指向不同内存位置的 String,并且使用 == 返回的结果将为 false。

我建议从一些教程开始,学习 Java 为您提供的几种机制,以便以更简单和干净的方式完成您正在执行的程序。

不过,希望对你有所帮助!

【讨论】:

以上是关于即使有一个带有特定代码的if语句用于我的输入,我的程序也会在我输入输入时停止[关闭]的主要内容,如果未能解决你的问题,请参考以下文章

嵌套的 if 语句、简洁的代码和 Pythonic,带有控制器/键盘输入

带有嵌套 if/else 语句的 While 循环

非数字路径测试

批处理文件 If 语句导致 CMD 关闭且不起作用

if or/and else if 语句被传递

Rails Simple Form Bootstrap - 显示输入的 Javascript if 语句