Java: 省略System.out

问题:

每次都需要输入 System.out.println() 很麻烦,有没有简化的办法呢?


解决办法:

添加:import static java.lang.System.out;

使用方法:

<span style="font-size:18px;">out.println("Hello, java");</span>


这里用到static import的概念, static import 用于导入class的静态方法

http://blog.sina.com.cn/s/blog_625651900100kwul.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
import java.util.ArrayList;import java.util.Scanner;class Clothes { private String name; private String color; private int size; public Clothes(String name, String color, int size) { this.name = name; this.color = color; this.size = size; } public String getName() { return name; } public String getColor() { return color; } public int getSize() { return size; }}class Wardrobe { private ArrayList<Clothes> clothesList = new ArrayList<>(); public void addClothes(Clothes clothes) { clothesList.add(clothes); } public void removeClothes(int index) { clothesList.remove(index); } public void listClothes() { for (int i = 0; i < clothesList.size(); i++) { Clothes clothes = clothesList.get(i); System.out.println((i + 1) + ". " + clothes.getName() + " (" + clothes.getColor() + ", " + clothes.getSize() + ")"); } }}public class WardrobeManagementSystem { public static void main(String[] args) { Wardrobe wardrobe = new Wardrobe(); Scanner scanner = new Scanner(System.in); while (true) { System.out.println("请选择操作:"); System.out.println("1. 添加衣物"); System.out.println("2. 删除衣物"); System.out.println("3. 查看衣柜中的衣物"); System.out.println("4. 退出"); int choice = scanner.nextInt(); scanner.nextLine(); switch (choice) { case 1: System.out.println("请输入衣物名称:"); String name = scanner.nextLine(); System.out.println("请输入衣物颜色:"); String color = scanner.nextLine(); System.out.println("请输入衣物尺寸:"); int size = scanner.nextInt(); scanner.nextLine(); Clothes clothes = new Clothes(name, color, size); wardrobe.addClothes(clothes); System.out.println("衣物添加成功!"); break; case 2: System.out.println("请输入要删除的衣物编号:"); int index = scanner.nextInt() - 1; scanner.nextLine(); wardrobe.removeClothes(index); System.out.println("衣物删除成功!"); break; case 3: wardrobe.listClothes(); break; case 4: System.out.println("程序已退出!"); System.exit(0); default: System.out.println("输入有误,请重新输入!"); break; } } }}改进代码让原衣柜就有衣服怎么在wardrobe 添加默认衣服
05-31
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值