获取用户输入时出错(JAVA) [英] Error in getting user input(JAVA)

查看:73
本文介绍了获取用户输入时出错(JAVA)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚学会了在执行过程中使用Scanner类来获取用户输入。然而,在练习这个时,我面临着一个问题。

这是我的代码



  import  java.util.Scanner; 
public class a
{
public static void main()
{
扫描仪b = 扫描仪(System.in);
System.out.println( 输入整数);
int c = b.nextInt();
System.out.println( 输入字符串);
String d = b.nextLine();
System.out.println();
System.out.println( 您输入的整数是 + a);
System.out.println( 您输入的字符串是 + b);
}
}





我的尝试:



在运行程序时,它正在输入整数并相应地打印,但它不是要求String的输入并直接打印空白字符串。



但是,如果我要完全消除整数的输入并打印它,那么我的程序正确地输入字符串并打印。为什么不能两者一起完成?



有人可以帮忙吗?!

解决方案

试试添加b.nextLine();在int c = b.nextInt()之后;这样它就会读取整数后的新行字符。



  import  java.util.Scanner; 
public class MyClass {
public static void main( String args []){
扫描仪b = 扫描仪(System.in);
System.out.println( 输入整数);
int c = b.nextInt();
b.nextLine();
System.out.println( 输入字符串);
String d = b.nextLine();
System.out.println( 输入整数第2部分);
int e = b.nextInt();
b.nextLine();
System.out.println( 输入字符串第2部分);
String f = b.nextLine();
System.out.println();
System.out.println( 您输入的整数是 + c);
System.out.println( 您输入的字符串是 + d);
System.out.println( 您输入的整数是 + e);
System.out.println( 您输入的字符串是 + f);
}
}





Java请求带有多个单词的字符串输入 - Stack Overflow [ ^ ]


你使用了什么魔法?

  int  c = b.nextInt();  //  读取c中的整数 
...
System.out .println( 您输入的整数是 + a); // 并打印



< pre lang =java> String d = b.nextLine(); // 您阅读了d
...
系统中的下一行。 out.println( 您输入的字符串是 + b); // 并打印b



you肯定会学习调试器。



你的代码没有你想象的那样,你不明白为什么!



有一个几乎通用的解决方案:逐步在调试器上运行代码,检查变量。

调试器在这里显示你的代码正在做什么和你的任务是与它应该做的比较。

调试器中没有魔法,它不知道你应该做什么,它没有找到bug,它只是帮助你通过向您展示正在发生的事情。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。

调试器 - 维基百科,免费的百科全书 [ ^ ]

http://docs.oracle.com/javase/7/docs/technotes/tools/windows /jdb.html [ ^ ]

https: //www.jetbrains.com/idea/help/debugging-your-first-java-application.html [ ^ ]

调试器仅显示您的代码正在执行的操作,您的任务是与什么进行比较应该这样做。


Hi, I've just learned to use Scanner class to take user input during execution. However, on practicing this, I'm facing an issue.
Here is my code

import java.util.Scanner;
public class a
{
public static void main()
{
    Scanner b = new Scanner(System.in);
    System.out.println("Enter the integer");
    int c = b.nextInt();
    System.out.println("Enter the string");
    String d = b.nextLine();
    System.out.println();
    System.out.println(" The integer you entered is" + a);
    System.out.println(" The string you entered is "+b);
    }
}



What I have tried:

On running the program, it's taking input of integer and printing accordingly but it isn't asking for the input for String and printing a blank string directly.

However, if i were to completely eliminate the taking input from integer and printing it, then my program correctly takes input of String and prints. Why can't the both be done together?

Can someone please help?!

解决方案

Try add "b.nextLine();" right after int c = b.nextInt(); so that it will read the new line character after the integer.

import java.util.Scanner;
public class MyClass {
    public static void main(String args[]) {
       Scanner b = new Scanner(System.in);
        System.out.println("Enter the integer");
        int c = b.nextInt();
        b.nextLine();
        System.out.println("Enter the string");
        String d = b.nextLine();
        System.out.println("Enter the integer part 2");
        int e = b.nextInt();
        b.nextLine();
        System.out.println("Enter the string part 2");
        String f = b.nextLine();
        System.out.println();
        System.out.println(" The integer you entered is " + c);
        System.out.println(" The string you entered is "+d);
         System.out.println(" The integer you entered is " + e);
        System.out.println(" The string you entered is "+f);
    }
}



Java ask String input with multiple words - Stack Overflow[^]


What magic are you using ?

    int c = b.nextInt(); // you read the integer in c
...
    System.out.println(" The integer you entered is" + a); // and you print a


    String d = b.nextLine(); // you read the next line in d
...
    System.out.println(" The string you entered is "+b); // and you print b


you will certainly get advantage of learning the debugger.

Your code do not behave the way you expect, and you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


这篇关于获取用户输入时出错(JAVA)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆