Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lecture3-流程控制思考题 #21

Open
wenuanhappy opened this issue Sep 30, 2016 · 2 comments
Open

lecture3-流程控制思考题 #21

wenuanhappy opened this issue Sep 30, 2016 · 2 comments

Comments

@wenuanhappy
Copy link

wenuanhappy commented Sep 30, 2016

以下代码的第32行可以省略么?为什么?

1   
2    // Compare integers using if structures, relational operators 
3    // and equality operators.
4    
5    // Java extension packages
6    import javax.swing.JOptionPane;
7    
8    public class Comparison {
9    
10      // main method begins execution of Java application
11      public static void main( String args[] )
12      {
13         String firstNumber;   // first string entered by user
14         String secondNumber;  // second string entered by user
15         String result;        // a string containing the output
16         int number1;          // first number to compare
17         int number2;          // second number to compare
18   
19         // read first number from user as a string
20         firstNumber =
21            JOptionPane.showInputDialog( "Enter first integer:" );
22   
23         // read second number from user as a string
24         secondNumber =
25            JOptionPane.showInputDialog( "Enter second integer:" );
26         
27         // convert numbers from type String to type int
28         number1 = Integer.parseInt( firstNumber );
29         number2 = Integer.parseInt( secondNumber );
30   
31         // initialize result to empty String
32         result = "";
33   
34         if ( number1 == number2 )
35            result = number1 + " == " + number2;
36   
37         if ( number1 != number2 )
38            result = number1 + " != " + number2;
39   
40         if ( number1 < number2 )
41            result = result + "\n" + number1 + " < " + number2;
42   
43         if ( number1 > number2 )
44            result = result + "\n" + number1 + " > " + number2;
45   
46         if ( number1 <= number2 )
47            result = result + "\n" + number1 + " <= " + number2;
48   
49         if ( number1 >= number2 )
50            result = result + "\n" + number1 + " >= " + number2;
51   
52         // Display results
53         JOptionPane.showMessageDialog(
54            null, result, "Comparison Results",
55            JOptionPane.INFORMATION_MESSAGE );
56   
57         System.exit( 0 );  // terminate application
58   
59      }  // end method main
60   
61   }  // end class Comparison
@ssjjcao
Copy link

ssjjcao commented Sep 30, 2016

不可以,变量result需要初始化。

@Guitenbay
Copy link

result是局部变量,没有初始值,所以需要第32行,否则会报错(已亲自试验)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants