我的程序在while循环后不会执行任何操作

时间:2012-07-05 12:51:42

标签: java

我写了这个“计算器”,我希望它在计算后输出结果,但不会出于某种原因!!
示例输入
2
1 + 1 * 2 =

输出 4

提前致谢

import java.util.Scanner;


public class Main {

/**
 * @param args
 */
public static void main(String[] args) {
    Scanner input=new Scanner(System.in);
    int n=input.nextInt();
    for (int i=0;i<n;i++){
        int result=input.nextInt();
        String operand=input.next();
        int flag=0;
        while (flag==0){
            int temp=input.nextInt();
            if (operand.equals("*"))
                result*=temp;
            else if (operand.equals("/"))
                result/=temp;
            else if (operand.equals("+"))
                result+=temp;
            else if (operand.equals("-"))
                result-=temp;
            else if (operand.equals("=")){
                flag=1;
                System.out.println(result);
            }
            operand=input.next();
        }
    }

}

  }

public class Main 
{
    public static void main(String[] args) 
    {
         Scanner input=new Scanner(System.in);
         int n=input.nextInt();
         for (int i=0;i<n;i++)
         {
             int result=input.nextInt();
             String operand=input.next();
             int flag=0;
             while (flag==0)
             {
                 int temp=input.nextInt();
                 if (operand.equals("*"))
                     result*=temp;
                 else if (operand.equals("/"))
                     result/=temp;
                 else if (operand.equals("+"))
                     result+=temp;
                 else if (operand.equals("-"))
                     result-=temp;
                 else if (operand.equals("="))
                 {
                     flag=1;
                     System.out.println(result);
                 }
            operand=input.next();
            }
        }
    }
}

3 个答案:

答案 0 :(得分:0)

进入while循环后,您永远不会输入任何内容。跑一个

operand=input.next(); 

再次在你的while循环中的右括号之前。

否则您的1 + 1 * 2 =将读为1 + 1 + 2(从不更新您的运营商)

while (flag==0)
        {
        int temp=input.nextInt();
        if (operand.equals("*"))
            result*=temp;
        else if (operand.equals("/"))
            result/=temp;
        else if (operand.equals("+"))
            result+=temp;
        else if (operand.equals("-"))
            result-=temp;
        else if (operand.equals("="))
             {
             flag=1;
             System.out.println(result);
             }
        operand=input.next();// needs to be inside closing bracket

public class Main 
{
    public static void main(String[] args) 
    {
         Scanner input=new Scanner(System.in);
         int n=input.nextInt();
         for (int i=0;i<n;i++)
         {
             int result=input.nextInt();
             String operand=input.next();
             int flag=0;
             while (flag==0)
             {
                 int temp=input.nextInt();
                 if (operand.equals("*"))
                     result*=temp;
                 else if (operand.equals("/"))
                     result/=temp;
                 else if (operand.equals("+"))
                     result+=temp;
                 else if (operand.equals("-"))
                     result-=temp;
                 else if (operand.equals("="))
                 {
                     flag=1;
                     System.out.println(result);
                 }
            operand=input.next();
            }
        }
    }
}

答案 1 :(得分:0)

控件正在等待以下行的输入

    while (flag==0){
        int temp=input.nextInt(); <- its waiting for input after you enter =

答案 2 :(得分:0)

这是一个非常简单的测试示例:1 + 1 =

第一次迭代:

 int result=input.nextInt();  //===> (1) + 1 =
 String operand=input.next();  //===> 1 (+) 1 =
 int temp=input.nextInt(); //===> 1 + (1) =
 result+=temp;  //===> result == 2
 operand=input.next(); //===> 1 + 1 (=)

第二次迭代:

 int temp=input.nextInt(); //===> ooops nothing to read : wait for a user input!

以下是适用的算法版本:

import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner input=new Scanner(System.in);
    int n=input.nextInt();
    for (int i=0;i<n;i++){
      int result=input.nextInt();
      String operand=input.next();
      while (true){
        if (operand.equals("=")) {
          System.out.println(result);
          break;
        } else {
          int temp=input.nextInt();
          if (operand.equals("*"))
            result*=temp;
          else if (operand.equals("/"))
            result/=temp;
          else if (operand.equals("+"))
            result+=temp;
          else if (operand.equals("-"))
            result-=temp;
          operand=input.next();
        }
      }
    }

}

}