hands-on brain java java class classroom hands-on brain

java classroom hands-on brain

 

1, carefully read the examples: EnumTest.java, run it, run the analysis results? What conclusions can you get? You have mastered the basic usage of enumeration types yet?

package text;2904628156

the Enum {public static class public void main (String [] args) {
Size = S Size.SMALL;
Size t = Size.LARGE;
// S and t refer to the same object?
System.out.println (S == t); //
// primitive data type?
System.out.println (s.getClass () isPrimitive ().);
// convert from a string
Size Size.valueOf U = ( "the SMALL");
System.out.println (S == U); // to true
// List of all its values
for (Size value: Size.values ()) {
System.out.println (value);
}
}

}
enum Size{SMALL,MEDIUM,LARGE};

operation result:

false
false
true
SMALL
MEDIUM
LARGE

Conclusion: The enumeration can be used in a switch statement. Enumeration type does not belong to the original data, each of which references a specific value of the specific object. The same value as the reference the same object.

Can use the "==" and equals () method for direct enumeration value ratio, in other words, for the enumerated type variables, the result of performing the method "==" and equals () are equivalent.

2、

Two input box data shows the result with a message box

package text;
//An addition program

import javax.swing.JOptionPane; // import class JOptionPane
public class Addition {
public static void main( String args[] )
{
String firstNumber, // first string entered by user
secondNumber; // second string entered by user
int number1, // first number to add
number2, // second number to add
sum; // sum of number1 and number2

// read in first number from user as a string
firstNumber =
JOptionPane.showInputDialog( "Enter first integer" );

// read in second number from user as a string
secondNumber =
JOptionPane.showInputDialog( "Enter second integer" );

// convert numbers from type String to type int
number1 = Integer.parseInt( firstNumber ); 
number2 = Integer.parseInt( secondNumber );

// add the numbers
sum = number1 + number2;

// display the results
JOptionPane.showMessageDialog(
null, "The sum is " + sum, "Results",
JOptionPane.PLAIN_MESSAGE );

System.exit( 0 ); // terminate the program
}
}

 Operating results: Enter first integer 4

                   Enter second integer 4

                    The sum is 9

3, run the following code

 

package text;

public class Textdouble {

public static void main(String args[]) {
System.out.println("0.05 + 0.01 = " + (0.05 + 0.01));
System.out.println("1.0 - 0.42 = " + (1.0 - 0.42));
System.out.println("4.015 * 100 = " + (4.015 * 100));
System.out.println("123.3 / 100 = " + (123.3 / 100));
}
}

operation result:

0.05 + 0.01 = 0.060000000000000005
1.0 - 0.42 = 0.5800000000000001
4.015 * 100 = 401.49999999999994
123.3 / 100 = 1.2329999999999999

You see what kind of output, accident? Why double the value of the type of a "mathematically precise" not the result of the operation?

Not surprisingly,

Use of a double numerical calculation,  the result is inaccurate. a float account the sign bit, 8-bit exponent, the mantissa is 23 bits, so a total of four bytes; accounting for a double symbol bit, 11-bit exponent, 52 mantissa. Sign bit is 0, then instructions are positive for words 1 described is negative; order code is the number of power mantissa is multiplied by 2; mantissa default is to remove an integer bit, that is added to the mantissa sequence 010,101,011 of ( and there are many), then in fact it is 1.01010101 (and there are many). For example, a variable of type float, the sign bit is 0, the order codes into decimal 3, mantissa 01000000 (hereinafter both 0).

4、

What is the output of the following code?

int X=100;

int Y=200;

System.out.println("X+Y="+X+Y);

System.out.println(X+Y+"=X+Y");

Why is this output?

Run Results: X + Y = 100200 300 = X + Y

In a sentence, the "+" is the amount of connection; the statement II, "+" is an operator, for summation.

5、

package text;

import java.util.Scanner;

public class InputTest {
public static void main(String[] args)

Scanner in = new Scanner(System.in);

// get first input
System.out.print("What is your name? ");
String name = in.nextLine();

// get second input
System.out.print("How old are you? ");
int age = in.nextInt();


/* int i;
String value="100";
i=Integer.parseInt(value);
i=200;
String s=String.valueOf(i);*/

// display output on console
System.out.println("Hello, " + name + ". Next year, you'll be " + (age + 1));


}
}

1, carefully read the examples: EnumTest.java, run it, run the analysis results? What conclusions can you get? You have mastered the basic usage of enumeration types yet?

package text;2904628156

the Enum {public static class public void main (String [] args) {
Size = S Size.SMALL;
Size t = Size.LARGE;
// S and t refer to the same object?
System.out.println (S == t); //
// primitive data type?
System.out.println (s.getClass () isPrimitive ().);
// convert from a string
Size Size.valueOf U = ( "the SMALL");
System.out.println (S == U); // to true
// List of all its values
for (Size value: Size.values ()) {
System.out.println (value);
}
}

}
enum Size{SMALL,MEDIUM,LARGE};

operation result:

false
false
true
SMALL
MEDIUM
LARGE

Conclusion: The enumeration can be used in a switch statement. Enumeration type does not belong to the original data, each of which references a specific value of the specific object. The same value as the reference the same object.

Can use the "==" and equals () method for direct enumeration value ratio, in other words, for the enumerated type variables, the result of performing the method "==" and equals () are equivalent.

2、

Two input box data shows the result with a message box

package text;
//An addition program

import javax.swing.JOptionPane; // import class JOptionPane
public class Addition {
public static void main( String args[] )
{
String firstNumber, // first string entered by user
secondNumber; // second string entered by user
int number1, // first number to add
number2, // second number to add
sum; // sum of number1 and number2

// read in first number from user as a string
firstNumber =
JOptionPane.showInputDialog( "Enter first integer" );

// read in second number from user as a string
secondNumber =
JOptionPane.showInputDialog( "Enter second integer" );

// convert numbers from type String to type int
number1 = Integer.parseInt( firstNumber ); 
number2 = Integer.parseInt( secondNumber );

// add the numbers
sum = number1 + number2;

// display the results
JOptionPane.showMessageDialog(
null, "The sum is " + sum, "Results",
JOptionPane.PLAIN_MESSAGE );

System.exit( 0 ); // terminate the program
}
}

 Operating results: Enter first integer 4

                   Enter second integer 4

                    The sum is 9

3, run the following code

 

package text;

public class Textdouble {

public static void main(String args[]) {
System.out.println("0.05 + 0.01 = " + (0.05 + 0.01));
System.out.println("1.0 - 0.42 = " + (1.0 - 0.42));
System.out.println("4.015 * 100 = " + (4.015 * 100));
System.out.println("123.3 / 100 = " + (123.3 / 100));
}
}

operation result:

0.05 + 0.01 = 0.060000000000000005
1.0 - 0.42 = 0.5800000000000001
4.015 * 100 = 401.49999999999994
123.3 / 100 = 1.2329999999999999

You see what kind of output, accident? Why double the value of the type of a "mathematically precise" not the result of the operation?

Not surprisingly,

Use of a double numerical calculation,  the result is inaccurate. a float account the sign bit, 8-bit exponent, the mantissa is 23 bits, so a total of four bytes; accounting for a double symbol bit, 11-bit exponent, 52 mantissa. Sign bit is 0, then instructions are positive for words 1 described is negative; order code is the number of power mantissa is multiplied by 2; mantissa default is to remove an integer bit, that is added to the mantissa sequence 010,101,011 of ( and there are many), then in fact it is 1.01010101 (and there are many). For example, a variable of type float, the sign bit is 0, the order codes into decimal 3, mantissa 01000000 (hereinafter both 0).

4、

What is the output of the following code?

int X=100;

int Y=200;

System.out.println("X+Y="+X+Y);

System.out.println(X+Y+"=X+Y");

Why is this output?

Run Results: X + Y = 100200 300 = X + Y

In a sentence, the "+" is the amount of connection; the statement II, "+" is an operator, for summation.

5、

package text;

import java.util.Scanner;

public class InputTest {
public static void main(String[] args)

Scanner in = new Scanner(System.in);

// get first input
System.out.print("What is your name? ");
String name = in.nextLine();

// get second input
System.out.print("How old are you? ");
int age = in.nextInt();


/* int i;
String value="100";
i=Integer.parseInt(value);
i=200;
String s=String.valueOf(i);*/

// display output on console
System.out.println("Hello, " + name + ". Next year, you'll be " + (age + 1));


}
}

Guess you like

Origin www.cnblogs.com/fdfds228/p/11706148.html