「java两位数加减法测试」两位数的加减法有哪些计算方法

博主:adminadmin 2023-01-14 01:42:07 364

本篇文章给大家谈谈java两位数加减法测试,以及两位数的加减法有哪些计算方法对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

一道JAVA作业题(小学生加减法测验)

package my.test.main;

import java.util.Scanner;

public class Exam {

/**

 * @param args

 */

public static void main(String[] args) {

// TODO Auto-generated method stub

Scanner in = new Scanner(System.in);

int score = 0;

// 十次循环,出题

for (int i = 0; i  10; i++) {

// 通过随机产生1和2,来决定这题是加法还是减法

int operate = (int) (Math.random() * 2 + 1);

int result = 0;// 每题的结果

if (operate == 1) {// 1做加法

int num1, num2;

// 产生连个相加不和不超出50的整数

do {

num1 = (int) (Math.random() * 51);

num2 = (int) (Math.random() * 51);

} while (num1 + num2  50);

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 (num1  num2) {

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 (score  90) {

System.out.println("总分为:" + score + ",SMART!");

} else if (score  80  score = 90) {

System.out.println("总分为:" + score + ",GOOD!");

} else if (score  70  score = 80) {

System.out.println("总分为:" + score + ",OK!");

} else if (score  60  score = 70) {

System.out.println("总分为:" + score + ",PASS!");

} else {

System.out.println("总分为:" + score + ",TRY AGAIN!");

}

}

}

java.求两个大数的精确加减法,a=4632864832684683568465765487657665765236465244

花了十分钟,亲手给你订制的,测试过了正确

下面给你贴出源代码

public class AddSub {

public static void main(String[] args) {

String a="4632864832684683568465765487657665765236465244";

String b="47";

int []pa=stringToInts(a);

int []pb=stringToInts(b);

String ans_add=add(pa, pb);

String ans_sub=sub(pb,pa);

System.out.println("相加结果是:"+ans_add);

System.out.println("相减结果是:"+ans_sub);

}

public static int[] stringToInts(String s){

int[] n = new int[s.length()];

for(int i = 0;is.length();i++){

n[i] = Integer.parseInt(s.substring(i,i+1));

}

return n;

}

public static String add(int []a,int []b){

StringBuffer sb=new StringBuffer();

int a_len= a.length-1;

int b_len=b.length-1;

int jinwei=0;//进位

while(a_len=0||b_len=0){

int temp=0;

if(a_len=0b_len=0){

temp=a[a_len]+b[b_len]+jinwei;

}else if(a_len=0){

temp=a[a_len]+jinwei;

}else if(b_len=0){

temp=b[b_len]+jinwei;

}

sb.append(temp%10+"");

jinwei=temp/10;

a_len--;b_len--;

}

return getNum(sb.reverse());

}

public static String sub(int []a,int []b){

StringBuffer sb=new StringBuffer();

boolean flag=false;//判断a是不是比b小

if(a.lengthb.length){

int c[]=a;

a=b;b=c;

flag=true;

}

int a_len= a.length-1;

int b_len=b.length-1;

int jiewei=0;//借位

while(a_len=0||b_len=0){

int temp=0;

if(a_len=0b_len=0){

if((a[a_len]-jiewei)b[b_len]){

temp=a[a_len]+10-b[b_len]-jiewei;

jiewei=1;

}else{

temp=a[a_len]-b[b_len]-jiewei;

}

}else if(a_len=0){

temp=a[a_len]-jiewei;

jiewei=0;

}

sb.append(temp+"");

a_len--;b_len--;

}

if(flag){

return getNum(sb.append("-").reverse());

}

return getNum(sb.reverse());

}

//去掉最前面的0

public static String getNum(StringBuffer sb){

while(sb.length() 1 sb.charAt(0) == '0') {

sb.deleteCharAt(0);

}

return sb.toString();

}

}

编写一个Java程序:利用随机函数产生10道两位数的加法题,判断用户得分

import javax.swing.*;

public class Test {

public static void main(String[] args) {

int score = 0;

for(int i=0; i10; i++) {

int x = (int)(10+Math.random()*90);

int y = (int)(10+Math.random()*90);

int a = x + y;

JOptionPane.showMessageDialog(null, x+"+"+y+"="+"?");

String input = JOptionPane.showInputDialog("请输入结果");

int b = Integer.parseInt(input);

if(b == a) {

score += 10;

JOptionPane.showMessageDialog(null, "回答正确");

}

else JOptionPane.showMessageDialog(null, "回答错误");

}

JOptionPane.showMessageDialog(null, "你的得分为:" + score);

}

}

用JAVA接受用户输入两个数字,进行加减运算

那要看你是用什么IDE来编程的啦,运行java程序要必须首先安装JDK。

1.如果你是用记事本编辑的话。“运行”--“notepad”

输入:

public class Add

{

public static void main(String args[])

{

int a;

int b;

int result;

a=Integer.parseInt(args[0]);

b=Integer.parseInt(args[1]);

result=a+b;

System.out.println(result);

}

}

保存为“Add.java”

“运行”-“CMD”-进入刚才保存的目录

使用“javac(空格)Add.java”编译成class文件

使用“java(空格)Add(空格)2(空格)1”运行

结果:3

还记得public static void main(String[] args)吗?这里的args就是你的启动参数。

在运行时你输入java package1.class1 -arg1 -arg2,args中就会有两个String,一个是arg1,另一个是arg2。

2.如果你是用eclipse3.1.2的话。麻烦点,因为它自带控制台。所以代码在运行时要进行设置:

同样是上面的代码

在eclipse3.1.2中,右击类名“Add.java”在弹出菜单中选择“run as”--“run……”--弹出对话框--在“java application”中,你会看到刚创建的“Add.java”如果没有在左下方,点击“new”--在右面的“(x)=arguments”选项卡中的“program arguments”中输入“1(空格)2”--“apply”--“run”

在下面的“console”即可看到“3”

java两位数加减法测试的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于两位数的加减法有哪些计算方法、java两位数加减法测试的信息别忘了在本站进行查找喔。