gpt4 book ai didi

java - Eclipse 提示以下 String.format 用法

转载 作者:行者123 更新时间:2023-11-30 03:52:46 24 4
gpt4 key购买 nike

该程序由以下两个类组成,并应打印时间。
在类 Melons 中,Eclipse 在方法 String.format 下给我一个错误,我不明白为什么

public class Melons {

private int hour;
private int minute;
private int second;

public void setTime(int h, int m, int s)
{
hour = ((h>=0 || h<24) ? h : 0);
minute = ((m>=0 || m<60) ? m : 0);
second = ((s>=0 || s<24) ? s : 0);
}

public String toString()
{
// Problem here
return String.format("%d:%02d:%02d %s",
((hour==0||hour==12) ? 12 : hour%12),
minute,
second,
(hour<12 ? "AM" : "PM"));
}
}

class Apples
{
public static void main(String args[])
{
Melons melonsObject = new Melons();
System.out.println(melonsObject.toString());
melonsObject.setTime(13, 35, 9);
System.out.println(melonsObject.toString());
}
}

最佳答案

format自 Java 1.5 起一直是 String 的成员方法。确保您使用的是此版本或更高版本,并且类路径上没有其他 String 类。

关于java - Eclipse 提示以下 String.format 用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23971589/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com