简体   繁体   English

程序编译但无法运行

[英]Program compiles but does not run

This is an assignment i have to complete. 这是我必须完成的任务。

Can someone lead me in the right direction? 有人可以指引我正确的方向吗?

The program compiles but wont run correctly. 该程序可以编译,但无法正确运行。

The error is InputMissmatch exception. 错误是InputMissmatch异常。

The error you are getting means that you are trying to use some kind of data as another one, in your case, you are probably trying to use a String as a float. 您得到的错误意味着您正在尝试将某种数据用作另一种数据,就您而言,您可能正在尝试将String用作浮点数。

When using any of the next methods in the Scanner class you should first be sure that there's an appropiate input from the user. 在Scanner类中使用下一个方法时,首先应确保用户输入了适当的信息。

In order to do so, you need to use the has methods. 为此,您需要使用has方法。

Your problem is that you are not checking wether the input is a correct float or not before using your Scanner.nextFloat(); 您的问题是,在使用Scanner.nextFloat();之前,您是否不检查输入是否为正确的float。

You should do something like this: 您应该执行以下操作:

if (hope.hasNextFloat())
{
    // Code to execute when you have a proper float, 
    // which you can retrieve with hope.nextFloat()
}
else
{ 
    // Code to execute when the user input is not a float
    // Here you should treat it properly, maybe asking for new input
}

That should be enough to point you in the right direction. 这足以将您指向正确的方向。

Also, check the Scanner api documentation for further details. 另外,请查看Scanner api文档以获取更多详细信息。

EDIT 编辑

Also, you are asking the user to input characters (or strings): "A", "B", etc..., but you are trying to compare them with a float. 另外,您还要求用户输入字符(或字符串):“ A”,“ B”等...,但是您试图将它们与浮点数进行比较。 That's wrong, you should compare them with a string or character, like this: 错了,您应该将它们与字符串或字符进行比较,如下所示:

if (hope.hasNextString())
{
   if (hope.nextString().equals("A"))
   {
       // Code for option "A"
   }
   else if (hope.nextString().equals("B"))
   {
       // Code for option "B"
   }
   else ...
}

You could use a switch there, but it seems that you are not yet very fammiliar with java, so I'll leave it for another time. 您可以在此处使用开关,但是您似乎对Java不太熟悉,因此我将其再留一遍。

Your problem is that you are entering a letter into a float field. 您的问题是您要在浮点型字段中输入字母。 In your program you're asking the user to enter a float: 在您的程序中,您要求用户输入浮点数:

A = hope.nextFloat();

But if you enter the letter "A", you're going to get an exception because "A" is not a float, it's a string. 但是,如果输入字母“ A”,则将得到一个例外,因为“ A”不是浮点数,而是一个字符串。

A simpler way to solve your problem is instead of having all the choices fields, you just read the input the user enters from the scanner like: 解决问题的一种更简单的方法是,您不必阅读所有选择字段,而只需阅读用户从扫描仪输入的输入,例如:

String choice = hope.next();

Next in the if statement, you check if the value from the string choice is equal to a specific letter, for example 接下来,在if语句中,检查字符串选择中的值是否等于特定的字母,例如

if (choice.equals("A")) {
        number4 = (number1 + number2 + number3);
        System.out.printf("Your results are:" + (number4));
    } 

And you can do the same thing for the other choices you have. 您可以为其他选择做同样的事情。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM