其他分享
首页 > 其他分享> > hellow

hellow

作者:互联网

编程题
1.请编写一个实现如下功能的Application:比较从键盘输入的两个整数是否相等,并根据比较结果显示“相等”或“不相等”。
import java.util.Scanner;
public class COMPare {
public static void main(String args []){
Scanner tr=new Scanner(System.in);
int b=tr.nextInt();
int a=tr.nextInt();
if(a==b)
System.out.println("相等");
else
System.out.println("不相等");
}
}


2. 编写一个字符界面的Java Application 程序,接受用户输入的10个整数,并输出这10个整数的最大值和最小值。
答:import java.io.* ;
public class abc
{
public static void main(String args[ ])
{ int i , n = 10 , max = 0 , min = 0 , temp = 0;
try {
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
max = min = Integer.parseInt(br.readLine( ));
} catch ( IOException e ) { } ;
for ( i = 2 ; i <= n ; i ++ ) {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
temp = Integer.parseInt(br.readLine( ));
if (temp > max ) max=temp;
if (temp < min) min=temp;
} catch ( IOException e ) { } ;
}
System.out.println("max="+max+"\nmin="+min);
}
}


3.编写一个Application,利用数组求出”HELLO”,”JAVA””PROGRAM”三个字符串的平均长度。
class Average {
public static void main(String args[]) {
String array[] = new String[3];
array[0] = "HELLO";
array[1] = "JAVA";
array[2] = "PROGRAM";
int total = array[0].length();
total += array[1].length();
total += array[2].length();
System.out.println("平均字符串长度为: " + total/3);
}
}

4.按以下要求编写程序
(1) 创建一个Rectangle类,添加width和height两个成员变量
(2) 在Rectangle中添加两种方法分别计算矩形的周长和面积
(3) 编程利用Rectangle输出一个矩形的周长和面积

解答:、public class Rectangle {
float width, height;

public Rectangle(float width, float height) {
this.width = width;
this.height = height;
}

public float getLength(){
return (this.width + this.height) * 2;
}

public float getArea(){
return this.width * this.height;
}

public static void main(String [] args) {
Rectangle rect = new Rectangle(10, 20);
System.out.println("周长是:" + rect.getLength());
System.out.println("面积是:" + rect.getArea());
}
}

5.阅读下面的程序,在空线处填上恰当的代码
public class car{
__ private String type ____;//定义用于存放车类型的私有字符串变量type
private String brand;//用于存放车品牌的变量
public car(String type,String brand){
this.type=type;
______ this.brand=brand _____;//对成员变量进行赋值
}
public void printInfo(){
System.out.println(“类型:”+type);
System.out.println(“品牌:”+brand);
}
}
______ class test ___//定义类test
{
public static void main(String args[]){
car c1= ________ new car(“轿车”,“宝马”) ______;
//取类型为:“轿车”,品牌为:“宝马”,生成car类实例c1
__________ c1.printInfo()______________;
//调用printInfo()方法,输出实例c1的信息;
}
}

6.编写一个Java Application,接收从键盘输入的10个整数,并输出其中的最大、最小以及平均值。要求:将计算功能定义在一个类中,由主类接收输入,再用数组的方式传递给实现计算的类的方法,再输出结果。
public class Complex{
private float shibu;
private float xubu;
Complex()
{
this(0,0);
}
Complex(float shibu,float xubu){
this.shibu=shibu;
this.xubu=xubu;
}

public void Add(Complex p)
{
Complex result=new Complex();
result.shibu=this.shibu+p.shibu;
result.xubu=this.xubu+p.xubu;
System.out.print("加法果为:"+result.shibu+"+"+result.xubu+"i");
}

public static void main(String[] args) {
Complex fushu1=new Complex(1,2);
Complex fushu2=new Complex(3,4);
fushu1.Add(fushu2);
}
}

标签:String,System,Complex,new,hellow,public,out
来源: https://www.cnblogs.com/hongyixiaojiang/p/15729401.html