为什么会出现这些错误?

时间:2015-12-06 03:52:37

标签: java

我正在尝试制作一个分数计算器,当你输入quit时退出,无论外壳是什么。然而,这两个错误已经出现。有什么建议??非常感谢。

FractionCalculator.java:70: error: '(' expected
         else if kb.next().equalsIgnoreCase("quit"){
                ^
FractionCalculator.java:70: error: ')' expected
         else if kb.next().equalsIgnoreCase("quit"){
                                                   ^
FractionCalculator.java:70: error: 'else' without 'if'
         else if kb.next().equalsIgnoreCase("quit"){
         ^

import java.util.*;

public class FractionCalculator {

     // useDelimiter or split method in string class


   public static void main(String[] args) {
     Greeting();
     produceAnswer();                              
  }

  static String value1Str = "";
  static String value2Str = "";
  static char operator = ' ';

   public static void Greeting() {
      Scanner kb = new Scanner (System.in);      
      String userName;
      System.out.print("Hello Person, What is your first name: ");
      userName = kb.next();
      System.out.println("Hi " +userName +", welcome to the great mystical fraction calculator.");   
   }   

   public static void produceAnswer() {
      Scanner kb = new Scanner (System.in);
      System.out.println("What is your input or type quit to leave?");
      String input = kb.nextLine();     
      boolean value1Done = false;
      boolean operatorDone = false;
      boolean value2Done = false;
      boolean correctFormat = false;      

      for (int i = 0; i < input.length(); i++) {   
         System.out.println("The input of given string is: " +input.charAt(i));         

         if (input.charAt(i) != ' ' && value1Done == false) {
            value1Str += input.charAt(i);
     }
         else {
           value1Done = true;
     }  

         if (input.charAt(i) == '+' || input.charAt(i) == '-' || input.charAt(i) == '*' || input.charAt(i) == '/' && operatorDone == false && value1Done == true) {
           operator = input.charAt(i);
           operatorDone = true;
           i=i+1;
     } 

         if (input.charAt(i) != ' ' && value1Done == true && value2Done == false) {
            value2Str  += input.charAt(i);
     }      
        else {
            value2Done = false;
     }                
              else if kb.next().equalsIgnoreCase("quit"){
                System.out.println("Why are you leaving? Comeback soon bby");
               break;
               // quit = true;
               //correctFormat = true;
      }                    
      }  

      System.out.println("value1Str  is: " +value1Str); 
      System.out.println("Operator is: " +operator);    
      System.out.println("Value2Str is: " +value2Str);
   }
}

2 个答案:

答案 0 :(得分:1)

使用if条件时出错。 正确使用if,if else条件更新代码。

import java.util.*;

public class FractionCalculator {

    // useDelimiter or split method in string class

    public static void main(String[] args) {
        Greeting();
        produceAnswer();

    }

    static String value1Str = "";
    static String value2Str = "";
    static char operator = ' ';

    public static void Greeting() {
        Scanner kb = new Scanner(System.in);

        String userName;
        System.out.print("Hello Person, What is your first name: ");
        userName = kb.next();
        System.out.println("Hi " + userName
                + ", welcome to the great mystical fraction calculator.");

    }

    public static void produceAnswer() {
        Scanner kb = new Scanner(System.in);
        System.out.println("What is your input or type quit to leave?");
        String input = kb.nextLine();
        boolean value1Done = false;
        boolean operatorDone = false;
        boolean value2Done = false;
        boolean correctFormat = false;

        for (int i = 0; i < input.length(); i++) {
            System.out.println("The input of given string is: "
                    + input.charAt(i));

            if (input.charAt(i) != ' ' && value1Done == false) {
                value1Str += input.charAt(i);
            } else {
                value1Done = true;
            }

            if (input.charAt(i) == '+' || input.charAt(i) == '-'
                    || input.charAt(i) == '*' || input.charAt(i) == '/'
                    && operatorDone == false && value1Done == true) {
                operator = input.charAt(i);
                operatorDone = true;
                i = i + 1;
            }

            if (input.charAt(i) != ' ' && value1Done == true
                    && value2Done == false) {
                value2Str += input.charAt(i);
            } else if (kb.next().equalsIgnoreCase("quit")) {
                System.out.println("Why are you leaving? Comeback soon bby");
                break;
                // quit = true;
                // correctFormat = true;
            } else {
                value2Done = false;
            }

        }

        System.out.println("value1Str  is: " + value1Str);
        System.out.println("Operator is: " + operator);
        System.out.println("Value2Str is: " + value2Str);

    }
}

检查如何在此链接中使用if-then和if-then-else语句:

  

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html

答案 1 :(得分:0)

您的语句格式错误if

 if (...) { ....
 }
 else {
        value2Done = false;
 }
 else // ERROR

if语句不能有两个else条款......您在此处。

实际上,第三条错误消息是明确地