Topic: 高手帮下忙

  Print this page

1.高手帮下忙 Copy to clipboard
Posted by: pnwqcomputer
Posted on: 2005-07-08 21:12

请使用准确的文字描述作为标题
Your next post without a proper Subject will be removed.

Original subject :
高手帮下忙


import java.io.*;
public class ym{
public static void main(String args[]) throws IOException
{
int num;
String str1;
BufferedReader buffer1;
buffer1=new BufferedReader(new InputStreamReader(System.in));
while(true)
{
try
{
System.out.print("请输入成绩:");
str1=buffer1.readLine();
if (str1.length()<1){System.out.println("你必须输入一个数值!");}
else {break;}
}

catch(IOException e)
{
continue;
}
}
num=Integer.parseInt(str1);
if(num>=60){System.out.println(num+"是及格成绩");}
else{System.out.println(num+"不是及格分数");}
}
}

在 int num;后加上int i=1;后把true改为i==1为什么就通不过编译呢???帮下忙,谢谢了!!!!!

2.Re:高手帮下忙 [Re: pnwqcomputer] Copy to clipboard
Posted by: szaba
Posted on: 2005-07-08 21:40

同样不懂 不过在我的平台里 编译报错只是认为str1应该初始化
只要把String str1; 这一句改为 String str1 = "";就行了

3.Re:高手帮下忙 [Re: pnwqcomputer] Copy to clipboard
Posted by: pnwqcomputer
Posted on: 2005-07-08 21:45

同样不懂 不过在我的平台里 编译报错只是认为str1应该初始化
只要把String str1; 这一句改为 String str1 = "";就行了

原程序是在书上抄来的,在我的平台上没报错!晕!!越来越多难点了!!!

4.Re:高手帮下忙 [Re: pnwqcomputer] Copy to clipboard
Posted by: szaba
Posted on: 2005-07-08 22:39

没报错怎么叫通不过编译呢?

5.Re:高手帮下忙 [Re: pnwqcomputer] Copy to clipboard
Posted by: why
Posted on: 2005-07-09 00:19

pnwqcomputer wrote:
在 int num;后加上int i=1;后把true改为i==1为什么就通不过编译呢???

ARE you sure?

原程序是在书上抄来的,在我的平台上没报错!

String str1; 这一句未改時没报错?
ARE you sure?!?!

Please be responsible for the information you provided.
Check carefuly, please!

6.Re:高手帮下忙 [Re: why] Copy to clipboard
Posted by: szaba
Posted on: 2005-07-09 06:59

老大 我试过的 的确是原来不报错 后来改了就报错了

7.Re:高手帮下忙 [Re: szaba] Copy to clipboard
Posted by: why
Posted on: 2005-07-09 07:48

szaba wrote:
老大 我试过的 的确是原来不报错 后来改了就报错了

Could you compile the two files and show us what are on your Command Prompt window (assuming you're using one)?

Show us the two files as well - 原来的 and 后来改了的

8.Re:高手帮下忙 [Re: pnwqcomputer] Copy to clipboard
Posted by: pnwqcomputer
Posted on: 2005-07-09 08:43

原程序是《精彩java 2程序设计》第169页的(没报错)

import java.io.*;
public class ym{
public static void main(String args[]) throws IOException
{
int num;
String str1;
BufferedReader buffer1;
buffer1=new BufferedReader(new InputStreamReader(System.in));
while(true)
{
try
{
System.out.print("请输入成绩:");
str1=buffer1.readLine();
if (str1.length()<1){System.out.println("你必须输入一个数值!");}
else {break;}
}

catch(IOException e)
{
continue;
}
}
num=Integer.parseInt(str1);
if(num>=60){System.out.println(num+"是及格成绩");}
else{System.out.println(num+"不是及格分数");}
}
}

修改后的是

import java.io.*;
public class ym{
public static void main(String args[]) throws IOException
{
int num;
int i=1;
String str1;
BufferedReader buffer1;
buffer1=new BufferedReader(new InputStreamReader(System.in));
while(i==1)
{
try
{
System.out.print("请输入成绩:");
str1=buffer1.readLine();
if (str1.length()<1){System.out.println("你必须输入一个数值!");}
else {break;}
}

catch(IOException e)
{
continue;
}
}
num=Integer.parseInt(str1);
if(num>=60){System.out.println(num+"是及格成绩");}
else{System.out.println(num+"不是及格分数");}
}
}

改后就报错了;

再改为:

import java.io.*;
public class ym{
public static void main(String args[]) throws IOException
{
int num;
int i=1;
String str1="";
BufferedReader buffer1;
buffer1=new BufferedReader(new InputStreamReader(System.in));
while(i==1)
{
try
{
System.out.print("请输入成绩:");
str1=buffer1.readLine();
if (str1.length()<1){System.out.println("你必须输入一个数值!");}
else {break;}
}

catch(IOException e)
{
continue;
}
}
num=Integer.parseInt(str1);
if(num>=60){System.out.println(num+"是及格成绩");}
else{System.out.println(num+"不是及格分数");}
}
}

又没报错!!高手们阿!Tell me why?Thank you!

9.Re:高手帮下忙 [Re: pnwqcomputer] Copy to clipboard
Posted by: why
Posted on: 2005-07-09 11:56

> 原程序是《精彩java 2程序设计》第169页的(没报错)
while (true) { ...
str1=buffer1.readLine(); // would be run at least once (or Exception caught), so str1 would be initialized

> 修改后的是
> 改后就报错了;
num=Integer.parseInt(str1); // uninitialized local variable

> 再改为:
corrected now.

10.Re:高手帮下忙 [Re: pnwqcomputer] Copy to clipboard
Posted by: pnwqcomputer
Posted on: 2005-07-09 13:18

谢谢你!可是我还有点不明白
> 修改后
不是还要执行
str1=buffer1.readLine(); 不也一样初试化了吗?
Aren't "while (true)" and "int i=1;while(i==1)" the same?

11.Re:高手帮下忙 [Re: pnwqcomputer] Copy to clipboard
Posted by: why
Posted on: 2005-07-09 21:17

pnwqcomputer wrote:
谢谢你!可是我还有点不明白
> 修改后
不是还要执行
str1=buffer1.readLine(); 不也一样初试化了吗?
Aren't "while (true)" and "int i=1;while(i==1)" the same?

The compiler is not clever enough to know that i==1 is always true.

12.Re:高手帮下忙 [Re: pnwqcomputer] Copy to clipboard
Posted by: szaba
Posted on: 2005-07-10 11:13

呵呵 应该说 compiler is clever enough!
不知道我这样理解对不对?

当while(ture)的时候
虽然 str1定义时没有赋初值
编译也能通过而不报错

但是当while(i==1)的时候 就会要求 str1定义时必须赋初值?

请WHY老大继续指教!谢谢Smile

13.Re:高手帮下忙 [Re: szaba] Copy to clipboard
Posted by: why
Posted on: 2005-07-10 12:10

szaba wrote:
呵呵 应该说 compiler is clever enough!

Dead
As you like...

The compiler won't tell whether i is still 1 from lines 7 to 10 (of "修改后的是"), so it won't assume that str1 would be initialized at line 15.

I'm already out of patience, so please don't ask me any further.

14.Re:高手帮下忙 [Re: pnwqcomputer] Copy to clipboard
Posted by: szaba
Posted on: 2005-07-10 17:58

了解
WHY老大不是版主 自然没有答疑解惑的职责
想想幼时 也因反应迟钝(?)不得老师之喜
道理是一样的
以后兄弟 不指名老大指教就是
这样老大什么时候 out of patience了
只要视若无睹 当偶放P便是了


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923