У меня проблемы с отображением нужного сообщения для определенного случая. Часть моей программы проверяет, имеют ли введенные пользователем четные или нечетные цифры, и должна отображать соответствующее сообщение для каждого случая. Моя проблема в том, что я хочу отобразить сообщение «Нет четных / нечетных чисел», если четные / нечетные числа не были введены.
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
}