2016-09-24 231 views
0

好日子的家伙,我是新来的。我正在为我的前卫部队做一个任务,所以请耐心等待。JAVA:if语句里面的for循环和退出for循环

所以我要做的是写一个代码,可以输入从1到120之间的整数的人的年龄。然后用户必须计算平均年龄,并且应该以实数计算。但用户必须输入年龄值,直到用户输入0,即停止程序然后输出平均值。如果用户输入的年龄无效,那么程序应该继续重新提示用户,直到他们输入有效年龄。

所以我尽了我的一份力。我创建了一个代码,我想出了这一点:

public static void main(String[] args) 
{ 

    int ageValue = 0; 
    double getAge; 
    getAge = inputAge(); 
    System.out.println("Average age is: " + getAge); 
} 

public static double inputAge() 
{ 
    int ageValue = 0; 
    double avgAge = 0; 
    Scanner sc = new Scanner(System.in); 
    for (int i = 1; i <= 120; i++) 
    { 
     System.out.println("Enter age"); 
     ageValue += sc.nextInt(); 

     avgAge = ageValue/(double) i; 
     if (ageValue == 0) 
     { 
      System.out.println("Average age is: " + avgAge); 
      System.exit(0); 
     } 
     while (ageValue < 0 || ageValue > 120) 
     { 
      System.out.println("Invalid input. Try again!"); 
      ageValue = sc.nextInt(); 
     } 

    } 

    return avgAge; 
} 

现在我放下我的代码,我得到了我的平均公式莫名其妙地工作。现在,问题是,当我按0时,它不会提示“if”语句。但是,当第一个“输入你的年龄”提示出现,我按0,“if”声明工作。但是对于每次迭代,程序都不会让我执行语句。

另一方面,我也在努力弄清楚如何在不使用break或System.exit()的情况下退出循环,因为这会给我零标记。我想要的是当我按0时,它应该退出循环并输出平均值,就像任务所说的那样。

我不知道你们是否可以得到它..代码是否正确?我在正确的轨道上吗?我错过了什么?

干杯

回答

1

你可以考虑一个do while循环的方法。这将允许您的代码自然运行一次,并在用户输入0后退出:

int ageValue = 0, numOfAges = 0, sumOfAges = 0; 
do { 
    System.out.println("Enter age"); 
    ageValue = sc.nextInt(); 
    if (ageValue < 0 || ageValue > 120) 
     System.out.println("Bad value... try again"); 
    else if (ageValue != 0) { 
     sumOfAges += ageValue; 
     numOfAges++; 
    } 
} while (ageValue != 0); 
return ((double)sumOfAges/numOfAges); 
+0

以及修复各种错误,这也是该部门只有一次。应该注意的是,如果用户输入nextInt无法解析的内容,这会引发异常。 – Rodney

+0

哦,真的没有意识到while while循环。谢谢,这解决了我的问题 – Seth

0

在另一方面,我也在努力找出如何退出循环,而不使用断点或System.exit()的,因为这会给我零分。

你可以有另一种情况在你像这样

boolean finished = false; 
for (int i = 1; i <= 120 && finished == false; i++) 

循环和更换

System.exit(0) 

finished = true; 

但是,我为什么用“破发会质疑“会给你零分。这正是这种场景中断的意图。

+0

感谢您的回复。而且,它应该只用于switch case语句。不知道为什么,但我的导师说如此 – Seth

+0

“应该只用于switch case”..那是他/她的意见。 Oracle教程给出了什么时候适合摆脱for循环的好例子https://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html – Rodney

0

你可以试试这种方法。

我已经纠正了一点退出条件和平均完成的方式。

您在代码中显示的“for”循环将样本数量限制为120,但问题不会这么说,所以我冒昧地将您的问题推广到任意数量的样本中。

第一件事是你应该查找“if-else”条件结构,因为这是你的代码中缺少的主要观点。

https://en.wikipedia.org/wiki/Conditional_(computer_programming)

你能想到的问题是表现为方式:

  1. 在意甲计算平均
  2. 意甲是键盘输入
  3. 时零输入,退出循环并返回当前平均值
  4. 当输入任何超出范围[0,120]的值时,给出消息并继续循环而不更改任何值意甲
  5. 当结合的[1119]内的任何值被输入物品的价值添加到意甲,并重新计算平均

    import java.util.ArrayList; 
    import java.util.List; 
    import java.util.Scanner; 
    
    public class Test 
    { 
        public static void main(String[] args) 
        { 
         System.out.println("Final Average age is: "+inputAge()); 
        } 
    
        private static double inputAge() 
        { 
         int ageValue=0; 
         double avgAge=0; 
         boolean shouldExit=false; 
         Scanner sc=new Scanner(System.in); 
         List<Integer> samples=new ArrayList<Integer>(); 
         // loop until flag is true 
         while(!shouldExit) 
         { 
    
          System.out.println("Enter age"); 
          ageValue=sc.nextInt(); 
          if(ageValue==0) 
          { 
           shouldExit=true; 
          } 
          else if(ageValue<0||ageValue>120) 
          { 
           System.out.println("Invalid input. Try again!"); 
          } 
          else 
          { 
           // add current input in the samples and calculate average over all the samples 
           samples.add(ageValue); 
           avgAge=getAvg(samples); 
           System.out.println("Current Average age is: "+avgAge); 
          } 
         } 
         sc.close(); 
         return avgAge; 
        } 
    
        private static double getAvg(List<Integer> samples) 
        { 
         double avgAge=0; 
         for(Integer tmp:samples) 
         { 
          avgAge+=tmp; 
         } 
         return avgAge/(double) samples.size(); 
        } 
    } 
    
+0

将研究一下! – Seth