如何获得正确的消息显示?

时间:2019-10-07 20:50:11

标签: java if-statement

在某些情况下,我无法显示正确的消息。我的程序的一部分检查用户输入的内容是否具有偶数或奇数数字,并应针对每种情况显示相应的消息。我的问题是,如果没有输入偶数/奇数,我想显示消息“没有偶数/奇数”。

import java.util.Scanner;   // Needed for Scanner class
import java.io.*;           // Needed for File I/O classes

public class Reverse {
    public static void main(String[] args) throws IOException {
        Scanner keyboard = new Scanner(System.in);
        String Continue = "yes";
        int num;

        //creates the file name
        File fileWR = new File("outDataFile.txt");
        //creates the file object
        fileWR.createNewFile();
        //file scanner
        BufferedWriter output = new BufferedWriter(new FileWriter(fileWR, true));

        while (Continue.equals("yes")) {
            System.out.print("Enter an integer number greater than 0 :");
            num = keyboard.nextInt();
            keyboard.nextLine();
                if (fileWR.exists())
                {
                    validate(num, output);
                }
                else
                {
                    fileWR.createNewFile();
                }

                //option if the user wants to continue
                System.out.println("Do you wish to continue?(yes or no): ");
                Continue = keyboard.nextLine();
            }
        output.close();
    }


    public static void validate(int num, BufferedWriter output) throws IOException {
        Scanner keyboard = new Scanner(System.in);
        if (num > 0)
        {
            System.out.print("The original numbers are " + num +"\n");
            output.write("\r\nThe original numbers are " + num +"\r\n");
            reverse (num, output);
            even (num, output);
            odd(num, output);
        }
        else if (num <= 0)
        {
            System.out.print("Please enter a positive integer");
            num = keyboard.nextInt();
        }
        else
        {
            System.out.print("Error, please enter a vaid positive number");
            num = keyboard.nextInt();
        }
    }// end of public static void validate

    public static void reverse(int num, BufferedWriter output) throws IOException {                                                                                                         
        String input = String.valueOf(num);         //must output result within the void method for it to count as a void method
        String result = "";                         //otherwise, you cannot output it in the main method.

        for (int i = (input.length() - 1); i >= 0; i--) 
           {
               result = result + input.charAt(i)+' ';
           }
           result = "the number reversed "+ result +"\r\n";
           System.out.print(result);
           output.write(result);
    }// end of public static void reverse

    public static void even(int num, BufferedWriter output) throws IOException {
        String input = String.valueOf(num);
        String result = "";

           for (int i = 0; (i < input.length()); i++) 
           {
               if (Character.getNumericValue(input.charAt(i)) % 2 == 0) 
                   result = result + input.charAt(i) + ' ';
               else if (Character.getNumericValue(input.charAt(i)) % 2 != 0)
               {
                   result = "There are no even digits";
               }
           }
           result = "the even digits are "+ result +"\r\n";
           System.out.print(result);
           output.write(result);
    }// end of public static void even

    public static void odd(int num, BufferedWriter output) throws IOException {
        String input = String.valueOf(num);
        String result = "";

           for (int i = 0; (i < input.length()); i++) 
           {
               if (Character.getNumericValue(input.charAt(i)) % 2 == 1)
               {
                   result = result + input.charAt(i) + ' ';
               }
               else if (Character.getNumericValue(input.charAt(i)) % 2 != 1)
               {
                   result = "There are no odd digits";
               }
           }
           result = "the odd digits are "+ result +"\r\n";
           System.out.print(result);
           output.write(result);
           System.out.print("------------------------\n");
           output.write("------------------------\n");

    }// end of public static void odd
}

1 个答案:

答案 0 :(得分:0)

在偶数和奇数函数中,您都在变量结果中建立了一个由匹配数字组成的字符串。您必须先查看所有数字,才能知道没有匹配的数字。将even()中的逻辑更改为此:

   String result = "";
   for (int i = 0; (i < input.length()); i++) 
   {
       if (Character.getNumericValue(input.charAt(i)) % 2 == 0)  {
               result = result + input.charAt(i) + ' ';
       }
   }
   if (result == "") {
       result = "There are no even digits";
   } else {
       result = "the even digits are "+ result +"\r\n";
   }