代码不是打印结果

时间:2017-01-03 20:13:55

标签: java input output

我收到输入整数的提示,但之后没有任何内容。有人能告诉我为什么我的结果不打印?

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");
    }
}

3 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

正如其他人所说,你是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");
            }
        }
    }
}

答案 2 :(得分:0)

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.Hope这段代码有帮助!