我如何在循环中添加一串代码并在特定推荐

时间:2018-06-02 10:29:18

标签: java debugging

这是我的java程序,这是它的工作原理。首先,它要求用户输入他/她的名字,然后显示用户得分和旁边的名字,然后它要求第二个用户输入数学方程式,而另一个用户必须计算出方程式,如果他做得对,他得到10如果错误他得不到积分,用户可以在玩家完成解决他的等式之后退出,而用户需要退出"退出"然后它输出他们的分数,然后计算谁拥有较高分数,如果玩家1得分较高,那么玩家2则表示"玩家名称"胜!或相反,如果用户具有相同的分数,它只是说"它是一个平局!"

 import java.util.*;
    public class Mathgame {
    public static void main(String[] args) {
        String[] player =new String[3];
        double scorea = 0;
        double scoreb = 0;
        Scanner input = new Scanner(System.in);

        for (int i=0;i<2;i++) {
          int c=i;
          System.out.println("Welcome to the maths quiz game please input player name "+c++);// tells the player to input his/her name
          player[i] = input.next();

        }
        System.out.println("-------------------------------------");//shows a list of players which has been outputted
        System.out.println("\t\tPLAYER LIST ");
        System.out.println(player[0]+"\t"+"\t"+"score:"+"\t"+scorea);
        System.out.println(player[1]+"\t"+"\t"+"score:"+"\t"+scoreb);
        System.out.println("-------------------------------------");



        double answer = 0;
        double numA, numB;
        char operator;
        char operator2;
        boolean quit = false;
        double sum1;
        double sum2;
        numA = 0;
        numB = 0;
        operator = Character.MIN_VALUE;

        while (quit == false) {
          System.out.println("to exit the game you need to type quit  ");//ask the user if he wants to quit if yes he needs to type "quit" and hit enter
          System.out.println("");
          System.out.println("Please enter your equation please "+player[1]+"press enter for each number and mathematical symbol"); // tells tuser 1 to put a math number or symbol
          System.out.println("");
          String s=input.next();// this to allow the user to input a word "quit" to activte the quit fuction
          if(s.equals("quit")){//if the user quits thiss will be outputted saying thier name and score next to them
            System.out.println("Thanks for playing");
            System.out.println("-------------------------------------");
            System.out.println("\t\tPLAYER LIST ");
            System.out.println(player[0]+"\t"+"\t"+"score:"+"\t"+scorea);
            System.out.println(player[1]+"\t"+"\t"+"score:"+"\t"+scoreb);
            System.out.println("-------------------------------------");
            if (scorea > scoreb){//it checks to see who has the higher score if 
              System.out.println(player[0]+ "\tWins!");
              quit = true; break;
            }
            else if (scorea < scoreb) {
              System.out.print(player[1]+ "\tWins!");
              quit = true; break;
            }
            else if (scorea == scoreb)
              System.out.print("Is a tie!");
            quit = true; break;
          }

          numA = Double.parseDouble(s);
          operator = input.next().charAt(0);
          numB = input.nextDouble();
        do {
          numA = Double.parseDouble(a); // here you had Double.parseDouble(s) which is wrong
          operator2 = input.next().charAt(0);
          numB = input.nextDouble();
         switch (operator) {
            case '+':
            answer = numA + numB;
            break;
            case '-':
            answer = numA - numB;
            break;
            case '*':
            answer = numA * numB;
            break;
            case '/':
            answer = numA / numB;
            break;
            case '^':
            answer = (int) Math.pow(numA, numB);
            break;
            default:
            System.out.println("Invlaid character or symbol please input again ");

            break;
        }
        } while 
          System.out.println("");
          System.out.println(player[0]+"\t solve the equation");
          sum2 = input.nextDouble();
          if (sum2 == answer){
            scoreb = scoreb + 10.00;
            System.out.println("correct you got 10 points to your score");
            System.out.println("");
          } else{ System.out.println("incorrcet you got no points the corrcet answer was:"+"" + answer);
          }

          numA = 0;
          numB = 0;
          operator = Character.MIN_VALUE;
          answer = Double.NaN;
          System.out.println("");
          System.out.println("to exit the game you need to type quit  ");
          System.out.println("");
          System.out.println("Please enter your equation please "+player[0]+"press enter for each number and mathematical symbol");
          String a=input.next();
          if(a.equals("quit")){
            System.out.println("Thanks for playing");
            System.out.println("-------------------------------------");
            System.out.println("\t\tPLAYER LIST ");
            System.out.println(player[0]+"\t"+"\t"+"score:"+"\t"+scorea);
            System.out.println(player[1]+"\t"+"\t"+"score:"+"\t"+scoreb);
            System.out.println("-------------------------------------");
            if (scorea > scoreb){
              System.out.println(player[0]+ "\tWins!");
              quit = true; break;
            }
            else if (scorea < scoreb) {
              System.out.print(player[1]+ "\tWins!");
              quit = true; break;
            }
            else
              System.out.print("Is a tie!");
            quit = true; break;
          }
          do {
          numA = Double.parseDouble(a); // here you had Double.parseDouble(s) which is wrong
          operator2 = input.next().charAt(0);
          numB = input.nextDouble();
         switch (operator) {
            case '+':
            answer = numA + numB;
            break;
            case '-':
            answer = numA - numB;
            break;
            case '*':
            answer = numA * numB;
            break;
            case '/':
            answer = numA / numB;
            break;
            case '^':
            answer = (int) Math.pow(numA, numB);
            break;
            default:
            System.out.println("Invlaid character or symbol please input again ");

            break;
        }
        } while 

          System.out.println("");
          System.out.println(player[1]+"\t solve the equation");
          System.out.println("");
          sum1 = input.nextDouble();
          if (sum1 == answer){
            scorea = scorea + 10.00;
            System.out.println("correct you got 10 points to your score");
            System.out.println("");
          } else{ System.out.println("incorrcet you got no points the corrcet answer was:"+"" + answer);
            System.out.println("");
          }
          numA =  0;
          numB =  0;
          operator = operator = Character.MIN_VALUE;
          answer = Double.NaN;
        }
    }
    }

我如何添加一个会产生某种循环的代码,但只有当在switch操作符选择时才选择类似+ _ * / ^然后它继续我下面的其他代码串,如果它在案例列表中选择none,它循环返回告诉用户输入数学等式

  do {
      numA = Double.parseDouble(a); // here you had Double.parseDouble(s) which is wrong
      operator2 = input.next().charAt(0);
      numB = input.nextDouble();
     switch (operator) {
        case '+':
        answer = numA + numB;
        break;
        case '-':
        answer = numA - numB;
        break;
        case '*':
        answer = numA * numB;
        break;
        case '/':
        answer = numA / numB;
        break;
        case '^':
        answer = (int) Math.pow(numA, numB);
        break;
        default:
        System.out.println("Invlaid character or symbol please input again ");

        break;
    }
    } while 

0 个答案:

没有答案