Java 问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java 问题相关的知识,希望对你有一定的参考价值。

1. The following pieces of code contain programming errors. Identify programming error in each piece of code and explain the error.

(a) class A public void f(B b) System.out.println(b.getNumber());

class B private int getNumber() return 5;

(b) class A public void f(B b) int n = b.getNumber(); System.out.println(n);

class B public String getNumber() return “5”;

(c) class A public void f(int x) int x = 10; System.out.println(x);

(d) class A public void f() int x = 10; System.out.println(x); public int s()return x;

(e) class A public int f(int x) if (x>0) return 2*x;

2. What is the output of each of the following 2 pieces of Java code? Explain why the 2 outputs are different.

(a) public class Apublic static void main(String[] args)int x=10; int y=20; Swapper s=new Swapper(); s.swap(x,y); System.out.println(x+” ”+y);

class Swapper() public void swap(int n, int k)int temp; temp = n; n = k; k = temp; System.out.println(n+” ”+k);

(b) public class Apublic static void main(String[] args)C c=new C(); c.x=10; c.y=20; Swapper s=new Swapper(); s.swap(c); System.out.println(c.x+” ”+c.y);

class Swapper public void swap(C c)int temp; temp = c.x; c.x = c.y; c.y = temp; System.out.println(c.x+” ”+c.y);

class C public int x, y;

3. Convert the following for-loop to an equivalent while-loop.

for (int i = 0; i < 10; i++) System.out.println(i);

4. Convert the following while-loop to an equivalent for-loop.

int k = 1, n = 1;

while ( k <= 10)

n = n * k;

k++;



5. Convert the following while(condition)-loop to an equivalent while(true)-loop with break.

String s = scanner.next();

while (s != “0”)

System.out.println(s);

S = scanner.next();



英文答得追50分

1:
1.a:at pieces of code 'b.getNumber()' there has error 'The method getNumber() from the type B is not visible' because in class B the method getNumber() is private.
1.b:at pieces of code 'int n = b.getNumber()' there has error 'cannot convert from String to int' because b.getNumber() is type String and n is type int.
1.c:at pieces of code 'f(int x) int x = 10; System.out.println(x);
' there has error 'cannot convert from String to int' because there has Duplicate local variable x int x and int x = 10;
1.d:at pieces of code 'return x there has error 'x cannot be resolved'' because variable x has not defined.
1.e:at pieces of code 'f(int x)' there has error 'This method must return a result of type int' because there only access if will return a result.
----------------------------------------------------------------------------------------------------------
2:
2.a:there has a Syntax error at 'class Swapper() ' because define class not need '()';
2.b:there will output:
20 10
20 10
----------------------------------------------------------------------------------------------------------
3:while-loop code:
int i = 0;
while(i<10)
System.out.println(i);
i++;

----------------------------------------------------------------------------------------------------------
4:for-loop code:
for(int k=1,n=1;k<=10;k++)
n = n * k;

----------------------------------------------------------------------------------------------------------
5:while(true)-loop with break code:
whie(true)
System.out.println(s);
S = scanner.next();
if(s=="0")
break;


你第二题的a):可能是写错了的,如果把语法错误修改过来接过如下:
a:there will output:
20 10
10 20
b:there will output:
20 10
20 10
explain cause:
a:the variable x ,y is the the value at memory,so while at class Swapper method swap(int x,int y) change x,y will not affect the variable x ,y at class A;
b:the variable c is the the reference at memory,so while at class Swapper method swap(C c) change c will affect the variable c at class A;
参考技术A 1.(a)class B private->public
(b)class A int->String
(c)Delete the data type method of int
(d) variable X is undefined in Method s()
(e)method must return a result of type int
2.先写这么多,有空再写,这会忙死了

以上是关于Java 问题的主要内容,如果未能解决你的问题,请参考以下文章

java问题

java问题

java问题

Java中String问题

java 小问题

JAVA排序的问题