我必须制作一个代码来确定一个三位数是否是回文(即121从前到后它是相同的),并且由于某种原因,程序无限地要求输入。我似乎无法弄清楚为什么。任何帮助将不胜感激,谢谢!
代码:
/* Class: CS1301
* Section: 9:30
* Term: Fall 2015
* Name: Matthew Woolridge
* Instructor: Mr. Robert Thorsen
* Assignment: 3
* Program: 4
* ProgramName: PalindromeNumber
* Purpose: To tell the user if a number is palindrome
* Operation: The information to be numbers are statically instantiated in the code and
* the table is output to the screen.
* Input(s): The user inputs a three digit number
* Output(s): The output is whether or not the number is palindrome
* Methodology: This program will use selections, math statements and print to determine and tell the user if a number is palindrome
*
*/
import java.util.Scanner;
public class PalindromeNumber
{
public static void main (String[] main)
{
/******************************************************************************
Declarations Section
******************************************************************************/
/****************************CONSTANTS********************************/
// Defined variables, must be integers to get a remainder
int reverse=0;
int palindrome;
int remainder;
int num=0;
Scanner scan = new Scanner (System.in); //Scanner utility initialization
/******************************************************************************
Inputs Section
******************************************************************************/
System.out.print("Please input a three digit number: ");
palindrome = scan.nextInt(); // The following lines prompt the user to input a three digit number
/****************************variables********************************/
// Variables are not in this section //
/******************************************************************************
Processing Section
******************************************************************************/
while (palindrome > 0)
{
remainder = palindrome % 10; //Uses the remainder function to find the remainder of the number using 10
reverse = reverse * 10 + remainder; // Calculates the reverse by multiplying the initial reverse value of 0 by 10 and dividing by ten to get the number of reverse
num = palindrome / 10; // Divides the palindrome by ten to get the number minus the remainder
}
if (num == reverse) // If the num (copied from palindrome) is equal to the reverse the number is a palindrome
{
System.out.println(palindrome + " is a palindrome.");
}
else
{
System.out.println(palindrome + " is not a palindrome."); // If and else statements determine if palindrome = reverse statement is true or false and print accordingly.
} // Closes while and if statement
/******************************************************************************
Outputs Section
******************************************************************************/
//Outputs are in processing//
} // Ends string
} // Ends program
答案 0 :(得分:1)
问题在于while循环
while (palindrome > 0)
{
remainder = palindrome % 10; //Uses the remainder function to find the remainder of the number using 10
reverse = reverse * 10 + remainder; // Calculates the reverse by multiplying the initial reverse value of 0 by 10 and dividing by ten to get the number of reverse
num = palindrome / 10; // Divides the palindrome by ten to get the number minus the remainder
}
palindrome
永远不会改变,所以循环会永远重复。
如果给出该数字将有3位数,为什么不为3位数字创建自定义算法呢?
答案 1 :(得分:0)
由于某种原因,程序无限期地要求输入。我似乎无法弄明白为什么
即使已经给出输入,它仍然提示输入,它是由换行符缓冲区引起的。
尝试更改:
palindrome = scan.nextInt();
要强>
palindrome = Integer.parseInt(scan.nextLine());
顺便说一句,如果是3位数的输入。检查它是否是回文就像:
public boolean is3Digit_Palindrome(int num){
return (num /100) == (num % 10); //Check first digit == last digit
}