java代码没有打印结果

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

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

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

fdx2calv1#

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。希望这段代码有帮助!

ajsxfq5m

ajsxfq5m2#

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

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

uyhoqukh

uyhoqukh3#

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

相关问题