Se ha denunciado esta presentación.
Se está descargando tu SlideShare. ×

Please help me on this problem with only using import java-util-Scanne.docx

Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio

Eche un vistazo a continuación

1 de 3 Anuncio

Please help me on this problem with only using import java-util-Scanne.docx

Descargar para leer sin conexión

Please help me on this problem with only using import java.util.Scanner;.
I\'m not certain on how to approach this problem as I keep running to multiple errors. Thanks.
Solution
import java.util.Scanner;
public class MenuDriven {
public static void main(String args[]) {
int startingPoint = 1, startingPrime = 2, ch, n;
final int printPerLine = 10;
Scanner s = new Scanner(System.in);
System.out.println(\"enter the choice among the menu\ \");
System.out.println(\"1.Display prime palindromes 2.display mirrored primes 3.exit prgrm\ \");
ch = s.nextInt();
switch (ch) {
case 1:
IsItPrime(startingPrime);
IsItPalin(startingPrime);
System.out.println(\"enter the number upto which prim pal are to be displayed\ \");
n = s.nextInt();
while (startingPoint <= n) {
if (IsItPrime(startingPrime) && IsItPalin(startingPrime)) {
System.out.print(startingPrime + \" \");
if (startingPoint % printPerLine == 0) {
System.out.println();
}
startingPoint++;
}
startingPrime++;
}
break;
case 2:
startingPoint = 1;
startingPrime = 2;
System.out.println(\"enter the number upto which prim pal are to be displayed\ \");
n = s.nextInt();
while (startingPoint <= n) {
if (IsItPrime(startingPrime) && !IsItPalin(startingPrime)) {
int rev=reverseNumber(startingPrime);
if(IsItPrime(rev)){
System.out.print(startingPrime + \" \");
}
if (startingPoint % printPerLine == 0) {
System.out.println();
}
startingPoint++;
}
startingPrime++;
}
break;
case 3: exit(0);
}
}
public static boolean IsItPrime(int sPrime) {
if (sPrime % 2 == 0 && sPrime != 2) {
return false;
}
int sqrtPrime = (int) Math.sqrt(sPrime);
for (int i = 3; i <= sqrtPrime; i += 2) {
if (sPrime % i == 0) {
return false;
}
}
return true;
}
public static boolean IsItPalin(int sPrime) {
int sPrimeBackup = sPrime;
int reverse = 0;
while (sPrime > 0) {
reverse = reverse * 10 + sPrime % 10;
sPrime = sPrime / 10;
}
return (sPrimeBackup == reverse);
}

public static int reverseNumber(int number){

int reverse = 0;
while(number != 0){
reverse = (reverse*10)+(number%10);
number = number/10;
}
return reverse;
}
}
.

Please help me on this problem with only using import java.util.Scanner;.
I\'m not certain on how to approach this problem as I keep running to multiple errors. Thanks.
Solution
import java.util.Scanner;
public class MenuDriven {
public static void main(String args[]) {
int startingPoint = 1, startingPrime = 2, ch, n;
final int printPerLine = 10;
Scanner s = new Scanner(System.in);
System.out.println(\"enter the choice among the menu\ \");
System.out.println(\"1.Display prime palindromes 2.display mirrored primes 3.exit prgrm\ \");
ch = s.nextInt();
switch (ch) {
case 1:
IsItPrime(startingPrime);
IsItPalin(startingPrime);
System.out.println(\"enter the number upto which prim pal are to be displayed\ \");
n = s.nextInt();
while (startingPoint <= n) {
if (IsItPrime(startingPrime) && IsItPalin(startingPrime)) {
System.out.print(startingPrime + \" \");
if (startingPoint % printPerLine == 0) {
System.out.println();
}
startingPoint++;
}
startingPrime++;
}
break;
case 2:
startingPoint = 1;
startingPrime = 2;
System.out.println(\"enter the number upto which prim pal are to be displayed\ \");
n = s.nextInt();
while (startingPoint <= n) {
if (IsItPrime(startingPrime) && !IsItPalin(startingPrime)) {
int rev=reverseNumber(startingPrime);
if(IsItPrime(rev)){
System.out.print(startingPrime + \" \");
}
if (startingPoint % printPerLine == 0) {
System.out.println();
}
startingPoint++;
}
startingPrime++;
}
break;
case 3: exit(0);
}
}
public static boolean IsItPrime(int sPrime) {
if (sPrime % 2 == 0 && sPrime != 2) {
return false;
}
int sqrtPrime = (int) Math.sqrt(sPrime);
for (int i = 3; i <= sqrtPrime; i += 2) {
if (sPrime % i == 0) {
return false;
}
}
return true;
}
public static boolean IsItPalin(int sPrime) {
int sPrimeBackup = sPrime;
int reverse = 0;
while (sPrime > 0) {
reverse = reverse * 10 + sPrime % 10;
sPrime = sPrime / 10;
}
return (sPrimeBackup == reverse);
}

public static int reverseNumber(int number){

int reverse = 0;
while(number != 0){
reverse = (reverse*10)+(number%10);
number = number/10;
}
return reverse;
}
}
.

Anuncio
Anuncio

Más Contenido Relacionado

Similares a Please help me on this problem with only using import java-util-Scanne.docx (20)

Más de rtodd19 (20)

Anuncio

Más reciente (20)

Please help me on this problem with only using import java-util-Scanne.docx

  1. 1. Please help me on this problem with only using import java.util.Scanner;. I'm not certain on how to approach this problem as I keep running to multiple errors. Thanks. Solution import java.util.Scanner; public class MenuDriven { public static void main(String args[]) { int startingPoint = 1, startingPrime = 2, ch, n; final int printPerLine = 10; Scanner s = new Scanner(System.in); System.out.println("enter the choice among the menu "); System.out.println("1.Display prime palindromes 2.display mirrored primes 3.exit prgrm "); ch = s.nextInt(); switch (ch) { case 1: IsItPrime(startingPrime); IsItPalin(startingPrime); System.out.println("enter the number upto which prim pal are to be displayed "); n = s.nextInt(); while (startingPoint <= n) { if (IsItPrime(startingPrime) && IsItPalin(startingPrime)) { System.out.print(startingPrime + " "); if (startingPoint % printPerLine == 0) { System.out.println(); } startingPoint++; }
  2. 2. startingPrime++; } break; case 2: startingPoint = 1; startingPrime = 2; System.out.println("enter the number upto which prim pal are to be displayed "); n = s.nextInt(); while (startingPoint <= n) { if (IsItPrime(startingPrime) && !IsItPalin(startingPrime)) { int rev=reverseNumber(startingPrime); if(IsItPrime(rev)){ System.out.print(startingPrime + " "); } if (startingPoint % printPerLine == 0) { System.out.println(); } startingPoint++; } startingPrime++; } break; case 3: exit(0); } } public static boolean IsItPrime(int sPrime) { if (sPrime % 2 == 0 && sPrime != 2) { return false; } int sqrtPrime = (int) Math.sqrt(sPrime); for (int i = 3; i <= sqrtPrime; i += 2) { if (sPrime % i == 0) { return false; } } return true; }
  3. 3. public static boolean IsItPalin(int sPrime) { int sPrimeBackup = sPrime; int reverse = 0; while (sPrime > 0) { reverse = reverse * 10 + sPrime % 10; sPrime = sPrime / 10; } return (sPrimeBackup == reverse); } public static int reverseNumber(int number){ int reverse = 0; while(number != 0){ reverse = (reverse*10)+(number%10); number = number/10; } return reverse; } }

×