测试加减法的JAVA代码 java加减法程序编写( 三 )


do {
num1 = (int) (Math.random() * 51);
num2 = (int) (Math.random() * 51);
} while (num1 + num250);
result = num1 + num2;
System.out.println(num1 + "+" + num2 + "=");
} else if (operate == 2) {// 2做减法
int num1, num2;
// 产生两个整数 , 用大的减去小的
num1 = (int) (Math.random() * 51);
num2 = (int) (Math.random() * 51);
if (num1num2) {
result = num1 - num2;
System.out.println(num1 + "-" + num2 + "=");
} else {
result = num2 - num1;
System.out.println(num2 + "-" + num1 + "=");
}
}
// 三次输入答案机会
for (int j = 1; j = 3; j++) {
int input = in.nextInt();
if (input == result) {
switch (j) {
case 1:
score += 10;
break;
case 2:
score += 7;
break;
case 3:
score += 5;
break;
}
System.out.println("正确!");
break;
} else {
System.out.println("错误!还可以回答" + (3 - j) + "次 。");
}
}
}
if (score90) {
System.out.println("总分为测试加减法的JAVA代码:" + score + ",SMART!");
} else if (score80score = 90) {
System.out.println("总分为:" + score + ",GOOD!");
} else if (score70score = 80) {
System.out.println("总分为:" + score + ",OK!");
} else if (score60score = 70) {
System.out.println("总分为:" + score + ",PASS!");
} else {
System.out.println("总分为:" + score + ",TRY AGAIN!");
}
}
}
java 中实现复数的加减(1)测试加减法的JAVA代码:具体代码(附注释)
复数类测试加减法的JAVA代码:
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 void Sub(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 void Mul(Complex p)
{
Complex result=new Complex();
result.shibu=this.shibu*p.shibu-this.xubu*p.xubu;
result.xubu=this.shibu*p.xubu+p.shibu*this.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);
fushu1.Sub(fushu2);
fushu1.Mul(fushu2);
}
}
(2):提供一个例子:
源代码:
import java.io.*;
public class Book{
double sb;
double xb;
Book(double x,double y){
this.sb=x;
this.xb=y;
}
Book(){
}
public static void main(String args[]){
System.out.println("请输入数据:");
double a=0;
double b=0;
double c=0;
double d=0;
String s;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入第一个复述测试加减法的JAVA代码的实部:");
try{
s = in.readLine();
a=Double.parseDouble(s);
}
catch(IOException e)
{ System.out.println("抛掷异常");}
System.out.println("请输入第一个复述测试加减法的JAVA代码的虚部:");
try{
s = in.readLine();
b =Double.parseDouble(s);
}
catch(IOException e)
{ System.out.println("抛掷异常");}
System.out.println("请输入第二个复述的实部:");

推荐阅读