1、
(1)源程序如下:
public class One {
public static void main(String[] args) {
String name = "张三"
int age = 23
char sex = '男'
String myclass = "某某专业2班"
System.out.println("姓名:" + name)
System.out.println("姓名:" + age)
System.out.println("姓名:" + sex)
System.out.println("姓名:" + myclass)
}
}
(2)
编写完程序的后缀名是.java,如本题,文件名就是One.java。
开始\运行\cmd,进入“命令提示符窗口”,然后用javac编译器编译.java文件,语句:javac One.java。
(3)
编译成功后,生成的文件名后缀是.class,叫做字节码文件。再用java解释器来运行改程序,语句:java One
2、编写程序,输出1到100间的所有偶数
(1)for语句
public class Two1 {
public static void main(String[] args) {
for(int i=2i<=100i+=2)
System.out.println(i)
}
}
(2)while语句
public class Two2 {
public static void main(String[] args) {
int i = 2
while (i <= 100) {
System.out.println(i)
i += 2
}
}
}
(3)do…while语句
public class Two3 {
public static void main(String[] args) {
int i = 2
do {
System.out.println(i)
i += 2
}while(i<=100)
}
}
3、编写程序,从10个数当中找出最大值。
(1)for循环
import java.util.*
public class Three1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in)
int number
int max = 0
for (int i = 0i <10i++) {
System.out.print("输入第" + (i + 1) + "个数:")
number = input.nextInt()
if (max <number)
max = number
}
System.out.println("最大值:" + max)
}
}
(2)while语句
import java.util.*
public class Three2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in)
int number
int max = 0
int i = 0
while (i <10) {
System.out.print("输入第" + (i + 1) + "个数:")
number = input.nextInt()
if (max <number)
max = number
i++
}
System.out.println("最大值:" + max)
}
}
(3)do…while语句
import java.util.*
public class Three3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in)
int number
int max = 0
int i = 0
do {
System.out.print("输入第" + (i + 1) + "个数:")
number = input.nextInt()
if (max <number)
max = number
i++
}while(i<10)
System.out.println("最大值:" + max)
}
}
4、编写程序,计算从1到100之间的奇数之和。
(1)for循环
public class Four1 {
public static void main(String[] args) {
int sum=0
for(int i = 1i<=100i+=2){
sum+=i
}
System.out.println("1~100间奇数和:" + sum)
}
}
(2)while语句
public class Four2 {
public static void main(String[] args) {
int sum = 0
int i = 1
while (i <= 100) {
sum += i
i += 2
}
System.out.println("1~100间奇数和:" + sum)
}
}
(3)do…while语句
public class Four3 {
public static void main(String[] args) {
int sum = 0
int i = 1
do {
sum += i
i += 2
} while (i <= 100)
System.out.println("1~100间奇数和:" + sum)
}
}
5、
(1)什么是类的继承?什么是父类?什么是子类?举例说明。
继承:是面向对象软件技术当中的一个概念。如果一个类A继承自另一个类B,就把这个A称为"B的子类",而把B称为"A的父类"。继承可以使得子类具有父类的各种属性和方法,而不需要再次编写相同的代码。在令子类继承父类的同时,可以重新定义某些属性,并重写某些方法,即覆盖父类的原有属性和方法,使其获得与父类不同的功能。另外,为子类追加新的属性和方法也是常见的做法。继承需要关键字extends。举例:
class A{}
class B extends A{}
//成员我就不写了,本例中,A是父类,B是子类。
(2)编写一个继承的程序。
class Person {
public String name
public int age
public char sex
public Person(String n, int a, char s) {
name = n
age = a
sex = s
}
public void output1() {
System.out.println("姓名:" + name + "\n年龄:" + age + "\n性别:" + sex)
}
}
class StudentPerson extends Person {
String school, department, subject, myclass
public StudentPerson(String sc, String d, String su, String m, String n,
int a, char s) {
super(n, a, s)
school = sc
department = d
subject = su
myclass = m
}
public void output2() {
super.output1()
System.out.println("学校:" + school + "\n系别:" + department + "\n专业:"
+ subject + "\n班级:" + myclass)
}
}
public class Five2 {
public static void main(String[] args) {
StudentPerson StudentPersonDemo = new StudentPerson("某某大学", "某某系别",
" 某专业", "某某班级", " 张三", 23, '男')
StudentPersonDemo.output2()
}
}
class Phone{private String phonenumber
public void setPhonenumber(String phonenumber){
this.phonenumber=phonenumber
}
public String getPhonenumber(){
return phonenumber
}
public void recCall(){
System.out.println("接到一个电话")
}
public void telCall(){
System.out.println("拨出一个电话")
}
}class Fixedphone extends Phone{
private String phonenumber//号码是私有,设置为private,不可继承
public void recCall(){
System.out.println("以"+this.phonenumber+"呼出了一个电话") //重载了父类的recCall
}
}class Cordlessphone extends Fixedphone{
private String phonenumber
public void info(){
System.out.println("这是无绳电话的信息")
}
}interface Moveable{
public void moveinfo()
}class Mobilephone extends Phone implements Moveable{
private String phonenumber
public void moveinfo(){
System.out.println("我实现了可移动性")
}
}public class PhoneTest{
public static void main(String a[]){
Phone[] p=new Phone[5]
Phone p1=new Phone()
p1.setPhonenumber("123456789")
p[0]=p1
Phone p2=new Phone()
p2.setPhonenumber("987654321")
p[1]=p2
Mobilephone mp=new Mobilephone()
mp.setPhonenumber("11111")
p[2]=mp
Fixedphone fp=new Fixedphone()
fp.setPhonenumber("22222")
p[3]=fp
Cordlessphone cp=new Cordlessphone()
cp.setPhonenumber("33333")
p[4]=cp
for(int i=0i<p.lengthi++){
System.out.println(p[i].getPhonenumber())
} p[4]=p[1]
System.out.println(p[4].getPhonenumber())
}} 写的不是很好,希望对你有帮助噶
第一种:方式借助于,while循环获取,提示输入内容获取输入值,然后判断如果余数为5结束循环。
int i = 0
do{
System.out.println("请输入数据边界值:")
//获取输入数字
Scanner sc = new Scanner(System.in)
int s = sc.nextInt()
i = s%10
if(i == 5){
System.out.println(s)
sc.close()
}
}while( i == 5 )
引入类:
第二种:用for循环实现可以控制一定的循环次数。
public static void testFor(){
for (int i = 0i <= 10 i++) {
System.out.println("请输入数据边界值:")
//获取输入数字
Scanner sc = new Scanner(System.in)
int s = sc.nextInt()
int b = 0
b = s%10
if(b == 5){
System.out.println(s)
sc.close()
break
}
}
}
第三种:让用户输入一个字符串,数字按逗号分隔,然后判断其中有多少数字余数为5
//实现输入一个字符窜数字集合数字以逗号分隔
public static void testString(){
System.out.println("请输入数据边界值:")
//获取输入数字
Scanner sc = new Scanner(System.in)
String s = sc.next()
if(s != null &&s.length() >0){
String [] sNum = s.split(",")
for (int i = 0i < sNum.length i++) {
int sn = Integer.valueOf(sNum[i])
int b = 0
b = sn % 10
if(b == 5){
System.out.println(sn)
}
}
}
//关闭输入流
sc.close()
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)