2015-12-07 3 views
-2

Сложность хранения следующей статистики для печати при завершении программы. Я был в состоянии сохранить общее число действительных и недействительных выражений, однако у меня возникают трудности со следующим:Сохранение статистики в Java Calculator

  • Наибольшее общее значение результата.

  • Наименьшее общее значение результата.

  • Совокупность всех значений результата, то есть всех результатов, сгруппированных вместе.

  • Среднее значение результата

Любое руководство для любого из указанных выше была бы оценена.

Scanner input = new Scanner(System.in); // Creating new scanner to take user choice input 
    int validExpressions = 0; 
    int invalidExpressions = 0; 
    int lowestval = 0; 
    int highestval; 
    while (true) { 

     System.out.println("Enter K to enter a postfix expression or F to open a text file:"); // requesting user input 

     String choice = input.nextLine(); // Taking user input from keyboard, either K for manual entry or F for file input 

     float answer; // Variable used to store answers. 


     if ("F".equals(choice)) { 
      System.out.println("Please enter the name of a .txt file:"); // user prompted to enter the name of the text file 

      String file = input.nextLine(); // File name will be stored in a String (file) to be used later 
      File txtfile = new File(file); 
      Scanner reader = new Scanner(txtfile); // Creating a new scanner which reads the text file 

      while (reader.hasNextLine()) { // The new scanner (reader) reads each line of the text file 

       String expression = reader.nextLine(); // Each line is stored in a String called Expression 
       String[] parts = expression.split(" "); // The string is split into three parts; two numbers and an operator 

       String part1 = parts[0]; 
       String part2 = parts[1]; 
       String part3 = parts[2]; 

       float number1 = Float.parseFloat(part1); // Parts one and two are both converted from a String to a Float 
       float number2 = Float.parseFloat(part2); 

       // Program processes the operator and applies it to Float number1 and Float number2 before outputting the 
       // expression and the correct answer 
       if ("+".equals(part3)) { 
        answer = number1 + number2; 
        System.out.println(part1 + " " + "+" + " " + part2 + " " + "=" + " " + answer); 
        validExpressions++; 
       } else if ("-".equals(part3)) { 
        answer = number1 - number2; 
        System.out.println(part1 + " " + "-" + " " + part2 + " " + "=" + " " + answer); 
        validExpressions++; 
       } else if ("*".equals(part3)) { 
        answer = number1 * number2; 
        System.out.println(part1 + " " + "*" + " " + part2 + " " + "=" + " " + answer); 
        validExpressions++; 
       } else if ("/".equals(part3)) { 
        answer = number1/number2; 
        System.out.println(part1 + " " + "/" + " " + part2 + " " + "=" + " " + answer); 
        validExpressions++; 
       } 

      } 

     } else if ("K".equals(choice)) // If the user enters K input will be taken from the keyboard 

      System.out.println("Please enter a post-fix expression:"); // User is prompted to enter a post fix expression 

     Scanner expression = new Scanner(System.in); // Creating a new Scanner called Expression to read user input 
     String exp = expression.nextLine(); // The user's input is stored in a string 
     String[] elements = exp.split(" "); // Expression is split into three elements, two numbers and an operator 

     String element1 = elements[0]; 
     String element2 = elements[1]; 
     String element3 = elements[2]; 

     float num1 = Float.parseFloat(element1); // Element1 and element2 are converted to Floats and named num1 and num2 
     float num2 = Float.parseFloat(element2); 


     // Program processes the operator and applies it to Float num1 and Float num2 before outputting the 
     // expression and the correct answer 
     if ("+".equals(element3)) { 
      answer = num1 + num2; 
      System.out.println(element1 + " " + "+" + " " + element2 + " " + "=" + " " + answer); 
      validExpressions++; 
     } else if ("-".equals(element3)) { 
      answer = num1 - num2; 
      System.out.println(element1 + " " + "-" + " " + element2 + " " + "=" + " " + answer); 
      validExpressions++; 
     } else if ("*".equals(element3)) { 
      answer = num1 * num2; 
      System.out.println(element1 + " " + "*" + " " + element2 + " " + "=" + " " + answer); 
      validExpressions++; 
     } else if ("/".equals(element3)) { 
      answer = num1/num2; 
      System.out.println(element1 + " " + "/" + " " + element2 + " " + "=" + " " + answer); 
      validExpressions++; 

      if (choice.isEmpty()) { // If the user does not enter K or F the program will exit 
       System.out.println("Evaluations Complete"); 
       System.out.println("-------------------------"); 
       System.out.println("Valid expressions: " + validExpressions); 
       System.out.println("Invalid expressions: " + invalidExpressions); 
       System.exit(0); 
      } 
     } 
     }       
    } 
} 
+1

И каков ваш вопрос? – zeeMonkeez

+0

Извинения. Как бы я это сделал, чтобы выразить его в самой простой форме. –

+2

Добро пожаловать в StackOverflow. Пожалуйста, отредактируйте (http://stackoverflow.com/posts/34135803/edit) свое сообщение, чтобы включить свой вопрос. Также, пожалуйста, обратите внимание на то, что у вас есть проблемы (мы не хотим просеивать сотни строк кода). См. [Как спросить] (http://stackoverflow.com/help/how-to-ask) и [минимальный пример] (http://stackoverflow.com/help/how-to-ask). – zeeMonkeez

ответ

0

я изменил что-то неправильно на логику и синтаксис, но я не debuged его, вы можете попробовать.

Scanner input = new Scanner(System.in); // Creating new scanner to take user choice input 
int validExpressions = 0; 
int invalidExpressions = 0; 
float lowestval = 0; 
float highestval =0 ; 
float total=0; 
while (true) { 

    System.out.println("Enter K to enter a postfix expression or F to open a text file:"); // requesting user input 

    String choice = input.nextLine(); // Taking user input from keyboard, either K for manual entry or F for file input 

    float answer; // Variable used to store answers. 


    if ("F".equals(choice)) { 
     System.out.println("Please enter the name of a .txt file:"); // user prompted to enter the name of the text file 

     String file = input.nextLine(); // File name will be stored in a String (file) to be used later 
     File txtfile = new File(file); 
     Scanner reader = new Scanner(txtfile); // Creating a new scanner which reads the text file 

     while (reader.hasNextLine()) { // The new scanner (reader) reads each line of the text file 

      String expression = reader.nextLine(); // Each line is stored in a String called Expression 
      String[] parts = expression.split(" "); // The string is split into three parts; two numbers and an operator 

      String part1 = parts[0]; 
      String part2 = parts[1]; 
      String part3 = parts[2]; 

      float number1 = Float.parseFloat(part1); // Parts one and two are both converted from a String to a Float 
      float number2 = Float.parseFloat(part2); 

      // Program processes the operator and applies it to Float number1 and Float number2 before outputting the 
      // expression and the correct answer 
      if ("+".equals(part3)) { 
       answer = number1 + number2; 
       System.out.println(part1 + " " + "+" + " " + part2 + " " + "=" + " " + answer); 
       validExpressions++; 
      } else if ("-".equals(part3)) { 
       answer = number1 - number2; 
       System.out.println(part1 + " " + "-" + " " + part2 + " " + "=" + " " + answer); 
       validExpressions++; 
      } else if ("*".equals(part3)) { 
       answer = number1 * number2; 
       System.out.println(part1 + " " + "*" + " " + part2 + " " + "=" + " " + answer); 
       validExpressions++; 
      } else if ("/".equals(part3)) { 
       answer = number1/number2; 
       System.out.println(part1 + " " + "/" + " " + part2 + " " + "=" + " " + answer); 
       validExpressions++; 
      } else{ 
       invalidExcepressions++; 
       continue; 
      } 
      highestval=(highestval>=answer)?highestval:answer; 
      lowestval=(lowestval<=answer)?lowestval:answer; 
      total+=answer; 
     } 

    } else if ("K".equals(choice)){ // If the user enters K input will be taken from the keyboard 

     System.out.println("Please enter a post-fix expression:"); // User is prompted to enter a post fix expression 

    Scanner expression = new Scanner(System.in); // Creating a new Scanner called Expression to read user input 
    String exp = expression.nextLine(); // The user's input is stored in a string 
    String[] elements = exp.split(" "); // Expression is split into three elements, two numbers and an operator 

    String element1 = elements[0]; 
    String element2 = elements[1]; 
    String element3 = elements[2]; 

    float num1 = Float.parseFloat(element1); // Element1 and element2 are converted to Floats and named num1 and num2 
    float num2 = Float.parseFloat(element2); 


    // Program processes the operator and applies it to Float num1 and Float num2 before outputting the 
    // expression and the correct answer 
    if ("+".equals(element3)) { 
     answer = num1 + num2; 
     System.out.println(element1 + " " + "+" + " " + element2 + " " + "=" + " " + answer); 
     validExpressions++;   
    } else if ("-".equals(element3)) { 
     answer = num1 - num2; 
     System.out.println(element1 + " " + "-" + " " + element2 + " " + "=" + " " + answer); 
     validExpressions++; 
    } else if ("*".equals(element3)) { 
     answer = num1 * num2; 
     System.out.println(element1 + " " + "*" + " " + element2 + " " + "=" + " " + answer); 
     validExpressions++; 
    } else if ("/".equals(element3)) { 
     answer = num1/num2; 
     System.out.println(element1 + " " + "/" + " " + element2 + " " + "=" + " " + answer); 
     validExpressions++; 
    }else{ 
     invalidExceotions++; 
     continue; 
    } 
    highestval=(highestval>=answer)?highestval:answer; 
    lowestval=(lowestval<=answer)?lowestval:answer; 
    total+=answer; 
}else if (choice.isEmpty()) { // If the user does not enter K or F the program will exit 
      System.out.println("Evaluations Complete"); 
      System.out.println("-------------------------"); 
      System.out.println("Valid expressions: " + validExpressions); 
      System.out.println("Invalid expressions: " + invalidExpressions); 
      System.out.println("Total :"+total); 
      System.out.println("Average :"+total/validExceptions); 
      System.exit(0); 
     } 

}

+0

Пожалуйста, не просто отправляйте код - объясните, что вы изменили. –

+0

Большое спасибо. Не могли бы вы вкратце объяснить, как это работает: highval = (highval> = answer)? Highval: answer; lowval = (lowval <= answer)? Lowerval: answer; всего + = ответ; –