我的代码确定 3 位回文无限询问号码
My code to determine 3 digit palindrome infinitely asking for number
我必须编写一个代码来确定一个三位数是否为回文(即 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
问题出在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 位数字创建自定义算法?
and for some reason the program is infinitely asking for input. I cannot seem to figure out why
如果已经输入了还是提示输入,是换行缓冲区的问题。
尝试更改:
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
}
我必须编写一个代码来确定一个三位数是否为回文(即 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
问题出在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 位数字创建自定义算法?
and for some reason the program is infinitely asking for input. I cannot seem to figure out why
如果已经输入了还是提示输入,是换行缓冲区的问题。
尝试更改:
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
}