Java实现P5712 【深基3.例4】Apples

在这里插入图片描述

import java.io.IOException;
import java.util.Scanner;

public class Main {
    
    
	@SuppressWarnings("resource")
	public static void main(String[] args) throws IOException{
    
    
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		if(n==0||n==1) {
    
    
			System.out.println("Today, I ate "+n+" apple.");
		}else {
    
    
			System.out.println("Today, I ate "+n+" apples.");
		}
	}
}

思路:为减少代码量,因此分为apple是单数还是复数两种情况讨论,利用变量代入求解。

猜你喜欢

转载自blog.csdn.net/jinyeran/article/details/115436289