【问题标题】:Code is not printing results代码不打印结果
【发布时间】:2017-01-03 20:13:55
【问题描述】:

我收到输入整数的提示,但之后就没有了。有人能告诉我为什么我的结果没有打印出来吗?

import java.util.Scanner;

public class ChapterThreeQuiz {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);

        System.out.print("Enter a three-digit integer: ");
        double answer = input.nextDouble();


        double x = input.nextDouble();
        double y = input.nextDouble();
        double z = input.nextDouble();


        if (x == z && y == y && z == x)
            System.out.println(answer + " is a palindrome! ");
        else
            System.out.println(answer + " is not a palindrome");
    }
}

【问题讨论】:

  • 它正在等待输入。
  • 我想知道你什么时候认为y == y 是假的。
  • 你为什么要读取整数并分配给双精度数?
  • 另外,x == z && z == x 等价于x == z,因为== 操作是commutative。由于y == y 总是true,所以整个条件可以简化为if (x == z) ...
  • @shmosel 如果你输入NaN

标签: java input output


【解决方案1】:
double answer = input.nextDouble();
double x = input.nextDouble();
double y = input.nextDouble();
double z = input.nextDouble();

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

【讨论】:

  • 如何编写逻辑来对每个整数进行比较?
  • 首先,您需要实际使用整数,而不是双精度数。完成此操作后,您可以使用 % 和 / 运算符来获取数字。您是否熟悉 Java 中的整数除法以及模运算符的工作原理?
  • 我是新手,但我知道“/”除而“%”给出余数。
【解决方案2】:

正如其他人所提到的,您 a) 使用双打和 b) 试图读取太多数字:

import java.util.Scanner;

public class ChapterThreeQuiz {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a three-digit integer: ");

         // Read an int
        int answer = 0;
        try {
            answer = input.nextInt();
        }
        catch (InputMismatchException ex) {
            // Handle error
        }

        // Make sure it's 3 digits
        if (answer < 100 || answer >= 1000) {
           // Do something with bad input
        }
        else {
            // Just need to check if first and third digits are equal.
            // Get those values using integer math
            int digit1 = answer / 100;
            int digit3 = answer % 10;
            if (digit1 == digit3) {
                System.out.println(answer + " is a palindrome! ");
            }
            else {
                System.out.println(answer + " is not a palindrome");
            }
        }
    }
}

【讨论】:

  • 谢谢。我试图理解数学部分。 181 (3 digit = 1) 用什么除数得到余数 10?
  • 181 % 10 = 1 因为“181 / 10 = 18 余数为 1”。这就是取模的工作原理——它返回余数。
【解决方案3】:
import java.util.*;
class Palindrome
{
   public static void main(String args[])
   {
      String original, reverse = "";
      Scanner in = new Scanner(System.in);

      System.out.print("Enter a string : ");
      original = in.nextLine();

      int length = original.length();

      for ( int i = length - 1; i >= 0; i-- )
         reverse = reverse + original.charAt(i);

      if (original.equals(reverse))
         System.out.println("Entered string is a palindrome.");
      else
         System.out.println("Entered string is not a palindrome.");
    }
}

/*
OUTPUT:
Enter a string : MADAM
Entered string is a palindrome.

Enter a string : 15351
Entered string is a palindrome.
*/

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-16
    • 2023-02-06
    • 2019-01-07
    • 1970-01-01
    • 2022-12-10
    • 2019-03-11
    相关资源
    最近更新 更多