JAVA程序设计 急要 谢谢~~~

JAVA程序设计 急要 谢谢~~~,第1张

class Complex{

private int realPart;

private int imaginPart;

Complex(){

thisrealPart = 0;

thisimaginPart = 0;

}

Complex(int realPart, int imaginPart){

thisrealPart = realPart;

thisimaginPart = imaginPart;

}

public void setRealPart(int realPart){

thisrealPart = realPart;

}

public void setImaginPart(int imaginPart){

thisimaginPart = imaginPart;

}

public int getRealPart(){

return thisrealPart;

}

public int getImaginPart(){

return thisimaginPart;

}

Complex complexAdd(Complex a){

int realPart = thisrealPart + agetRealPart();

int imaginPart = thisimaginPart + agetImaginPart();

return new Complex(realPart, imaginPart);

}

public String toString(){

if(thisimaginPart == 0) return "" + thisrealPart;

else if(thisimaginPart > 0) return "" + thisrealPart + "+" + thisimaginPart + "i";

else return "" + thisrealPart + thisimaginPart + "i";

}

}

public class TestComplex{

public static void main(String[] args){

Complex a = new Complex(1,2);

Complex b = new Complex(3,4);

Complex sum = acomplexAdd(b);

Systemoutprintln("(" + a + ")" + "+" + "(" + b + ")" + "=" + "(" + sum + ")");

asetRealPart(2);

asetImaginPart(3);

bsetRealPart(bgetRealPart() 2);

bsetImaginPart(bgetImaginPart() 2);

sum = acomplexAdd(b);

Systemoutprintln("(" + a + ")" + "+" + "(" + b + ")" + "=" + "(" + sum + ")");

}

}

public class Char

{

public static void main(String []s)

{

char x1='一';

char x2='二';

char x3='三';

char x4='四';

Systemoutprintln(x1+x2+x3+x4);

Systemoutprintln(x1);

Systemoutprintln(x2);

Systemoutprintln(x3);

Systemoutprintln(x4);

}

}

输出结果

--------------------Configuration: <Default>--------------------

82288

Process completed

汉字是可以用char 的

为什么第一个输出是82288

原因是因为进行了 "+"运算

对于char的+运算 是unicode码的加减

对于String的 +运算 是字符串的拼接

给你写完了 很容易的:

/ Catalanjava

Contains 2 methods factorial and cata

catalan calculates the n-th catalan number

factorial calculates the factorial recursively

Receive: n

Return: n-th catalan number

/

public class Catalan {

private long factorial(int n) {

if (n == 0) {

return 1;

} else {

return n factorial(n - 1);

}

}

public long calCatalan(int n) {

return factorial(2 n) / (factorial(n + 1) factorial(n));

}

}

import javautilScanner;

import javautilregexPattern;

/

Calculates the value of a specific Catalan number

Input: integer value

Output: n-th Catalan number

/

public class CatalanDemo {

public static void main(String[] args) {

Scanner in = new Scanner(Systemin);

Catalan catalan = new Catalan();

while (true) {

Systemoutprintln("Which catalan number do you want to calculate (-1 to stop): ");

String s = innextLine();

if (Patternmatches("[0-9]", s)) {

int n = IntegerparseInt(s);

Systemoutprint("n=" + n);

Systemoutprintln(": catalan number is " + catalancalCatalan(n));

} else {

if (Patternmatches("-[0-9]", s)) {

int n = IntegerparseInt(s);

if (n == -1) {

Systemoutprintln("(program exits)");

break;

} else {

Systemoutprintln("Invalid input:\nValid input range is an input >= 0");

}

}

}

}

}

}

以上就是关于JAVA程序设计 急要 谢谢~~~全部的内容,包括:JAVA程序设计 急要 谢谢~~~、Java程序设计、程序设计java等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/9469536.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-28
下一篇 2023-04-28

发表评论

登录后才能评论

评论列表(0条)

保存