Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java执行while循环而不是循环_Java_Loops_Do While - Fatal编程技术网

Java执行while循环而不是循环

Java执行while循环而不是循环,java,loops,do-while,Java,Loops,Do While,到目前为止,我在课堂教材、课堂模块和2个多小时的搜索中都读到了,我无法理解为什么我的代码不起作用。 main方法中的do-while循环工作正常,但是我的get方法中的do-while循环没有循环。我输入了错误的号码,我得到了错误信息,然后它没有再次询问号码,而是转到下一个get方法 我希望这是一件我忽略了的简单的事情,但我将非常感谢在这方面能得到的任何帮助 下面是我的getHome方法的代码: public static int getHome() { int homeNum;

到目前为止,我在课堂教材、课堂模块和2个多小时的搜索中都读到了,我无法理解为什么我的代码不起作用。 main方法中的do-while循环工作正常,但是我的get方法中的do-while循环没有循环。我输入了错误的号码,我得到了错误信息,然后它没有再次询问号码,而是转到下一个get方法

我希望这是一件我忽略了的简单的事情,但我将非常感谢在这方面能得到的任何帮助

下面是我的getHome方法的代码:

public static int getHome()
{

    int homeNum;
    String home;
     do
    {
        home = JOptionPane.showInputDialog(null,"Enter 1(apartment), 2(house),"
            + " or 3(dorm).","Dwelling Type", JOptionPane.QUESTION_MESSAGE);
        homeNum = Integer.parseInt(home);

        if(!(homeNum == 1) && !(homeNum == 2) && !(homeNum == 3))
        {    
            JOptionPane.showMessageDialog(null, "The value for dwelling type "
                + "must be 1(apartment), 2(house), or 3(dorm)", "Dwelling"
                        + "Type Error", JOptionPane.ERROR_MESSAGE);

        }              
        return homeNum;        
    }
    while(homeNum < 0 || homeNum > 3);

以下是petRec方法,仅供澄清:

 public static void petRec(int homeType, double hoursAtHome)
{


    String pet;

        if(homeType == 1 && hoursAtHome >= 10)
            pet = "Cat";
        else
        if(homeType == 1 && hoursAtHome < 10)
        pet = "Hamster";
        else
        if(homeType == 2 && hoursAtHome >= 18)
        pet = "Pot-Bellied Pig";
        else
        if(homeType == 2 && hoursAtHome >= 10 && hoursAtHome <= 17)
        pet = "Dog";
        else
        if(homeType == 2 && hoursAtHome < 10)
        pet = "Snake";
        else
        if(homeType == 3 && hoursAtHome > 6)
        pet = "Fish";
        else
        if(homeType == 3 && hoursAtHome < 6)
        pet = "Ant Farm";
        else
        pet = "Nothing";

    JOptionPane.showMessageDialog(null, "You should get a " + pet + "!",
            "Recommended Pet", JOptionPane.INFORMATION_MESSAGE);



}
publicstaticvoidpetrec(int homeType,双小时)
{
线状宠物;
如果(homeType==1&&hoursAtHome>=10)
pet=“Cat”;
其他的
if(homeType==1&&hoursAtHome<10)
pet=“仓鼠”;
其他的
如果(homeType==2&&hoursAtHome>=18)
pet=“大肚猪”;
其他的
如果(homeType==2&&hoursAtHome>=10&&hoursAtHome 6)
pet=“鱼”;
其他的
如果(homeType==3&&hoursAtHome<6)
pet=“蚂蚁农场”;
其他的
pet=“无”;
JOptionPane.showMessageDialog(null,“您应该得到一个”+pet+“!”,
“推荐宠物”,JOptionPane.信息(信息);
}

去年我学习了VisualBasic的入门,有无限循环,今年我学习了Java,不能让循环重复。 getHours方法的结构与getHome方法几乎相同,只是在提示符中使用了不同的变量和措辞。 当输入的数字不是1、2或3时,程序应显示错误消息,然后再次循环询问您该数字。它显示错误消息,但接着询问小时数。 我再次非常感谢能提供的任何帮助。这项作业要到星期六才到期,但我只有两天的假期来完成这项工作。
提前感谢您的帮助:)

return
语句移到循环外:

public static int getHome()
{
    int homeNum;
    String home;
    do
    {
        home = JOptionPane.showInputDialog(null,"Enter 1(apartment), 2(house),"
            + " or 3(dorm).","Dwelling Type", JOptionPane.QUESTION_MESSAGE);
        homeNum = Integer.parseInt(home);

        if(!(homeNum == 1) && !(homeNum == 2) && !(homeNum == 3))
        {    
            JOptionPane.showMessageDialog(null, "The value for dwelling type "
                + "must be 1(apartment), 2(house), or 3(dorm)", "Dwelling"
                        + "Type Error", JOptionPane.ERROR_MESSAGE);

        }              
    }
    while(homeNum < 0 || homeNum > 3);
    return homeNum;    
}    
publicstaticintgethome()
{
int homeNum;
串回家;
做
{
home=JOptionPane.showInputDialog(空,“输入1(公寓),2(房子)”)
+“或3(宿舍)。”,“住宅类型”,JOptionPane。问题信息);
homeNum=Integer.parseInt(home);
如果(!(homeNum==1)和&!(homeNum==2)和&!(homeNum==3))
{    
JOptionPane.showMessageDialog(null,“居住类型的值”
+“必须是1(公寓)、2(房子)或3(宿舍)”、“住宅”
+“类型错误”,作业窗格。错误消息);
}              
}
而(homeNum<0 | | homeNum>3);
返回homeNum;
}    
实际上,您是在第一次循环迭代结束时从方法返回的。您可能还希望捕获
parseInt
调用可能引发的
NumberFormatException


另外,请注意,这将允许输入0。也许这是故意的;也许是疏忽。我不知道。

return
语句移到循环外:

public static int getHome()
{
    int homeNum;
    String home;
    do
    {
        home = JOptionPane.showInputDialog(null,"Enter 1(apartment), 2(house),"
            + " or 3(dorm).","Dwelling Type", JOptionPane.QUESTION_MESSAGE);
        homeNum = Integer.parseInt(home);

        if(!(homeNum == 1) && !(homeNum == 2) && !(homeNum == 3))
        {    
            JOptionPane.showMessageDialog(null, "The value for dwelling type "
                + "must be 1(apartment), 2(house), or 3(dorm)", "Dwelling"
                        + "Type Error", JOptionPane.ERROR_MESSAGE);

        }              
    }
    while(homeNum < 0 || homeNum > 3);
    return homeNum;    
}    
publicstaticintgethome()
{
int homeNum;
串回家;
做
{
home=JOptionPane.showInputDialog(空,“输入1(公寓),2(房子)”)
+“或3(宿舍)。”,“住宅类型”,JOptionPane。问题信息);
homeNum=Integer.parseInt(home);
如果(!(homeNum==1)和&!(homeNum==2)和&!(homeNum==3))
{    
JOptionPane.showMessageDialog(null,“居住类型的值”
+“必须是1(公寓)、2(房子)或3(宿舍)”、“住宅”
+“类型错误”,作业窗格。错误消息);
}              
}
而(homeNum<0 | | homeNum>3);
返回homeNum;
}    
实际上,您是在第一次循环迭代结束时从方法返回的。您可能还希望捕获
parseInt
调用可能引发的
NumberFormatException

另外,请注意,这将允许输入0。也许这是故意的;也许是疏忽。我说不上来。

欢迎来到Java:

do
{
    ...      
    return homeNum;        
}
while(homeNum < 0 || homeNum > 3);
  • return
    :当您返回一个值(如return homeNum)时,您将退出所使用的方法
  • 继续
    :当您
    继续
    时,您将进入下一次迭代;这只在循环中起作用
  • break
    :当您
    break
    时,您将结束循环或
    switch
    语句的执行。例如,如果你把
    break
    而不是
    返回h网膜循环将在此处结束
  • 抛出新异常(“Foobar”)
    :当您抛出错误时,它将退出当前的
    try。。catch
    block方法找到一个与异常类型匹配的方法
作为
中断
抛出
继续
的示例:

public static int getHome()
{
   int n = -1;
   for (;;) { // infinite loop powered !
     try {
       String home = JOptionPane.showInputDialog(null,"Enter 1(apartment), 2(house),"
                    + " or 3(dorm).","Dwelling Type", JOptionPane.QUESTION_MESSAGE);
       int homeRun =  Integer.parseInt(home);
       if(homeNum != 1 && homeNum != 2) && homeNum != 3) {
         // note: this is an example. You should NEVER use exception in these case
         throw new IllegalArgumentException("Damn!");
       }
       n = homeRun;
       break;

     } catch (NumberFormatException|IllegalArgumentException e) {
       JOptionPane.showMessageDialog(null, "The value for dwelling type "
             + "must be 1(apartment), 2(house), or 3(dorm)", "Dwelling"
                     + "Type Error", JOptionPane.ERROR_MESSAGE);       
       continue;
     }
  }
  return n;
}
这是一个丑陋的例子,向您展示了四个说明

  • 在这种情况下抛出异常是不好的做法。但是它将转到
    catch
    块,因为它捕获
    NumberFormatException
    (来自
    Integer.parseInt
    )和抛出的
    IllegalArgumentException
  • 中断可以由
    返回本垒打代替
  • 在这种情况下,
    continue
    是无用的,因为在
    try catch
    块之后什么都没有了
此外,如果你正在学习Java,也许你应该这样做,因为我认为你做得不对。有一些GUI组件为您处理输入转换的繁琐工作

甚至,您也不应该使用
作业窗格
,而应该依赖
系统。在
扫描仪中
:在终端/控制台中执行代码,然后在
public static int getHome()
{
   int n = -1;
   for (;;) { // infinite loop powered !
     try {
       String home = JOptionPane.showInputDialog(null,"Enter 1(apartment), 2(house),"
                    + " or 3(dorm).","Dwelling Type", JOptionPane.QUESTION_MESSAGE);
       int homeRun =  Integer.parseInt(home);
       if(homeNum != 1 && homeNum != 2) && homeNum != 3) {
         // note: this is an example. You should NEVER use exception in these case
         throw new IllegalArgumentException("Damn!");
       }
       n = homeRun;
       break;

     } catch (NumberFormatException|IllegalArgumentException e) {
       JOptionPane.showMessageDialog(null, "The value for dwelling type "
             + "must be 1(apartment), 2(house), or 3(dorm)", "Dwelling"
                     + "Type Error", JOptionPane.ERROR_MESSAGE);       
       continue;
     }
  }
  return n;
}