Java计算器无法运行

时间:2014-07-05 12:41:58

标签: java calculator

我是编程新手。 我甚至使用帮助制作了这段代码。 我想知道它为什么没有成功执行.. 当我尝试执行它时,我必须输入两次数字才能使它运行。 是或否退出也不起作用!

import java.util.*;
public class Cal{

public static void main (String[] args)
{


    String i;
    char   in;
    int    sol;
    int    x;
    int    y;


    System.out.println("Hello! \nWelcome to The Calculator");
    System.out.println("Use the following Commands to use The Calulator, \nType the following letters: \na for Addition \ns for Subtraction \nm for for Multipication \nd for Division" );

    Scanner keyIn = new Scanner(System.in);

do
{


    System.out.println("Now enter the letter for you operation: ");
    keyIn.nextLine();

    i = keyIn.nextLine();



    in = i.charAt(0);

    if (in == 'a')
    {


    System.out.println("Type in the first number ");
    keyIn.nextInt();
    x = keyIn.nextInt();

    System.out.println("Type in the second number ");
    keyIn.nextInt();
    y = keyIn.nextInt();
    sol = x + y;

    System.out.println("You chose + operation. \nAnswer = " + sol);
    }

    if (in == 's')
    {


    System.out.println("Type in your first number");
    keyIn.nextInt();
    x = keyIn.nextInt();

    System.out.println("Type in the second number");
    keyIn.nextInt();
    y = keyIn.nextInt();
    sol = x - y;

    System.out.println("You chose + operation. \nAnswer = " + sol);
    }


    if (in == 'm')
    {


    System.out.println("Type in your first number");
    keyIn.nextInt();
    x = keyIn.nextInt();

    System.out.println("Type in the second number");
    keyIn.nextInt();
    y = keyIn.nextInt();
    sol = x * y;

    System.out.println("You chose + operation. \nAnswer = " + sol);
    }


    if (in == 's')
    {


    System.out.println("Type in your first number");
    keyIn.nextInt();
    x = keyIn.nextInt();

    System.out.println("Type in the second number");
    keyIn.nextInt();
    y = keyIn.nextInt();
    sol = x / y;

    System.out.println("You chose + operation. \nAnswer = " + sol);
    }

    System.out.println("Do you want to do another calculation? No:n Yes:y");
    keyIn.nextLine();

}

while(keyIn.equals("y"));
if (keyIn.equals("n"));

    System.exit(0);
    }
}                 

4 个答案:

答案 0 :(得分:1)

你一直在做:

`keyIn.nextLine();

i = keyIn.nextLine();`

这就是你必须输入两个输入的原因。第一个输入被浪费,然后第二个输入存储输入。

删除第一个keyIn.nextLine();然后离开:

' i = keyIn.nextLine();`

你在哪里使用它。

因为它存储了单位,让你的程序对它进行评估。

答案 1 :(得分:0)

在您的代码中

System.out.println("Type in your first number");
    keyIn.nextInt();
    x = keyIn.nextInt();

你两次调用keyIn.nextInt()。由于您不会将第一个结果存储在变量中,因此它会丢失。

您可以使用调试器轻松找到它。

答案 2 :(得分:0)

使用此功能,并查看评论:

import java.util.*;
public class Cal

{

public static void main (String[] args)
{


    String i;
    char   in;
    int    sol;
    int    x;
    int    y;



    System.out.println("Hello! \nWelcome to The Calculator");
    System.out.println("Use the following Commands to use The Calulator, \nType the following letters: \na for Addition \ns for Subtraction \nm for for Multipication \nd for Division" );

    Scanner keyIn = new Scanner(System.in);

do
{


    System.out.println("Now enter the letter for you operation: ");
    //keyIn.nextLine();

    i = keyIn.nextLine(); // key input saved in variable!



    in = i.charAt(0);

    if (in == 'a')
    {


    System.out.println("Type in the first number ");
    //keyIn.nextInt(); // key input would be lost here..
    x = keyIn.nextInt();  // key input saved in variable

    System.out.println("Type in the second number ");
    //keyIn.nextInt();
    y = keyIn.nextInt();
    sol = x + y;

    System.out.println("You chose + operation. \nAnswer = " + sol);
    }

    if (in == 's')
    {


    System.out.println("Type in your first number");
    //keyIn.nextInt();
    x = keyIn.nextInt();

    System.out.println("Type in the second number");
    //keyIn.nextInt();
    y = keyIn.nextInt();
    sol = x - y;

    System.out.println("You chose + operation. \nAnswer = " + sol);
    }


    if (in == 'm')
    {


    System.out.println("Type in your first number");
    //keyIn.nextInt();
    x = keyIn.nextInt();

    System.out.println("Type in the second number");
    //keyIn.nextInt();
    y = keyIn.nextInt();
    sol = x * y;

    System.out.println("You chose + operation. \nAnswer = " + sol);
    }


    if (in == 's')
    {


    System.out.println("Type in your first number");
    //keyIn.nextInt();
    x = keyIn.nextInt();

    System.out.println("Type in the second number");
    //keyIn.nextInt();
    y = keyIn.nextInt();
    sol = x / y;

    System.out.println("You chose + operation. \nAnswer = " + sol);
    }



    System.out.println("Do you want to do another calculation? No:n Yes:y");
    keyIn.nextLine();



}

while(keyIn.equals("y"));
if (keyIn.equals("n"));

    System.exit(0);

答案 3 :(得分:0)

我修改了你的代码。请尝试以下方法:

    public static void main (String[] args)
{


    String i;
    char   in;
    int    sol;
    int    x;
    int    y;



    System.out.println("Hello! \nWelcome to The Calculator");
    System.out.println("Use the following Commands to use The Calulator, \nType the following letters: \na for Addition \ns for Subtraction \nm for for Multipication \nd for Division" );

    Scanner keyIn = new Scanner(System.in);

do
{


    System.out.println("Now enter the letter for you operation: ");
    keyIn.nextLine();

    i = keyIn.nextLine();



    in = i.charAt(0);

    if (in == 'a')
    {


    System.out.println("Type in the first number ");
    keyIn.nextInt();
    x = keyIn.nextInt();

    System.out.println("Type in the second number ");
    keyIn.nextInt();
    y = keyIn.nextInt();
    sol = x + y;

    System.out.println("You chose + operation. \nAnswer = " + sol);
    }

    if (in == 's')
    {


    System.out.println("Type in your first number");
    keyIn.nextInt();
    x = keyIn.nextInt();

    System.out.println("Type in the second number");
    keyIn.nextInt();
    y = keyIn.nextInt();
    sol = x - y;

    System.out.println("You chose + operation. \nAnswer = " + sol);
    }


    if (in == 'm')
    {


    System.out.println("Type in your first number");
    keyIn.nextInt();
    x = keyIn.nextInt();

    System.out.println("Type in the second number");
    keyIn.nextInt();
    y = keyIn.nextInt();
    sol = x * y;

    System.out.println("You chose + operation. \nAnswer = " + sol);
    }


    if (in == 's')
    {


    System.out.println("Type in your first number");
    keyIn.nextInt();
    x = keyIn.nextInt();

    System.out.println("Type in the second number");
    keyIn.nextInt();
    y = keyIn.nextInt();
    sol = x / y;

    System.out.println("You chose + operation. \nAnswer = " + sol);
    }



    System.out.println("Do you want to do another calculation? No:n Yes:y");
    keyIn.nextLine();
}while(keyIn.equals("y"));
}