public class Calculator {
成都创新互联主要从事做网站、网站制作、网页设计、企业做网站、公司建网站等业务。立足成都服务将乐,十年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18982081108
private int number1;
private int number2;
public Calculator(int number1, int number2) {
this.number1 = number1;
this.number2 = number2;
}
public double add() {
return number1 + number2;
}
public double subtract() {
return number1 - number2;
}
public double multiple() {
return number1 * number2;
}
public double divide() {
if(number2 == 0) {
return 0;
}
return number1 / number2;
}
}
public class TestCalculator {
public static void main(String[] args) {
Calculator calculator = new Calculator(5, 2);
System.out.println(calculator.add());
System.out.println(calculator.subtract());
System.out.println(calculator.multiple());
System.out.println(calculator.divide());
}
}
package com.demo;
import java.math.BigDecimal;
import java.util.Random;
import java.util.Scanner;
/*
* Java编写一个能够自动生成小朋友算术练习题目的程序。设计一个程序,要求能够随机生成每个题目。
* 编写一个能够自动生成小朋友算术练习题目的程序。设计一个程序,要求能够随机生成每个题目,并具有以下基本功能:
* (1)要求题目以单项选择题及填空题两种形式出现。
* (2)完成一题后自动判断答案是否正确,分别弹出不同的提示,并自动更新成下一题。
* (3)单击“退出”时,汇总本次计算结果,共完成几题,正确率,并给出相应评语。确定后正式退出。
*/
public class publicDemo2 {
public static void main(String[] args) {
Random random = new Random();
StringBuilder stringBuilder = new StringBuilder();
String[] symbol = {"+","-","*","/"};
String question_tip = "选择题(除法保留小数点后两位):";
stringBuilder.append(question_tip).append("\n");
System.out.println(question_tip);
int a;
int b;
String f;
String result_str;
for (int i=1;i11;i++){
a = random.nextInt(50);
b = random.nextInt(50);
f = symbol[random.nextInt(3)];
String question = i+"、"+a+f+b+"=";
System.out.println(question);
stringBuilder.append(question);
Scanner sc = new Scanner(System.in);
String sc_str = sc.next();
if("/".equals(f)){
double scan_value = Double.parseDouble(sc_str);
double result_value = a / b;
BigDecimal bigDecimal1 = new BigDecimal(scan_value);
scan_value = bigDecimal1.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
BigDecimal bigDecimal2 = new BigDecimal(result_value);
result_value = bigDecimal2.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
result_str = (scan_value==result_value)?"回答正确!":"回答错误!";
stringBuilder.append(scan_value).append(";(答案:"+result_value+")"+result_str).append("\n");
}else{
int scan_value = Integer.parseInt(sc_str);
int result_value;
if("+".equals(f)){
result_value = a + b;
}else if("-".equals(f)){
result_value = a - b;
}else if("*".equals(f)){
result_value = a * b;
}else{
break;
}
result_str = (scan_value==result_value)?"回答正确!":"回答错误!";
stringBuilder.append(scan_value).append(";(答案:"+result_value+")"+result_str).append("\n");
}
}
System.out.println(stringBuilder.toString());
}
}
21题:
int total = 1;
for (int i = 1; i 10; i++) {
total = total * i;
}
22题:
int array[] = new int[]{0, 1, 2, 3};
23题:
if (i 3) {
System.out.println(i);
} else {
System.out.println(3);
}
您好!上面是我写的回答,请确认。