我正在做一些java练习,我正在尝试创建一个计数为100的方法,并在每次for循环"循环"时打印该数字.
唯一的例外是,当数是3整除它会打印出"嘶嘶声","嗡嗡"当数是整除5.现在,我有我的方法三种回程类型是会返回一个字符串.但是,错误说我没有返回值.我知道我必须使它返回一个字符串外的for循环,但我有一些麻烦搞清楚应该怎么去回报我想要的价值.
我也知道我可以使用数组甚至arrayList来解决这个问题,但我认为没有它可能,我想尝试这样做.
任何帮助将非常感谢!
这是代码:
package etcOvaningar;
public class ovning1 {
public static String fizz ="Fizz!";
public static String buzz ="Buzz!";
public static String countDown(){
for (int number = 0; number < 100; number++){
if (number%3 == 0){
return fizz;
}
else if (number%5 == 0){
return buzz;
}
else
return String.valueOf(number);
}
//I need to insert a return here I suppose, but I want the correct return from the if and else //statements
}
public static void main(String[] args){ …Run Code Online (Sandbox Code Playgroud) 如标题所示,我试图捕获一个空字符串。我有一个类(我尝试创建的对象的类),当String为null或为空时(使用str.isEmpty检查),该类将引发Exception。当我尝试在另一个类中使用空字符串创建对象时,它会按预期工作并抛出异常。但是,我希望此类捕获该异常并通知用户。但是,即使我尝试编写Catch(Exception exc),它也似乎永远不会捕获异常。现在,我知道null或空字符串不是非法的。但是我的意图是对象类应该做到这一点。相反,似乎catch块根本不在乎。我开始认为我必须创建自己的某种异常类...或者我缺少什么?以下是代码的相关部分:
对象类的构造函数:
public Valueables(String name){
//name.trim().length() == 0
if(name == null || name.isEmpty()){
try {
throw new Exception("Subclasses of Valueables cannot take in an empty String or null value for the \"name\" constructor");
} catch (Exception e) {
e.printStackTrace();
System.exit(2);
}
}
else
this.name = name;
}
Run Code Online (Sandbox Code Playgroud)
另一个类(新的Trinket对象是Valueables的子类。一个带有上面的构造函数代码的对象):
while(loopPassErrorControl == false){
//I don't think the try loop is relevant. But just to make sure...
//Otherwise just ignore it
try{
TrinketForm Tform = new TrinketForm();
answer = JOptionPane.showOptionDialog(ValueablesFrame.this, Tform, "Nytt …Run Code Online (Sandbox Code Playgroud)