• Java循环和条件


        下列程序的输出结果是什么?

    1、Java循环和条件

    /**
     * @Title:IuputData.java
     * @Package:com.you.data
     * @Description:TODO
     * @Author: 游海东
     * @date: 2014年3月16日 下午10:18:46
     * @Version V1.2.3
     */
    package com.you.data;
    
    /**
     * @类名:IuputData
     * @描述:TODO
     * @Author:Administrator
     * @date: 2014年3月16日 下午10:18:46
     */
    public class IuputData 
    {
    	/**
    	 * @Title : main
    	 * @Type : IuputData
    	 * @date : 2014年3月16日 下午10:18:47
    	 * @Description : TODO
    	 * @param args
    	 */
    	public static void main(String[] args) 
    	{
    		for(int i=0;i<100;i++)
    			Integer inte = new Integer(i);
    		System.out.println("YouHaidong");
    
    	}
    
    }
    
       运行Java Application,出现错误:

    Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    	Syntax error, insert "AssignmentOperator Expression" to complete Assignment
    	Syntax error, insert ";" to complete Statement
    	Integer cannot be resolved to a variable
    	inte cannot be resolved to a variable
    	i cannot be resolved to a variable
    
    	at com.you.data.IuputData.main(IuputData.java:29)
    

           局部变量声明的作用范围是在一个块内,for循环仅限于执行语句。

        由于这段代码中的Integer inte 的作用范围在整个main方法中,这样就造成了变量重复定义,

        出现程序编译错误

    2、正确的做法

    /**
     * @Title:IuputData.java
     * @Package:com.you.data
     * @Description:TODO
     * @Author: 游海东
     * @date: 2014年3月16日 下午10:18:46
     * @Version V1.2.3
     */
    package com.you.data;
    
    /**
     * @类名:IuputData
     * @描述:TODO
     * @Author:Administrator
     * @date: 2014年3月16日 下午10:18:46
     */
    public class IuputData 
    {
    	/**
    	 * @Title : main
    	 * @Type : IuputData
    	 * @date : 2014年3月16日 下午10:18:47
    	 * @Description : TODO
    	 * @param args
    	 */
    	public static void main(String[] args) 
    	{
    		for(int i=0;i<100;i++)
    		{
    			Integer inte = new Integer(i);
    		}
    		System.out.println("YouHaidong");
    	}
    
    }
    

    输出:YouHaidong

  • 相关阅读:
    Git学习笔记06-版本回退
    python3+selenium入门07-元素等待
    [cerc2017J]Justified Jungle
    [codeforces126B]Password
    计算几何基础模板
    floyd路径记录
    [数据结构复习]层序建立二叉树
    [patl2-011]玩转二叉树
    [poj3348]Cows
    [poj3347]Kadj Squares
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13315300.html
Copyright © 2020-2023  润新知