[英]How to get the right message to display?
我在為特定情況顯示正確消息時遇到問題。 我的程序的一部分檢查用戶輸入的內容是偶數還是奇數,並且應該為每種情況顯示相應的消息。 我的問題是,如果沒有輸入偶數/奇數,我想顯示消息“沒有偶數/奇數”。
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
}
在偶數和奇數函數中,您都在變量結果中構建一串匹配的數字。 在您查看所有數字之前,您無法知道沒有匹配的數字。 將 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";
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.