내 교수는 소수 "소수점"을 쓸 수 있도록 지정했습니다. 숫자가 소수 또는 소수 인 경우 입력 한 숫자가 표시되는 곳에 다음 소수를 표시하십시오. 그는 잘못된 입력이 입력되면 오류 메시지를 표시하기를 원합니다. 음의 정수 부분은 간단하지만 문자 입력을 파악할 수는 없다고 생각했습니다. 또는 문자가 숫자가 아닌 경우. 어떻게 숫자가 아닌 입력을 차단합니까?숫자가 아닌 입력을 거부하는 방법은 무엇입니까?
또한 시스템은 3 개의 연속적인 잘못된 입력을 종료해야합니다. 카운터를 어떻게 재설정합니까? 내가 프로그램을 작성한 방법, 사용자가 두 개의 오류를 만들었지 만 다음 오류가 허용되면 다른 오류가 발생합니다. (따라서 연속적이지 않습니다.) 프로그램이 닫힙니다. 이것은 내가 처음으로 프로그래밍하는 과정이므로 잘 알지 못합니다. 어떤 도움이라도 대단히 감사하겠습니다.
또한 스캐너와 두 가지 방법을 사용해야합니다.
/**
*
* @param n
* @return
*/
public static boolean isPrime(int n) {
for (int i = 2; i < n; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
public static int nextPrime(int n) {
n++;
isPrime(n);
while (isPrime(n) == false) {
n++;
isPrime(n);
}
int prime = n;
return prime;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int answer = 2;
int counter = 1;
boolean playAgain = false;
Scanner input = new Scanner(System.in);
Scanner reader = new Scanner(System.in);
Scanner in = new Scanner(System.in);
do {
//ask for input
System.out.print("\nEnter the integer value-> ");
//input answer
int n = input.nextInt();
{
//decide is negative
while (n < 0){
//count counter
counter++;
//give error message
System.out.println("\nInvalid Input! Stike!");
//ask for input
System.out.print("\nEnter the integer value-> ");
//input answer
n = input.nextInt();
//decide is character
// if (n != '.'){
//count counter
// counter++;
//give error message
// System.out.println("\nInvalid Input! Strike!");
// }
//decide if count three errors
if (counter == 3){
//display three errors message
System.out.println("Three Strikes! You're Out!");
//close program
System.exit(0);
}
}
//decide if prime
if (isPrime(n)) {
//display prime answer
System.out.println(n + " Is Prime");
//decide if even
} else {
//display even answer
System.out.println(n + " Is Even");
}
//counter input
n++;
//while input is false
while (isPrime(n) == false) {
n++;
}
//display next prime
System.out.println(n + " Next prime");
{
//ask if you want to continue
System.out.println("\nPlay Again?\n\nEnter 1)Yes or 2)No ");
//input answer
answer = in.nextInt();
//if answer is 1)yes
if (answer == 1) {
playAgain = true;
//display play again and next input
System.out.println("\nPlay Again!");
}
//if answer is no
if (answer == 2) {
playAgain = false;
System.out.println("\nGoodbye!");
//close program
System.exit(0);
}
}
}
} while (playAgain != false);
}
}
나는 동의한다, 간단한 연구는 당신에게이 질문에 대한 답을 주어야한다. – DreadHeadedDeveloper