2024年4月15日发(作者:)
Java练习题
四、运行程序写结果
1. 当在命令提示符后输入”java SumTest 4 5”之后,下列程序的输出结果为:
4+5=9
public class SumTest
{
public static void main(String[] args)
{
int a, b, sum;
a=nt(args[0]);
b=nt(args[1]);
sum=a+b;
n(a + "+" + b + "=" + sum);
1
}
}
2. 当在命令提示符后输入”java PrintStar 8”之后,下列程序的输出结果为:
********
public class PrintStar {
public static void main(String[] args) {
int n = nt(args[0]);
int i;
for( i=1; i<=n; i++)
("*");
n();
}
}
2
3. 当在命令提示符后输入”java CalculateTest 5.5”之后,下列程序的输出结果为:
10.0
public class CalculateTest{
public static void main(String args[]){
float x,y;
x = loat(args[0]);
if(x<1)
y = x;
else if(1<=x&&x<10)
y = 2*x-1;
else
y = 3*x-11;
n(y);
3
}
}
4. 当在命令提示符后输入”java Factorial3 3”之后,下列程序的输出结果为:
结果是:6
public class Factorial3 {
public static void main(String[] args) {
int num = nt(args[0]);
int result = 1;
if(num<1)
n("应输入大于1的整数!");
else{
int i;
for( i=1; i<=num; i++)
4
result = result*i;
n("结果是:"+result);
}
}
}
5. 下列程序的输出结果为: null
public class X{
public static void main(String [] args)
{
String names[]=new String[5];
for(int x=0;x<;x++)
names[x]=args[x];
n(names[2]);
5
}
}
6. 下列程序的输出结果为: 9.5 10.0 8
public class Test{
public static void main(String[] args) {
( 3.5+1/2+56%10+" " );
( 3.5+1.0/2+56%10+" " );
int a = 4%3*7+1;
n( a );
}
}
a[0] = 1 a[1] = 2 a[2] = 3
7. 下列程序的输出结果为: a[3] = 4 a[4] =5
6
public class A1 {
public static void main(String args[]) {
int a[]=new int[5];
a[0]=1; a[1]=2; a[2]=3; a[3]=4; a[4]=5;
("a["+0+"] = "+a[0]+" ");
("a["+1+"] = "+a[1]+" ");
n("a["+2+"] = "+a[2]+" ");
("a["+3+"] = "+a[3]+" ");
("a["+4+"] = "+a[4]);
}
}
8. 下列程序的输出结果为: a[4]=4, a[3]=3, a[2]=2, a[1]=1,a[0]=0,
public class array1D_1 {
7
public static void main(String args[]) {
int i;
int a[ ];
a = new int[5];
for(i=0;i<5;i++)
a[i]=i;
for(i=-1;i>=0;i--)
(“a[“ +i + ”]=” + a[i] + ”,t”);
}
}
9. 运行结果是: Hi ! Like
public class Test1 {
String str = new String("Hi !");
8
char[] ch = { 'L', 'i', 'k', 'e' };
public static void main(String args[]) {
Test1 ex = new Test1();
System.
out
.print( + " ");
System.
out
.print();
}
}
10. 运行结果是: 4 6 8 10 12 14 16 18 20
public class Test3 {
public static void main(String[] args) {
int i;
for (i = 3; i <= 20; i ++) {
if ((i % 2) == 0)
9
System.
out
.print(i + " ");
}
}
}
11. 运行结果是: i=21,sum=108
public class Test4 {
public static void main(String[] args) {
int i,sum=0;
for (i = 3; i <= 20; i ++)
if ((i % 2) == 0) sum=sum+i;
System.
out
.print("i="+i+",sum="+sum);
}
}
10
12. 运行结果是: 2500
public class Test5 {
public static void main(String[] args) {
int i,sum=0;
for (i = 1; i <= 100; i ++) {
if ((i % 2) == 1)
sum=sum+i;
}
n(sum);
}
}
13. 运行结果是: 0
1 2
11
3 4
5 6
7 8
9
class Test6 {
public static void main(String[] args) {
int i;
for(i=0;i<10;i++){
(i+" ");
if (i%2!=0) continue;
n(" ");
}
}
12
}
14、运行结果是: 3 5 7 11 13
17 19 23 29
public class Test14 {
public static void main(String[] args) {
int n = 1, m, j, i;
for (i = 3; i <= 30; i += 2) {
m = (int) ((double) i);
for (j = 2; j <= m; j++)
if ((i % j) == 0)
break;
if (j >= m + 1) {
(i + " ");
13
if (n % 5 == 0)
("n");
n++;
}
}
}
}
15. 下列程序的输出结果为: 1 4 9 16 25 36 49 64 81 100 total is 385
public class Test1 {
public static void main(String args[]) {
int y, x=1, total=0;
while(x<=10) {
y=x*x;
14
(y+" ");
total+=y;
++x;
}
("total is "+total);
}
}
16. 下列程序的输出结果为: copy every letter
public class WhileExample{
public static void main(String[] args){
String copyFromMe="Copy every letter until you encounter 'u'.";
StringBuffer copyToMe=new StringBuffer(); //创建一个空的串变量
int i=0;
15
char c=(i);
while(c!='u')
{ (c);
c=(++i); }
n(copyToMe);
}
}
17. 下列程序的输出结果为: Found 1076 at i=1, j=2
public class BreakExample{
public static void main(String[] args){
int[ ][ ] array={{32,87,3},{589,12,1076},{2000,8,622}};
int search = 1076, i=0, j=0;
boolean foundIt = false;
16
for (i=0; i<3; i++) {
for (j=0; j<3; j++){
if (array[i][j] == search) { foundIt = true; break; } //跳出内层循环
}
if(foundIt) break; //跳出外层循环
}
if(foundIt)
n("Found " + search + " at i = " + i +", j = " + j);
else
n(search + " is not in the array");
}
}
18. 下列程序的输出结果为: 其最终和是:110
17
public class SumDemo2 {
public static void main(String[] args) {
int sum = 0, i = 0;
do{
if(i%2==0)
sum = sum+i;
i++;
}while(i<=20);
n("其最终和是:"+sum);
}
}
19. 下列程序的输出结果为: 10+20=30
public class AddTest {
18
public static void main(String[] args) {
int num1,num2;
num1 = 10;
num2 = 20;
n(num1 + "+" + num2 + "=" + add(num1,num2));
}
static int add(int op1,int op2){
return op1+op2;
}
}
20. 下列程序的输出结果为: 7+8=15, 5+8=13
class generalFunction{
19
public static int add(int x,int y){ return x+y; } //静态方法
}
public class StaticFunTest{
public static void main(String[] args){
int c=(7, 8);
("7 + 8 = " + c+ “, “);
generalFunction fun=new generalFunction();
int d= (5, 8);
n("5 + 8 = " + d);
}
}
21. 下列程序的输出结果为 Length:5 width:2 height:4 volumn: 40 area:76
class Box{
20
int length,width,height;
public void setInfo(int l,int w,int h){
length = l;
width = w;
height = h;
}
public int volumn(){
return length*width*height;
}
public int area(){
return (length*width + length*height + width*height) * 2;
}
public String toString(){
21
return "Length:" + length + " width:" + width + " height:" + height
+ " volumn: " + volumn() + " area:" + area();
}
}
public class BoxTest {
public static void main(String[] args) {
Box b = new Box();
o(5,2,4);
n(ng());
}
}
22. 下列程序的输出结果为: good and gbc
public class Example
22
{
String str=new String("good");
char[]ch={'a','b','c'};
public static void main(String args[])
{
Example ex=new Example();
(,);
(+" and ");
();
}
public void change(String str,char ch[])
{
str="test ok";
23
ch[0]='g';
}
}
23. 下列程序的输出结果为: 最小的数为:3
// 以数组为参数的方法调用
public class arrayParam1{
public static void main(String args[ ]){
int a[ ] = {8, 3, 7, 88, 9, 23};
LeastNumb MinNumber=new LeastNumb();
(a);
}
}
class LeastNumb{
24
public void least(int array[ ]){
int temp = array[0];
for(int i=0;i<;i++){
if(temp>array[i]) temp = array[i];
}
n("最小的数为: " + temp);
}
}
24. 运行结果是:=31
=40
=32
=40
import .*;
25
import .*;
class MemberVar {
static int
sn
= 30;
final int fn;
final int fk = 40;
MemberVar() {
fn = ++
sn
;
}
}
public class Test10 extends Applet {
public void paint(Graphics g) {
MemberVar obj1 = new MemberVar();
MemberVar obj2 = new MemberVar();
26
ring("=" + , 20, 30);
ring("=" + , 20, 50);
ring("=" + , 20, 70);
ring("=" + , 20, 90);
}
}
25. 下列程序的输出结果为: y=5
class A
{
static int y=3;
void showy( ){n("y="+y); }
}
class testA
27
{
public static void main(String aaa[])
{ A a1=new A( );
A.y+=1; a1.y++;
( );
}
}
26. 运行结果是: BADCADCA
public class Test9 {
static boolean foo(char c) {
(c);
return true;
}
28
public static void main( String[] argv ) {
int i =0;
for ( foo('B'); foo('A')&&(i<2); foo('C')){
i++ ;
foo('D');
}
}
}
27. 运行结果是: 1
import .*;
public class Test2{
public static void main(String args[]) {
Sub sb = new Sub( );
29
n(( )); }
}
class Father{
int a=3 , b=2 ;
int method(){ return a-b; }
}
class Sub extends Father{
int method2( ) { return a+b; }
}
Aclass
28. 下列程序的输出结果为 Bclass
class Aclass
{
30
void go()
{
n("Aclass");
}
}
public class Bclass extends Aclass
{
void go()
{
n("Bclass");
}
public static void main(String args[]){
Aclass a=new Aclass();
31
Aclass a1=new Bclass();
();
();
}
}
球的半径是10.0
29. 下列程序的输出结果为: 台球的颜色是蓝色,半径是5.0
class Ball{
private double r;
public void setR(double x){
r = x;
}
public double getR(){
32
return r;
}
}
class Billiards extends Ball{
private String color;
public void setCol(String col){
color = col;
}
public void show(){
n("台球的颜色是"+color+",半径是"+getR());
}
}
public class Demo{
33
public static void main(String[] args){
Ball b1 = new Ball();
(10);
n("球的半径是"+());
Billiards b2 = new Billiards();
(5);
("蓝色");
();
}
}
30. 运行结果是: What a pleasure!
I am Tom
How do you do?
34
public class Test12 extends TT{
public static void main(String args[]) {
Test12 t = new Test12 ("Tom");
}
public Test12(String s){
super(s);
n("How do you do?");
}
public Test12(){
this("I am Tom");
}
}
class TT{
35
public TT(){
n("What a pleasure!");
}
public TT(String s){
this();
n("I am "+s);
}
}
31. 运行结果是: What a pleasure!
I am I am Tom
How do you do?
public class Test13 extends TT{
public static void main(String args[]) {
36
Test13 t = new Test13 ( );
}
public Test13(String s){
super(s);
n("How do you do?");
}
public Test13(){
this("I am Tom");
}
}
class TT{
public TT(){
n("What a pleasure!");
37
}
public TT(String s){
this();
n("I am "+s);
}
}
32. 下列程序的输出结果为: 25.0
interface IShape{
public double area();
}
class square implements IShape{
double length ;
public square(double l){
38
length = l;
}
public double area(){
return length*length;
}
}
public class Test {
public static void main(String[] args) {
square s = new square(5);
n(());
}
}
33. 运行结果是: 36
39
class Test11{
class Dog {
private String name;
private int age;
public int step;
Dog(String s, int a) {
name = s;
age = a;
step = 0;
}
public void run(Dog fast) {
++;
}
40
}
public static void main(String args[]) {
Test11 a = new Test11();
Dog d = Dog("Kate", 5);
= 35;
(d);
System.
out
.println(" " + );
}
}
34. 运行结果是: 6 1 12 12
public class Test15 {
private static int count;
private String name;
41
public class Student {
private int count;
private String name;
public void Output(int count) {
count++;
++;
++;
++;
n(count + " "
);
}
}
public Student aStu() {
return new Student();
+
42
" + + " " + " +
}
public static void main(String args[]) {
Test15 g3 = new Test15();
= 10;
t s1 = ();
(5);
}
}
It’s caught!
35. 下列程序的输出结果为: It’s finally caught!
public class Q1{
public static void main(String args[]){
try{throw new MyException();}
43
catch(Exception e){
n(“It’s caught!”);
}
Finally{
n(“It’s finally caught!”);}
}
class MyException extends Exception{}
}
36. 运行结果是: exception000 (B12)
finally111
finished
public class Test10 {
public static void aMethod() throws Exception{
44
try{
throw new Exception();
}
catch(Exception e){
n("exception000");
}
finally{
n("finally111");
}
}
public static void main(String args[]){
try{
aMethod();
45
}
catch(Exception e){
n("exception");
}
n("finished");
}
}
37. 运行结果是: finally111
exception
finished
public class Test10 {
public static void aMethod() throws Exception{
try{
46
throw new Exception();
}
finally{
n("finally111");
}
}
public static void main(String args[]){
try{
aMethod();
}
catch(Exception e){
n("exception");
}
47
n("finished");
}
}
38. 运行结果是:exception000
finished
public class Test10 {
public static void aMethod() throws Exception{
try{
throw new Exception();
}
catch(Exception e){
n("exception000");
}
48
}
public static void main(String args[]){
try{
aMethod();
}
catch(Exception e){
n("exception");
}
n("finished");
}
}
39. 运行结果是: 13423
public class Test1 {
49
public static String output = "";
public static void foo(int i){
try{
if(i==1){throw new Exception();}
output += "1";
}
catch(Exception e){
output += "2";
return;
}
finally{
output += "3";
}
50
output += "4";
}
public static void main(String args[]){
foo(0);
foo(1);
n();
}
}
40. 运行结果是: 相等
public class Test7{
public static void main(String[] args) {
String s1="Java program";
String s2=new String("Java program");
51
if(!((s2))){
n("不相等");
}else{
n("相等");
}
}
}
41. 运行结果是: s1!=s2
class Test8
{
public static void main(String[] args)
{
String s1=" Java program ";
52
String s2=new String("Java program ");
if(s1==s2){
n("s1==s2");
}else{
n("s1!=s2");}
}
}
42. 在输入整数的提示后输入8,在输入符号的提示后输入*,那么下列程序的输出结
果为:
********
import .*;
class CharTest{
public static void main(String[] args){
Scanner reader = new Scanner();
53
n("请输入一个整数");
int n = t();
n("请输入一个符号");
char ch = ().charAt(0);
for(int i = 1; i<=n ;i++)
(ch);
n();
}
}
x is 0
43. 下列程序的输出结果为: y is 0
import .*;
public class PointDemo{
54
public static void main(String args[ ])
{
Frame f=new Frame ("Visual");
e(300,300);
ible(true);
Point p=ation();
n("x is "+ p.x);
n("y is "+ p.y);
}
}
55
发布者:admin,转转请注明出处:http://www.yc00.com/web/1713111997a2185317.html
评论列表(0条)