java代码没有打印结果

vx6bjr1n  于 2021-07-09  发布在  Java
关注(0)|答案(3)|浏览(438)

我得到的提示是输入一个整数,但之后什么都没有。有人能告诉我为什么我的结果没有打印出来吗?

  1. import java.util.Scanner;
  2. public class ChapterThreeQuiz {
  3. public static void main(String[] args) {
  4. // TODO Auto-generated method stub
  5. Scanner input = new Scanner(System.in);
  6. System.out.print("Enter a three-digit integer: ");
  7. double answer = input.nextDouble();
  8. double x = input.nextDouble();
  9. double y = input.nextDouble();
  10. double z = input.nextDouble();
  11. if (x == z && y == y && z == x)
  12. System.out.println(answer + " is a palindrome! ");
  13. else
  14. System.out.println(answer + " is not a palindrome");
  15. }
  16. }
fdx2calv

fdx2calv1#

  1. import java.util.*;
  2. class Palindrome
  3. {
  4. public static void main(String args[])
  5. {
  6. String original, reverse = "";
  7. Scanner in = new Scanner(System.in);
  8. System.out.print("Enter a string : ");
  9. original = in.nextLine();
  10. int length = original.length();
  11. for ( int i = length - 1; i >= 0; i-- )
  12. reverse = reverse + original.charAt(i);
  13. if (original.equals(reverse))
  14. System.out.println("Entered string is a palindrome.");
  15. else
  16. System.out.println("Entered string is not a palindrome.");
  17. }
  18. }
  19. /*
  20. OUTPUT:
  21. Enter a string : MADAM
  22. Entered string is a palindrome.
  23. Enter a string : 15351
  24. Entered string is a palindrome.
  25. * /

你在这里使用了错误的逻辑。如果你想检查回文,你不应该使用double。希望这段代码有帮助!

展开查看全部
ajsxfq5m

ajsxfq5m2#

  1. double answer = input.nextDouble();
  2. double x = input.nextDouble();
  3. double y = input.nextDouble();
  4. double z = input.nextDouble();

您的代码正在等待4个不同的输入。如果你全部输入4,它会运行-但是你的逻辑显然有问题。

uyhoqukh

uyhoqukh3#

正如其他人提到的,你是a)与双打和b)试图读取太多的数字:

  1. import java.util.Scanner;
  2. public class ChapterThreeQuiz {
  3. public static void main(String[] args) {
  4. Scanner input = new Scanner(System.in);
  5. System.out.print("Enter a three-digit integer: ");
  6. // Read an int
  7. int answer = 0;
  8. try {
  9. answer = input.nextInt();
  10. }
  11. catch (InputMismatchException ex) {
  12. // Handle error
  13. }
  14. // Make sure it's 3 digits
  15. if (answer < 100 || answer >= 1000) {
  16. // Do something with bad input
  17. }
  18. else {
  19. // Just need to check if first and third digits are equal.
  20. // Get those values using integer math
  21. int digit1 = answer / 100;
  22. int digit3 = answer % 10;
  23. if (digit1 == digit3) {
  24. System.out.println(answer + " is a palindrome! ");
  25. }
  26. else {
  27. System.out.println(answer + " is not a palindrome");
  28. }
  29. }
  30. }
  31. }
展开查看全部

相关问题