我的Java程序只输出两个答案,另外两个没有显示

时间:2015-02-15 05:03:30

标签: java for-loop methods palindrome perfect-numbers

我的程序应该向后拼写输入的单词,然后按下一行并回答输入的单词是否为回文结构。然后它将采用附带的数字,如果它是素数则输出然后输出,如果它是完美的或不是。

我在命令提示符下的输出仅显示回文部分和素数部分。我也不太明白为什么。

以下是完整的代码,这是一项家庭作业,因此您可以忽略这些评论。

public static void main (String[] args) throws Exception
{
    if (args.length == 0 ) // i.e  If you did not type anything after "java Lab5" on command line
    {
        System.out.println("FATAL ERROR: Must type a filename on cmd line\n" + 
                           "Like this ->   C:\\Users\\tim\\Desktop>java Lab5 words1.txt");
        System.exit(0);  //ABORT program. Make user try again with a filename this time
    }

    Scanner infile = new Scanner( new File(args[0]) );

    while ( infile.hasNext() )   
    {
        String word = infile.next(); // grab next token (word) from file

        // 1st method you must write below main:  printWordBackwards
        printWordBackwards( word );  // if word is "foobar"  your method prints "baroof"

        // 2nd method you must write below main:  isPalindrome

        if ( isPalindrome( word ) )
            System.out.println( word + " IS A PALINDROME" ); // DO NOT MODIFY/REMOVE
        else
            System.out.println( word + " NOT A PALINDROME" ); // DO NOT MODIFY/REMOVE

        // 3rd method you must write below main:  isPrime
        // NOTE WE ARE ASSUMING THAT EVERY OTHER TOKEN IN FILE IS AN INT TOKEN

        int number = infile.nextInt();  // grab next token and convert to int

        if ( isPrime( number  ) )
            System.out.println( number + " IS PRIME" ); // DO NOT MODIFY/REMOVE
        else
            System.out.println( number + " NOT PRIME" ); // DO NOT MODIFY/REMOVE            

        // 4th method you must write below main:  isPerfect
        // NOTE WE ARE ASSUMING THAT EVERY OTHER TOKEN IN FILE IS AN INT TOKEN

        if ( isPerfect( number  ) )
            System.out.println( number + " IS PERFECT" ); // DO NOT MODIFY/REMOVE
        else
            System.out.println( number + " NOT PERFECT" ); // DO NOT MODIFY/REMOVE

        System.out.println();
    } // END WHILE

    infile.close();  // WE ARE DONE WITH THE INPUT FILE./ CLOSE IT

} // END MAIN   

// WRITE YOUR FOUR METHOD DEFINITIONS DOWN HERE 

public static void printWordBackwards (String s)
{
    for (int i = s.length()-1 ; i<= 0 ; i--)
    {
        System.out.print(s.charAt(i));
    }
    System.out.println();
}

public static boolean isPalindrome (String s)
{
    int i = 0;
    int j = s.length() - 1;
    while (j > i)
    {
        if (s.charAt(i) != s.charAt(j))
        {
            return false;
        }
        ++i;
        --j;
    }
return true;
}

public static boolean isPrime (int i)
{
    for (int j = 2; j <= i/2; j++)
    {
        if (i % j == 0)
        {
            return false;
        }
    }
    return true;
}

public static boolean isPerfect (int i)
{
    for (int k = 1; i > 0; i++)
    {
        i -= k;
    }
    if (i == 0)
    {
        return true;
    }
return false;
}

} // END LAB5 CLASS

2 个答案:

答案 0 :(得分:3)

如何解决完美数字的想法在原始和“修复”中是不正确的。

public static void printWordBackwards (String s)
{
//Please note that this is greater than or equal to not less than or equal to.
    //for (int i = s.length()-1 ; i<= 0 ; i--)  ORIGINAL
    for (int i = s.length()-1 ; i>= 0 ; i--)
    {
        System.out.print(s.charAt(i));
    }
    System.out.println();
}
public static boolean isPerfect (int i)
{
//This is an infinite loop.  K is always 1, and i subtracts k and adds 1 to it meaning it will never end it should be as follows below.
// Please note that this does not mean it's a perfect answer, I simply fixed his original mistake
//In number theory, a perfect number is a positive integer that is equal to the sum of its proper positive divisors,
//for (int k = 1; i > 0; i++)  ORIGINAL
for (int k = 1; i > 0; k++)
{
    i -= k;
}
if (i == 0)
{
    return true;
    }
return false;
}

//More correct isPerfect using the http://en.wikipedia.org/wiki/Perfect_number document 
//and the statement that all even perfect numbers are of the form 
//2^(p-1)*((2^p)-1)
// and that odd perfect numbers are either rare or don't exist.
public static boolean betterIsPerfect(int i)
{
    return i==Math.pow(2, i-1)*(Math.pow(2,p)-1);
}

答案 1 :(得分:1)

for (int i = s.length()-1 ; i<= 0 ; i--)
{
    System.out.print(s.charAt(i));
}

这个循环有什么作用?我知道,我问的是一个问题,而不是回答问题,但我想让你自己帮忙。