java语言程序设计第二版课后答案吴倩_java语言程序设计课后习题答案解析

专业整理

知识分享

习题

2

3

.使用“

=

=

”对相同内容的字符串进行比

较,看会产生什么样的结果。

答:首先创建一个字符串变量有两种方式:

String str = new String("abc");

String str = "abc"

使用“

= =

”会因为创建的形式不同而产生

不同的结果:

String str1 = "abc";

String str2 = "abc";

System.out.println(str1=

=str2);

//true

String str1 = new String("abc");

String str2 = "abc";

System.out.println(str1=

=str2);

//false

String str1 = new String("abc");

String str2 = new String("abc");

System.out.println(str1=

=str2);

//false

因此自符串如果是对内容进行比较,使用

equals

方法比较可靠。

String str1 = "abc";

String str2 = "abc";

System.out.println(str1=

=str2);

//true

String str1 = new String("abc");

String str2 = "abc";

System.out.println(str1.equals(str2));

//true

String str1 = new String("abc");

String str2 = new String("abc");

System.out.println(str1.equals(str2));

//true

5

.编写一个程序,把变量

n

的初始值设置

1678

然后利用除法运算和取余运算把变

量的每位数字都提出来并打印,输出结果

为:

n=1678

n

的每位数字是

1

6

7

8

n

为任意值呢?

法一:

public class Exercise5{

public

static

void

main(String[]

args){

int n=1678;

int unit;

int decimal;

int hundred;

int thousand;

int count;

thousand=n/1000;

count=n%1000;

hundred=count/100;

count=count%100;

decimal=count/10;

count=count%10;

unit=count;

System.out.println("1678

包含的数

字分别是:

"+thousand+','+hundred+','+decimal+',

'+unit);

}

}

//

如果

n

为任意值

import java.io.*;

public class Exercise51{

public

static

void

main(String[]

args)

throws IOException{

System.out.print("

数:

");

InputStreamReader isStream=new

InputStreamReader(System.in);

BufferedReader bfReader=new

BufferedReader(isStream);

String

input=bfReader.readLine();

int length=input.length()-1;

int n=new

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值