| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 895 人关注过本帖
标题:按照视频教程的 一模一样但是 他没出错 我却出错了
只看楼主 加入收藏
wenxx10
Rank: 1
等 级:新手上路
帖 子:33
专家分:0
注 册:2008-8-4
收藏
 问题点数:0 回复次数:6 
按照视频教程的 一模一样但是 他没出错 我却出错了
class bl2
{
            public static void main(String [] args)
            {
                        int x=12;
                        {
                            if(x<3)
                            {
                                        System.out.println("x<3");
                                        System.out.println("ok");
                            }
                            else if(x>=3 && x<8)
                                        System.out.println("x>=3 && x<8");
                        }
              int y;            
                        /*if(x>0)
                                        y=x;
                        else
                                        y=-x;*/
                        y=x>0?x:-x;
                    
                        if(x==1)
                                    {
                                    if(y==1)
                                                {
                                                System.out.println("x==1,y==1");
                                                }
                                    else
                                            {
                                                System.out.println("x==1,y!=1");
                                         }
                                      }
                      else if(x>3)   
                                  {
                                  if(y==1)
                                    {
                                                System.out.println("x!=1,y==1");
                                    }
                                    else
                                    {
                                                System.out.println("x!=1,y!=1");
                                    }
                                    }
                        
                        x=1;
                        switch(x)
                        {
                                        case 1:
                                                        System.out.println("switch1");
                                                        break;
                                        case 2:
                                                        System.out.println("seitch2");
                                                        break;
                                        case 3:
                                        case 4:
                                                                        System.out.println("3,4");
                                                                        break;
                                        default:
                                                        System.out.println("other");
                        }
                        try
                        {      
                        x=System.in.read();
                          }
                          catch(Exception e){}
                        while(x!='q')
                        {
                                        System.out.println((char)x);
                                        x=System.in.read();  
                                             try
                                {      
                                x =System.in.read();
                                }
                                catch(Exception e){}
                                }     
            }
}

//编译时总是说 bl2.java:69: 未报告的异常 ;必须对其进行捕捉或声明以便抛出
                                                        x=System.in.read();
                                                                        ^
搜索更多相关主题的帖子: 视频 教程 一模一样 
2008-08-20 12:02
anaranjado
Rank: 1
来 自:法兰西
等 级:新手上路
威 望:1
帖 子:24
专家分:0
注 册:2008-8-5
收藏
得分:0 
程序代码:
while(x!='q'){
    System.out.println((char)x);
    x=System.in.read();
    try{      
        x =System.in.read();
    }catch(IOException e){
        System.err.println("IOException detected : " + e.getMessage());
    }
}


应该是因为没对第三行的那句read()做错误捕捉。
其实想省事点,直接在方法声明中表明该方法会排除异常。

程序代码:
public static void main(String [] args) throws IOException{
    ... ... ... ...
}


这样方法体内的 try 就可以省了。
2008-08-20 13:06
wenxx10
Rank: 1
等 级:新手上路
帖 子:33
专家分:0
注 册:2008-8-4
收藏
得分:0 
多谢   说详细些该怎样改呢?

[[it] 本帖最后由 wenxx10 于 2008-8-20 13:48 编辑 [/it]]
2008-08-20 13:47
anaranjado
Rank: 1
来 自:法兰西
等 级:新手上路
威 望:1
帖 子:24
专家分:0
注 册:2008-8-5
收藏
得分:0 
程序代码:
while(x!='q'){
    System.out.println((char)x);
    try{
        x=System.in.read();
    }catch(IOException e){}
    try{      
        x =System.in.read();
    }catch(IOException e){
        System.err.println("IOException detected : " + e.getMessage());
    }
}

或者把程序里面所有的try/catch去掉,但是在main声明处注明:
程序代码:
public static void main(String [] args) throws IOException{
    ... ... ... ...
}


但是说实话,我看不明白,你为啥要连续写两边read()。不知道是啥用意。
2008-08-20 15:24
wenxx10
Rank: 1
等 级:新手上路
帖 子:33
专家分:0
注 册:2008-8-4
收藏
得分:0 
xx  我也不懂视频上只是讲循环没将这个
2008-08-20 19:16
wenxx10
Rank: 1
等 级:新手上路
帖 子:33
专家分:0
注 册:2008-8-4
收藏
得分:0 
bl2.java:3: 找不到符号
符号: 类 IOException
位置: 类 bl2
            public static void main(String [] args) throws IOException
                                                           ^
1 错误
2008-08-20 19:19
anaranjado
Rank: 1
来 自:法兰西
等 级:新手上路
威 望:1
帖 子:24
专家分:0
注 册:2008-8-5
收藏
得分:0 
可能是因为没导入包,在程序第一行写入:
import *;
2008-08-20 22:40
快速回复:按照视频教程的 一模一样但是 他没出错 我却出错了
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.024147 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved