简体   繁体   English

我的Scanner Java代码有什么问题?

[英]What's wrong with my Scanner java code?

import java.util.Scanner;  

public class Exercise5 {  

 public static void main(String[] args) {  
  Scanner in = new Scanner(System.in);  

  System.out.print("Input first number: ");  
  int num1 = in.nextInt(5);  

  System.out.print("Input second number: ");  
  int num2 = in.nextInt(25);  

  System.out.println(num1 + " x " + num2 + " = " + num1 * num2);  
 }  

}  

**Here is my java code, the expect output should be like this: **这是我的Java代码,预期输出应如下所示:

Input first number: 25 输入第一个数字:25
Input second number: 5 输入第二个数字:5
25 x 5 = 125 25 x 5 = 125

After I plug in my code and run it, the output is too different with the answer 在插入代码并运行它之后,输出与答案太不同了

Here is the output: 这是输出:

Input first number: Exception in thread "main"   java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at Exercise5.main(Exercise5.java:27)

How can I fix my code? 如何修复我的代码?

"5" in in.nextInt(5); in.nextInt(5)中的“ 5”; means radix. 表示基数。 Do you need it? 你需要它吗? Does the program work if you use nextInt() method with no parameters? 如果您使用不带参数的nextInt()方法,程序是否可以正常工作?

Also, for some reason both of versions work for me with no error: 另外,由于某些原因,两个版本都可以正常工作:

// output of nextInt(radix) version
java Excercise5
Input first number: 32
Input second number: 23
17 x 53 = 901

// output of nextInt() version
java Excercise5
Input first number: 5
Input second number: 25
5 x 25 = 125

There are 2 nextInt() methods: 有2个nextInt()方法:

nextInt() alone allows you to read an int in base 10 number 仅使用nextInt()即可读取以10为底的整数

nextInt(radix) reads the next number in base N where N is 5, or 25 in your case nextInt(radix)读取基准的下一个号码N其中N为5,或25,你的情况

So, call the first one and you're done 所以,打电话给第一个就可以了

You shouldn't put 5 and 25 inside brackets of in.nextInt, you will write value inside console line and scanner will read it. 您不应将5和25放在in.nextInt的括号内,而是将值写入控制台行,而扫描仪将读取它。
So instead 所以与其

 int num1 = in.nextInt(5); 

it should look like 它应该看起来像

 int num1 = in.nextInt(); 

No need to specify the radix to nextInt. 无需将基数指定为nextInt。

Just use the no args version of nextInt. 只需使用nextInt的无参数版本。

Replace 更换

int num1 = in.nextInt(5);

with

int num1 = in.nextInt();

And

int num2 = in.nextInt(25);

with

int num2 = in.nextInt();

FIXED CODE 固定码

import java.util.Scanner;  

public class Exercise5 {  

public static void main(String[] args) {  
Scanner in = new Scanner(System.in);  

 System.out.print("Input first number: ");  
 int num1 = in.nextInt();  

 System.out.print("Input second number: ");  
 int num2 = in.nextInt();  

 total = num1*num2

System.out.println(num1 + " x " + num2 + " = " + total);  
}  

}  

Your nextInt() will catch the user input so there is no need to put in values. 您的nextInt()将捕获用户输入,因此无需放入值。 You will manually put them in when prompted on the output window in your IDE. 在IDE的输出窗口上出现提示时,您将手动将它们放入。

I would also put the num1 and num2 into separate variables (Ex. int total = num1*num2) then print out the total in the println :) 我还将num1和num2放入单独的变量中(例如,int total = num1 * num2),然后在println中打印出总数:)

EDIT: Didn't even notice you were supplying an argument to nextInt(). 编辑:甚至没有注意到您正在为nextInt()提供一个参数。 As the other mentioned, this is not what you want to do. 正如另一个提到的那样,这不是您想要做的。

Also, the new line character is still in the buffer after you call nextInt() . 另外,在调用nextInt()之后,换行符仍在缓冲区中。 You'll need to fix this as well before your program will operate as you intend. 您还需要解决此问题,然后程序才能按预期运行。

Understanding Scanner's nextLine(), next(), and nextInt() methods 了解扫描仪的nextLine(),next()和nextInt()方法

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

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