【问题标题】:Java Palindrome assignment - boolean always returns False valueJava Palindrome 赋值 - 布尔值总是返回 False 值
【发布时间】:2014-09-25 21:52:41
【问题描述】:

每个人。我的 Java 编程课作业有问题,希望得到您的意见。

pA 方法的赋值是创建一个仅使用 StringCharacter 方法的方法。因此,我编写了以下方法,当我尝试调用方法 pA 时,它会自动返回一个 false 值。我在想我的 for 循环可能有问题,但我不确定,我想我会在这里问你要说什么。

提前感谢您的宝贵时间。

public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in); //Creates Scanner object to take user input

    String pal; //String to contain user input

    boolean test = false; //boolean to test if user input is a palindrome.

    System.out.println("This program determines if your input is a palindrome!");
    System.out.println("Please enter your input to see if it's a palindrome.");
            pal = keyboard.nextLine();

    pA(pal, test);
    if (test == true)
    {
        System.out.println("Congratulations! You have a palindrome!");
    }
    else if (test == false)
    {
            System.out.println("Unfortunately, you do not have a palindrome.");
    }

}

public static boolean pA(String pal, boolean test) //Part A of Assignment 7.
{
    int l = pal.length(); //integer to contain length of string
    int i; //Loop control variable
    test = false;

    for (i = 0; i < l/2; i++) //for loop that tests to see if input is a palindrome.
    {
        if (pal.charAt(i) == pal.charAt(l-i-1)) //Compares character at point i with respective character at the other end of the string.
        {   
            test = true; //Sets boolean variable to true if the if statement yields true.
        } else 
            test = false; //Otherwise, a false value is returned.
             break;
    }
    return test; //Return statement for boolean variable
} //End pA

从这里开始,当我尝试运行程序时,使用输入“tubbut”时收到以下消息:

运行:

此程序确定您的输入是否为回文!

请输入您的输入以查看是否为回文。

管子

很遗憾,你没有回文。

构建成功(总时间:2 秒)

【问题讨论】:

    标签: java boolean palindrome


    【解决方案1】:

    您忽略了调用pA 方法的结果,因此main 中的test 变量保持不变。此外,没有理由将test 传递给pA,因为那样pA 无论如何都只有该值的副本。

    在main中,尝试

    test = pA(pal);
    

    并且在pA中,去掉test参数;您可以将其设为局部变量。

    public static boolean pA(String pal) //Part A of Assignment 7.
    {
       int l = pal.length(); //integer to contain length of string
       int i; //Loop control variable
       boolean test = false;  // *** Now it's a local variable
       // Rest of method is here.
    }
    

    此外,在main 中,test 已经是boolean,因此您无需将其与truefalse 进行比较。你可以替换

    if (test == true)
    {
        System.out.println("Congratulations! You have a palindrome!");
    }
    else if (test == false)
    {
        System.out.println("Unfortunately, you do not have a palindrome.");
    }
    

    if (test)
    {
        System.out.println("Congratulations! You have a palindrome!");
    }
    else
    {
        System.out.println("Unfortunately, you do not have a palindrome.");
    }
    

    【讨论】:

      猜你喜欢
      • 2017-11-23
      • 1970-01-01
      • 2019-06-10
      • 2012-11-16
      • 1970-01-01
      • 2018-11-10
      • 1970-01-01
      • 2017-04-22
      • 1970-01-01
      相关资源
      最近更新 更多