import java.awt.*;
站在用戶的角度思考問題,與客戶深入溝通,找到梧州網(wǎng)站設(shè)計(jì)與梧州網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:成都做網(wǎng)站、網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、域名與空間、虛擬空間、企業(yè)郵箱。業(yè)務(wù)覆蓋梧州地區(qū)。
import javax.swing.*;
import java.awt.event.*;
public class Teacher {
int numberOne,numberTwo;
String operator="";
boolean right;
public int giveNumberOne(int n){
numberOne=(int)(Math.random()*n)+1;
return numberOne;
}
public int giveNumberTwo(int n){
numberTwo=(int)(Math.random()*n)+1;
return numberTwo;
}
public String giveOperator(){
double d=Math.random();
if(d=0.8)
operator="+";
else if(d0.8d0.5)
operator="-";
else if(d=0.5d0.2)
operator="*";
else if(d=0.2)
operator="/";
return operator;
}
public boolean getRight(int answer){
if(operator.equals("+")){
if(answer==numberOne+numberTwo)
right=true;
else
right=false;
}
else if(operator.equals("-")){
if(answer==numberOne-numberTwo)
right=true;
else
right=false;
}
else if(operator.equals("*")){
if(answer==numberOne*numberTwo)
right=true;
else
right=false;
}
else if(operator.equals("/")){
if(answer==numberOne/numberTwo)
right=true;
else
right=false;
}
return right;
}
}
public class ComputerJFrame extends JFrame implements ActionListener{
TextField textOne,textTwo,textResult;
Button getProblem,giveAnswer;
Label operatorLabel,message;
Teacher teacher;
ComputerJFrame(String s){
super(s);
teacher = new Teacher();
setLayout(new FlowLayout());
textOne = new TextField(10);
textTwo = new TextField(10);
textResult = new TextField(10);
operatorLabel = new Label("+");
message = new Label("你還沒有回答呢");
getProblem = new Button("獲取題目");
giveAnswer = new Button("確認(rèn)答案");
add(getProblem);
add(textOne);
add(operatorLabel);
add(textTwo);
add(new Label("="));
add(textResult);
add(giveAnswer);
add(message);
textResult.requestFocus();
textOne.setEditable(false);//該方法設(shè)置文本框的可編輯性
textTwo.setEditable(false);
getProblem.addActionListener(this);//創(chuàng)建監(jiān)視器
giveAnswer.addActionListener(this);
textResult.addActionListener(this);
setBounds(100,100,450,100);//設(shè)置出現(xiàn)屏幕上時(shí)的初始位置
setLocationRelativeTo(null);//居中
setVisible(true);//窗口的可見性
validate();
addWindowFocusListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==getProblem){
int number1 = teacher.giveNumberOne(100);
int number2 = teacher.giveNumberTwo(100);
String operator = teacher.giveOperator();
textOne.setText(""+number1);//設(shè)置文本框中為參數(shù)字符串指定的文本
textTwo.setText(""+number2);
operatorLabel.setText(operator);
message.setText("請(qǐng)回答");
textResult.setText(null);
}
if(e.getSource()==giveAnswer){
String answer = textResult.getText();
try{ int result = Integer.parseInt(answer);
if(teacher.getRight(result)==true){
message.setText("你回答正確");
}
else{
message.setText("你回答錯(cuò)誤");
}
}
catch(NumberFormatException ex){
message.setText("請(qǐng)輸入數(shù)字字符");
}
}
textResult.requestFocus();
validate();
}
}
public class MainClass {
public static void main(String[] args) {
ComputerJFrame frame;
frame = new ComputerJFrame("算術(shù)測(cè)試");
}
}
import?java.util.Random;
import?java.util.Scanner;
public?class?TestRandom?{
public?static?void?main(String[]?args)?{
Random?rand?=?new?Random();
Scanner?input?=?new?Scanner(System.in);
while(true){
int?a?=?rand.nextInt(100);
int?b?=?rand.nextInt(100);
int?result?=?a+b;
System.out.println(a+"+"+b+"=?");
int??yourGuess=input.nextInt();?
if(yourGuess?==?result){
System.out.println("you?are?right");
}else?{
System.out.println("you?are?wrong");
}
}
}
}
花了好幾個(gè)小時(shí),測(cè)試通過,希望樓主多給點(diǎn)分
import java.util.Scanner;
class Question{
private int firstParam;
private int secondParam;
private String operator;
private int answer;
private int score;
public int getFirstParam() {
return firstParam;
}
public void setFirstParam(int firstParam) {
this.firstParam = firstParam;
}
public int getSecondParam() {
return secondParam;
}
public void setSecondParam(int secondParam) {
this.secondParam = secondParam;
}
public String getOperator() {
return operator;
}
public void setOperator(String operator) {
this.operator = operator;
}
public int getAnswer() {
return answer;
}
public void setAnswer(int answer) {
this.answer = answer;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
//構(gòu)造
public Question(int firstParam, int secondParam) {
this.firstParam = firstParam;
this.secondParam = secondParam;
}
}
public class StudentStudy {
private int questionNumber;
private String operator;
private Question[] questions;
private int rightNumber=0;
private int wrongNumber=0;
public String getOperator() {
return operator;
}
public void setOperator(String operator) {
this.operator = operator;
}
public int getQuestionNumber() {
return questionNumber;
}
public void setQuestionNumber(int questionNumber) {
this.questionNumber = questionNumber;
}
public int getRightNumber() {
return rightNumber;
}
public void setRightNumber(int rightNumber) {
this.rightNumber = rightNumber;
}
public int getWrongNumber() {
return wrongNumber;
}
public void setWrongNumber(int wrongNumber) {
this.wrongNumber = wrongNumber;
}
public Question[] getQuestions() {
return questions;
}
public void setQuestions(Question[] questions) {
this.questions = questions;
}
public int getRandom(){
return (int)(Math.random()*10);
}
public int calculateByOperator(String operator,int first,int second){
switch(operator.charAt(0)){
case '+':
return first+second;
case '-':
return first-second;
default:
return 0;
}
}
public void makeQuestion(){
mywhile:
while(questions[questionNumber-1]==null){
Question question=new Question(getRandom(),getRandom());
for(int i=0;questions[i]!=nulliquestionNumber;i++){
if((question.getFirstParam()==questions[i].getFirstParam())(question.getSecondParam()==questions[i].getSecondParam())){
continue mywhile;
}
}
for(int i=0;iquestionNumber;i++){
if(questions[i]==null){
questions[i]=question;
questions[i].setOperator(operator);
questions[i].setScore(100/questionNumber);
questions[i].setAnswer(calculateByOperator(operator,questions[i].getFirstParam(),questions[i].getSecondParam()));
break;
}
}
}
}
public StudentStudy(int questionNumber){
this.questionNumber=questionNumber;
questions=new Question[this.questionNumber];
}
public static void main(String[] args){
Scanner input=new Scanner(System.in);
System.out.println("現(xiàn)在開始測(cè)試,請(qǐng)?zhí)顚懸獪y(cè)試的試題數(shù)量(1-100):");
int number=input.nextInt();
StudentStudy ss=new StudentStudy(number);
System.out.println("請(qǐng)選擇要測(cè)試的試題類型:1加法2減法");
int type=input.nextInt();
switch(type){
case 1:
ss.setOperator("+");
break;
case 2:
ss.setOperator("-");
break;
default:
System.out.println("輸入有誤,退出系統(tǒng)!");
System.exit(0);
}
ss.makeQuestion();
for(int i=0;inumber;i++){
System.out.println("第"+(i+1)+"題:"+ss.getQuestions()[i].getFirstParam()+ss.getQuestions()[i].getOperator()+ss.getQuestions()[i].getSecondParam()+"=?");
int answer=input.nextInt();
if(ss.getQuestions()[i].getAnswer()==answer){
ss.setRightNumber(ss.getRightNumber()+1);
System.out.println("回答正確!");
}
else{
ss.setWrongNumber(ss.getWrongNumber()+1);
System.out.println("回答錯(cuò)誤!");
}
}
System.out.println("您總共答了"+ss.getQuestionNumber()+"道題,答對(duì)"+ss.getRightNumber()+"道,答錯(cuò)"+ss.getWrongNumber()+"道,最后總分為:"+ss.getRightNumber()*100/ss.getQuestionNumber()+"分");
}
}
你的想法是好的,但是我仔細(xì)構(gòu)思了一遍,太難了。
首先難點(diǎn)就是怎么生成題目。也是最難得,因?yàn)橛刑嗖淮_定,弄不好就會(huì)出現(xiàn)負(fù)數(shù)結(jié)果的題目。
然后是題目的解答,這個(gè)難點(diǎn)就不說了,這個(gè)我在幾年前就寫好了。一直沒用上,現(xiàn)在卻用上了。
還有,你要窗體,可是我不會(huì)用swing
還有你要的四則混合運(yùn)算是多長的,我總不能寫一個(gè)有1000多個(gè)數(shù)字寫成的式子吧。
題目中是否涉及到小數(shù)
下面是我的計(jì)算代碼:
package compute;
public class CaculateString {
private String str, s;
private int p1, p2;
public CaculateString(String str) {
this.str = str;
this.clear();
this.check();
}
public String caculate() {
while (isEnd(str)) {
p2 = str.indexOf(")");
if (p2 != -1) {
s = str.substring(0, p2);
p1 = s.lastIndexOf("(");
s = str.substring(p1 + 1, p2);
if (!s.equals("")) {
s = this.caculateNumber(s);
if (p2 == (str.length() - 1))
str = str.substring(0, p1) + s;
else
str = str.substring(0, p1) + s + str.substring(p2 + 1);
} else {
str = str.substring(0, p1) + str.substring(p2 + 1);
}
} else
str = this.caculateNumber(str);
}
return str;
}
private String caculateNumber(String ss) {
int p3 = 0, p4 = 0, p5 = 0;
char b = ' ', c = ' ';
String s1 = null, s2 = null;
double d1 = 0.0, d2 = 0.0, d3 = 0.0;
if (ss.charAt(0) == '+') {
ss = ss.substring(1);
if (ss.indexOf("+") == -1 ss.indexOf("-") == -1
ss.indexOf("*") == -1 ss.indexOf("/") == -1)
return ss;
} else if (ss.charAt(0) == '-') {
if (ss.indexOf("+", 1) == -1 ss.indexOf("-", 1) == -1
ss.indexOf("*", 1) == -1 ss.indexOf("/", 1) == -1)
return ss;
}
while (isEnd2(ss)) {
p3 = 0;
p4 = 0;
p5 = 0;
if (ss.indexOf("*") == -1 ss.indexOf("/") == -1) {
for (int i = 0; i ss.length(); i++) {
b = ss.charAt(i);
if (b == '+' || b == '-') {
if (p3 == 0 i != 0) {
p3 = i;
c = b;
} else if (p3 != 0 i != (p3 + 1)) {
p4 = i;
break;
}
}
}
if (p4 == 0) {
s1 = ss.substring(0, p3);
s2 = ss.substring(p3 + 1);
} else {
s1 = ss.substring(0, p3);
s2 = ss.substring(p3 + 1, p4);
}
d1 = Double.parseDouble(s1);
d2 = Double.parseDouble(s2);
if (c == '+')
d3 = d1 + d2;
else if (c == '-')
d3 = d1 - d2;
if (p4 == 0)
ss = new Double(d3).toString();
else
ss = new Double(d3).toString() + ss.substring(p4);
} else {
if (ss.indexOf("*") == -1) {
c = '/';
p4 = ss.indexOf("/");
} else if (ss.indexOf("/") == -1) {
c = '*';
p4 = ss.indexOf("*");
} else if (ss.indexOf("*") ss.indexOf("/")) {
c = '/';
p4 = ss.indexOf("/");
} else if (ss.indexOf("*") ss.indexOf("/")) {
c = '*';
p4 = ss.indexOf("*");
}
for (int i = p4 - 1; i = 0; i--) {
b = ss.charAt(i);
if (b == '+' || b == '-') {
if ((i - 1) != 0 ss.charAt(i - 1) != '+'
ss.charAt(i - 1) != '-') {
p3 = i;
break;
} else if ((i - 1) != 0
(ss.charAt(i - 1) == '+' || ss.charAt(i - 1) == '-')) {
p3 = i - 1;
break;
}
}
}
for (int i = p4 + 1; i ss.length(); i++) {
b = ss.charAt(i);
if ((b == '+' || b == '-' || b == '*' || b == '/')
(i - 1) != p4) {
p5 = i;
break;
}
}
if (p3 == 0)
s1 = ss.substring(0, p4);
else
s1 = ss.substring(p3 + 1, p4);
if (p5 == 0)
s2 = ss.substring(p4 + 1);
else
s2 = ss.substring(p4 + 1, p5);
d1 = Double.parseDouble(s1);
d2 = Double.parseDouble(s2);
if (c == '*')
d3 = d1 * d2;
else if (c == '/')
d3 = d1 / d2;
if (p3 == 0 p5 == 0)
ss = new Double(d3).toString();
else if (p3 == 0 p5 != 0)
ss = new Double(d3).toString() + ss.substring(p5);
else if (p3 != 0 p5 == 0)
ss = ss.substring(0, p3 + 1) + new Double(d3).toString();
else if (p3 != 0 p5 != 0)
ss = ss.substring(0, p3 + 1) + new Double(d3).toString()
+ ss.substring(p5);
}
}
return ss;
}
private boolean isEnd(String str) {
if (str.indexOf("(") != -1 || str.indexOf(")") != -1
|| str.indexOf("+", 1) != -1 || str.indexOf("-", 1) != -1
|| str.indexOf("*", 1) != -1 || str.indexOf("/", 1) != -1)
return true;
return false;
}
private boolean isEnd2(String ss) {
if (ss.indexOf("+", 1) != -1 || ss.indexOf("-", 1) != -1
|| ss.indexOf("*", 1) != -1 || ss.indexOf("/", 1) != -1)
return true;
return false;
}
private void clear() {
str = str.replaceAll(" ", "");
}
private void check() {
int right = 0, left = 0;
for (int i = 0; i str.length(); i++) {
char a = str.charAt(i);
if (a != '0' a != '1' a != '2' a != '3' a != '4'
a != '5' a != '6' a != '7' a != '8' a != '9'
a != '+' a != '-' a != '*' a != '/' a != '('
a != ')' a != '.') {
System.exit(0);
}
if (a == '(')
left++;
else if (a == ')')
right++;
}
if (left != right) {
System.exit(0);
}
}
public static void main(String args[]) {
System.out.println(new CaculateString("((1-(0/3))*7)+8").caculate());
}
}
你好,按照你的要求代碼如下,可以直接運(yùn)行
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class test {
public static void main(String[] args) {
int count = 0;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
for (int i = 0; i 10; i++) {
int a = (int) (Math.random() * 100);
int b = (int) (Math.random() * 100);
int c = (int) (Math.random() * 2);
int r = 0;
switch (c) {
case 0:
if (a + b = 100) {
a = 100 - b;
}
System.out.print(a + " + " + b + " = ");
r = a + b;
break;
case 1:
if (a b) {
int t = a;
a = b;
b = t;
}
System.out.print(a + " - " + b + " = ");
r = a - b;
}
int input;
try {
input = Integer.valueOf(in.readLine());
} catch (Exception e1) {
System.out.println("答案錯(cuò)誤\n");
continue;
}
if (input == r) {
count++;
System.out.println("答案正確\n");
} else {
System.out.println("答案錯(cuò)誤\n");
}
}
if (null != in) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("答對(duì)數(shù)量:" + count + "\n得分: " + count * 10);
}
}