简体   繁体   English

为什么我可以为我的操作输入任何东西?

[英]Why can i input anything for my operation?

So I'm trying to make a simple calculator in java but have no clue how to get an input for which operation. 所以我试图在java中创建一个简单的计算器,但不知道如何获取输入操作。 The program asks for two numbers, then asks for an operator. 该程序要求两个数字,然后要求操作员。 Everything works fine when i run it until i get to the operation type. 当我运行它直到我进入操作类型时,一切正常。 The compiler just skips right over it and doesn't even let me type anything in for it. 编译器只是跳过它,甚至不让我输入任何内容。 Any clues? 有线索吗?

Scanner keyboard = new Scanner(System.in);

double x;
double y;
double sum =0;
double minus =0;
double times =0;
double divide =0;
double nCr =0;
String Op;

System.out.print("X: ");
x = keyboard.nextDouble();
System.out.print("Y: ");
y = keyboard.nextDouble();
System.out.print("Op: ");
Op = keyboard.nextLine();

Try: 尝试:

System.out.print("X: ");
x = Double.parseDouble(keyboard.nextLine());
System.out.print("Y: ");
y = Double.parseDouble(keyboard.nextLine());
System.out.print("Op: ");
op = keyboard.nextLine();

This will parse the number you enter to Double and read the Carriage return. 这将解析您输入的数字为Double并读取回车符。

If you want error control (If the user inputs a string instead of a number): 如果您想要错误控制(如果用户输入字符串而不是数字):

Boolean check = true;

do
{
    try
    {
        //The code to read numbers
    }
    catch(InputMismatchException e)
    {
        System.err.println("Please enter a number!");
        check = false;
    }
}
while(check == false)

Instead of 代替

Op = keyboard.nextLine();

Try using 尝试使用

Op = keyboard.next();

Looks like the nextLine(); 看起来像nextLine(); method gives you a problem when invoked with methods like nextInt(), nextDouble(). 使用nextInt(),nextDouble()等方法调用时,方法会出现问题。

Observation: If the nextLine() is invoked alone, this doesn't happen. 观察:如果单独调用nextLine(),则不会发生这种情况。

Basically the nextDouble doesn't recognize the carriage return. 基本上nextDouble不识别回车。 Use the nextLine after each nextDouble . 在每个nextDouble之后使用nextLine

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我不能在我的模拟存储库中保存任何东西 - Why i can't save anything in my mocked repository 如何让我的输入验证工作,以便 -1 - 100 范围之外的任何内容都将显示错误消息? - How can I get my input validation to work so anything outside range -1 - 100 will display the error message? 如何让我的程序只输入特定的单词,如果输入的不是所说的单词,请重试输入 - How can I make my program only input specific words, if anything other than said word is entered, retry input 为什么当按下 Jbutton 并在 java 中执行其定义的 function 时,我无法在我的应用程序中执行任何操作? - why i can't do anything in my application when Jbutton is pressed and performing its defined function in java? 为什么我无法在Web服务中获得输入值? - Why I can't get the input value in my web service? 为什么我不能在这里打印我的声明或其他内容? - Why cant i print my statement or anything in here? 为什么我的代码不能同时运行两个面板? 当我运行它时它不会显示任何内容,但它会编译 - Why can't my code run two panels at same time? it doesnt show anything when I run it, but it compiles 我的JTable无法显示任何内容 - I can't get my JTable to show anything 为什么我不能只构建一个数组而不将其分配给任何东西? - Why can't I just build an array without assigning it to anything? 为什么我的方法没有返回任何东西? - Why are my methods not returning anything?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM