课后作业2 动手动脑:但是先动脑,再动手。如果先动手,再动脑也不是不行。但是如果只是一直动手不动脑。那可能会小脑萎缩。

ppt页数 37 44 46 50 52 54 56 59 62

1.问题

 2.源代码

package test;

enum Size{SMALL,MEDIUM,LARGE};

public class test {

 

public static void main(String[] args) {

Size s=Size.SMALL;

Size t=Size.LARGE;

System.out.println(s==t);

System.out.println(s.getClass().isPrimitive());

Size u=Size.valueOf("SMALL");

System.out.println(s==u); ֵ

for(Size value:Size.values()){

System.out.println(value);

}

 

}

 

}

 

3.运行截图

 4.过程解析

枚举类型有多重赋值方法,枚举类型不是八大基本类型。枚举类型方法values()是其所有值的集合;

1.问题

 二 答案

十进制数据的二进制表现形式就是原码,原码最左边的一个数字就是符号位,0为正,1为负。

正数的反码是其本身(等于原码),负数的反码是符号位保持不变,其余位取反。

正数的补码是其本身,负数的补码等于其反码 +1。

Java中用补码形式表示

1.问题

 

2.源代码

package test;

 

//An addition program

 

import javax.swing.JOptionPane; // import class JOptionPane

 

public class test {

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

}

}

 3.结果

 

 

 四

1.问题

 

2.源代码

package test;

public class test {

public static String value = "外层";

 

public static void main( String args[] )

{

 

String value = "内层";

System.out.println(value);

System.out.println(slim.value);

}

public static class slim

{

public static String value = "类中";

}

 

}

3.结果和解析

 内层会覆盖外层 在内层调用类中变量 会覆盖内层。总而言之 越内越先

1.问题

 2.源代码

package test;

import java.util.*;

public class test {

public static String value = "外层";

 

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();

 

 

/* 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));

 

}

}

3.结果

 六

1.问题

 2.答案

byte类型占8位; 1个字节
short类型占16位; 2个字节
int 类型占32位; 4 个字节
long 类型占64位; 8个字节
float 类型占32位; 4个字节
double 类型占64位; 8个字节
boolean类型占8位; 1个字节
char 类型占16位; 2个字节

当粗精度向更精细的精度转换时会出现损失

1.问题

 2.源代码

package test;

public class test {

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));

}

 

}

3.结果

 不意外 浮点数存储方式不同 计算不能得出精确值

1.问题

 2.源代码

package test;

import java.math.BigDecimal;

public class test {

public static void main( String args[] )

{

 

BigDecimal f1 = new BigDecimal("0.05");

BigDecimal f2 = BigDecimal.valueOf(0.01);

BigDecimal f3 = new BigDecimal(0.05);

System.out.println("����ʹ��String��ΪBigDecimal�����������ļ�������");

System.out.println("0.05 + 0.01 = " + f1.add(f2));

System.out.println("0.05 - 0.01 = " + f1.subtract(f2));

System.out.println("0.05 * 0.01 = " + f1.multiply(f2));

System.out.println("0.05 / 0.01 = " + f1.divide(f2));

System.out.println("����ʹ��double��ΪBigDecimal�����������ļ�������");

System.out.println("0.05 + 0.01 = " + f3.add(f2));

System.out.println("0.05 - 0.01 = " + f3.subtract(f2));

System.out.println("0.05 * 0.01 = " + f3.multiply(f2));

System.out.println("0.05 / 0.01 = " + f3.divide(f2));

}

}

3.结果及分析

 把浮点数转换为字符串去计算然后就可以得出精确值 

1.问题

 

2.源代码

package test;

public class test {

public static void main( String args[] )

{

 

int X = 100;

int Y = 200;

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

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

}

}

 

3.结果及解析

 

第一行的输出先输出一个字符串于是后面的加号默认后面添加一个字符串为X的值,然后是另一个字符串Y的值。

第二行先计算X+Y输出其结果,然后输出一个字符串=X+Y;

posted @ 2023-09-14 21:53  混沌武士丞  阅读(8)  评论(0编辑  收藏  举报