变量需要在for循环中初始化,但不能在while循环中执行吗?

时间:2015-11-27 16:25:41

标签: java

我只想计算你未来的年龄。您输入3件事:您当前的年龄,当前年份和未来年份。我正在使用fordo while循环进行体验。然后我遇到了这个问题,变量currentAge未初始化。但是没有其他变量也被初始化,其他变量也没问题。这里唯一的区别是其他人在do while循环中,但currentAgefor循环中。为什么会这样?有人可以解释这个差异吗?为什么?请参阅以下代码。

import java.util.Scanner; 

public class Lab3Class 
{ 

    public static void main(String[] args) 
    { 

        Scanner input = new Scanner(System.in); 
        String cleanUpStr; 
        int currentAge; 
        int futureAge; 
        int currentYear; 
        int futureYear;

        for (int cntr = 0; cntr < 3; ++cntr) 
        { 

            System.out.print("Enter your current age\t"); 

            currentAge = input.nextInt( ); 

            cleanUpStr = input.nextLine( ); 
        }

    if (currentAge >= 0){

        do 
        { 
            System.out.print("Enter current year\t"); 

            currentYear = input.nextInt( ); 

            cleanUpStr = input.nextLine( ); 
        } while(currentYear < 0); 

        do 
        { 
            System.out.print("Enter future year\t"); 

            futureYear = input.nextInt( ); 
            cleanUpStr = input.nextLine( ); 
        } while(futureYear < 0 || futureYear < currentYear);

        input.close();
        futureAge = currentAge + (futureYear - currentYear);

        System.out.println("In the year " + currentYear + " you are " + currentAge + " years old"); 
        System.out.println("In the year " + futureYear + " you will be " + futureAge + " years old"); 
    } else {
        System.out.println("Too many tries for an valid age!");
    }

    }
}

3 个答案:

答案 0 :(得分:3)

do / while 循环代码块执行一次(至少)无论条件是什么。但for循环不遵循相同的传统:for循环检查条件是否有效 FIRST 然后执行

执行演示:

For循环:(pre; condition; post)

  1. 检查条件 - &gt;如果条件为真 - > 2.执行代码块 - &gt; 3.继续下一次迭代
  2. Do / While循环:do { }while(condition);

    1. 执行代码块 - &gt; 2.检查条件 - &gt;如果条件为真 - > 3.继续下一次迭代。

答案 1 :(得分:1)

尝试这个完美的代码版本:

import java.util.Scanner;

public class Lab3Class {

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);
    String cleanUpStr;
    int currentAge = 0;
    int futureAge;
    int currentYear;
    int futureYear;

    System.out.print("Enter current age\t");
    currentAge = input.nextInt();

    if (currentAge >= 0) {
        do {
            System.out.print("Enter current year\t");
            currentYear = input.nextInt();
            cleanUpStr = input.nextLine();
        } while (currentYear < 0);

        do {

            System.out.print("Enter future year\t");
            futureYear = input.nextInt();

            cleanUpStr = input.nextLine();
        } while (futureYear < 0 || futureYear < currentYear);

        input.close();
        futureAge = currentAge + (futureYear - currentYear);

        System.out.println("In the year " + currentYear + " you are "
                + currentAge + " years old");
        System.out.println("In the year " + futureYear + " you will be "
                + futureAge + " years old");
    } else {
        System.out.println("Too many tries for an valid age!");
    }

 }
}

答案 2 :(得分:1)

为了使其有效 - 您只需要更改currentAge声明行,将其设置为初始值,如下所示:

int currentAge = 0;

Java使用默认值初始化类字段,但不使用方法变量。 作为一种替代解决方案(仅为了提高理解力),我建议将currentAge的声明移到类级别并保留默认值,如下所示:

public class Lab3Class
{

    private static         int currentAge;
...
}

程序行为将是相同的,但将描述字段和方法变量声明和初始化之间的差异。

您可以找到有用的以下链接: http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.3.1.1

http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.12.5