了解学习java基础程序设计_第1页
了解学习java基础程序设计_第2页
了解学习java基础程序设计_第3页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、(浪曦分享)java基础程序设计学习java使用system.out来表示标准输出设备,使用system.in来表示标准输入设备。java并不直接支持控制台输入,但是可以使用scanner类创建它的对象,以读取来自system.in的输入。scanner input = new scanner(system.in);scanner对象的方法:方法描述nextbyte()读取一个byte类型的整数nextshort读取一个short类型的整数nextint()读取一个int类型的整数nextlong()读取一个long类型的整数nextfloat()读取一个float类型的数nextdouble

2、()读取一个double类型的数next()读取一个字符串,该字符串在一个空白符之前结束nextline()读取一行文本,以回车键结束代码1(求圆的面积):12345678910111213141516public class main public static void main(string args) scanner input =new scanner(system.in); system.out.print("enter a number for radius: "); double radius = input.nextdouble(); double are

3、a = radius * radius *3.1415926; system.out.println("the area for the circle of radius " + radius +" is " + area); /*enter a number for radius: 23.45the area for the circle of radius 23.45 is 1727.5696247214998*/代码2(求输入三个数的平均值):123456789101112131415public class main public static

4、void main(string args) scanner input =new scanner(system.in); system.out.print("enter three numbers: "); double num1, num2, num3, average; num1 = input.nextdouble(); num2 = input.nextdouble(); num3 = input.nextdouble(); average = (num1 + num2 + num3) /3; system.out.println("the averag

5、e of" + num1 +" " + num2 +" " + num3 +" is " + average); java中使用final关键字表示一个变量是常量string类型不是基本类型,而是引用类型。可以进行字符串连接,使用“+”连接符,如果操作数之一不是字符串,非字符串值先转换为字符串,再与另一个字符串连接起来“+=”也可以用于字符串12345678910111213141516public class main public static void main(string args) string message

6、="welcome " +"to " +"java" int i =1, j =2; string s ="chapter" +2; string s1 ="supplement" +'b' system.out.println(message); /welcome to java system.out.println(s); /chapter2 system.out.println(s1); /supplementb message +="and java is fu

7、n!" system.out.println("i + j is " + i + j); /i + j is 12 system.out.println("i + j is " + (i + j); /i + j is 3 为从控制台读取字符串,调用scanner对象上的next()方法:1234567891011121314public class main public static void main(string args) scanner input =new scanner(system.in); system.out.printl

8、n("enter three strings: "); string s1 = input.next(); string s2 = input.next(); string s3 = input.next(); system.out.println("s1 is " + s1); system.out.println("s2 is " + s2); system.out.println("s3 is " + s3); next()方法读取以空白字符结束的字符串(' '、't'、'f'、'r'、'n')可以使用nextline()方法读取一整行文本。nextline()方法读取以按下回车键为结束标志的字符串:12345678910public class main public static void main(string args) scanner input =new scanner(system.in); system.out.p

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论