专业整理
知识共享
习题
2
3
.“
=
=
”比较相同内容的字符串
比较一下,看看会产生什么样的结果。
答:首先,有两种方法可以创建字符串变量。
stringstr=newstring(ABC );
Stringstr='abc '
;
" "
==
”是因为制作的形式不同而发生的
不同的结果:
Stringstr1='abc ';
Stringstr2='abc ';
system.out.println(str1=
=str2;
//true
stringstr1=newstring(ABC );
Stringstr2='abc ';
system.out.println(str1=
=str2;
//false
stringstr1=newstring(ABC );
stringstr2=newstring(ABC );
system.out.println(str1=
=str2;
//false
因此,在比较内容时,使用自符号串
equals
方法比较可靠。
Stringstr1='abc ';
Stringstr2='abc ';
system.out.println(str1=
=str2;
//true
stringstr1=newstring(ABC );
Stringstr2='abc ';
system.out.println(str1.equals ) str2);
//true
stringstr1=newstring(ABC );
stringstr2=newstring(ABC );
system.out.println(str1.equals ) str2);
//true
5
.变量为
n
的初始值设定
,来定义您自己的自订外观
1678
,
然后利用除法和剩余运算来改变
的各数字并打印,然后输出结果
如下所示。
n=1678。
n
的各个数字是
1
,
6
,
7
,
8。
如果
n
是任意值吗?
法一:
公共类执行5 {
公共
静态
void
main(string[]
args ()。
intn=1678;
intunit;
intdecimal;
inthundred;
intthousand;
intcount;
thousand=n/1000;
count=n00;
hundred=count/100;
count=count0;
decimal=count/10;
计数=计数器;
单元=计数;
system.out.println('1678
包含的数量
各个字如下。
' thousand ',' hundred ',' decimal ',
' unit;
}
}
//
如果
n
任意值
importjava.io.*;
公共类执行51 {
公共
静态
void
main(string[]
args )
throwsIOException{
System.out.print (
请讲
会输的
因
一
个
管教
数量:
();
InputStreamReaderisStream=new
输入流读取器(system.in;
BufferedReaderbfReader=new
缓冲读我(is stream;
斯汀
input=bfReader.readLine (;
intlength=input.length((-1;
intn=new