
正文
用java代码写减法,java减法怎么写
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
希望做一个JAVA一百以内的加减法源代码
import java.util.Scanner;
public class JianJian {
public static void main(String[] args) {
System.out.println("一百以内的加减法");
System.out.println("输入-1,退出系统");
Scanner sc = new Scanner(System.in);
int score = 0;
int nums = 0;
while (true) {
int x = (int) (Math.random() * 2);//随机一个0,1的数字,当数字是1的时候,输出加法,当数字是0的时候,输出减法
int a = (int) (Math.random() * 100);
int b = (int) (Math.random() * 100);
if (x == 1) {
System.out.print(a + "+" + b + "=");
int c = sc.nextInt();
if (c == -1) {
break;
}
nums++;
if (c == (a + b)) {
score = score + 10;
}
} else {
if (a b) {// 排除a-b0的情况.保证结果都是大于0的
b = a + b;
a = b - a;
b = b - a;
}
System.out.print(a + "-" + b + "=");
int c = sc.nextInt();
if (c == -1) {
break;
}
nums++;
if (c == (a - b)) {
score = score + 10;
}
}
}
System.out.println("做了" + nums + "道题目,得分: " + score);
}
}
输出
一百以内的加减法
输入-1,退出系统
87-18=59
23-6=17
72-30=42
97+14=-1//-1表示退出系统,所以不算做一道题
做了3道题目,得分: 20
相关问答
Q1: JAVA的加,减,乘,除运算方法
首先可以把计算器看成是一个对象就是一个类,它有加、减、乘、除功能,这四个就是这个类的方法;你可以给这个类定义两个成员变量
int
x、int
y
然后分别用这四个方法对x
、y
实行加、减、乘、除并返回其值
代码大概如下:
class
counter{
private
int
x;
private
int
y;
public
counter(){
}
public
counter(int
x,int
y){
this.x=x;
this.y=y;
}
public
double
adding(){
//加运算
return
x+y;
}
public
double
minus(){
//减运算
return
x-y;
}
public
double
times(){
//乘运算
return
x*y;
}
public
double
divide(){
//除运算
return
x/y;
}
}
//测试类
public
class
test{
public
static
void
main(string[]
args){
counter
c=new
counter(5,4);//实例化
system.out.println(c.adding());//输出加的结果
system.out.println(c.minus());//输出减的结果
system.out.println(c.times());//输出乘的结果
system.out.println(c.divide());//输出除的结果
}
}
希望对你有帮助
Q2: 用java编写10道,10以内的加减
方式如下:
public class Test {
// 加法
public void add(int a, int b) {
int c = a + b;
System.out.println(a + "+" + b + "=" + c);
}
// 减法
public void subtraction(int a, int b) {
int c = a - b;
System.out.println(a + "-" + b + "=" + c);
}
// 测试
public static void main(String[] args) {
Test test = new Test();
// 加法5道
test.add(5, 6);
test.add(1, 7);
test.add(4, 2);
test.add(0, 0);
test.add(9, 3);
// 减法5道
test.subtraction(7, 2);
test.subtraction(4, 7);
test.subtraction(8, 5);
test.subtraction(9, 8);
test.subtraction(0, 6);
}
}
运行结果:
关于用java代码写减法和java减法怎么写的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。







