Java初学者需要帮助解决while循环问题 [英] Java beginner needs help for while loop problem

查看:69
本文介绍了Java初学者需要帮助解决while循环问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这项任务要求我制作一个简单的谢谢你的电子邮件生成器,我输入一个名字和捐赠的金额,然后生成一封电子邮件..这是源代码



The assignment requires me to make a simple 'thankyou email generator' where I enter a name and amount of money donated and it generates an email.. Here is the source code

    while (keepgoing)
        {
            
            
            
            out.println("What is the persons name?");
            
            String personname = keyboard.nextLine();
            
            out.println("How much did they donate?");
            
            int donation = keyboard.nextInt();
            
            out.println(" Dear " + personname + "");
            out.println("Thankyou for your donation of  $"   + donation + "\n");
            
           
            out.println("Regards, Will");

For the first run it works as needed, but on the second it skips the String input and goes straight to the Int.

What I have tried:

It works as needed if personname is an int variable, no matter how many times it loops but once I switch back to String it skips again..

推荐答案

+捐赠+\ n);


out.println(问候,意志);

对于第一次运行,它可以根据需要运行,但在第二次运行时它会跳过String输入并直接进入Int。

我尝试了什么:

如果personname是一个int变量,无论循环多少次但是一旦我切回to String它再次跳过..
" + donation + "\n"); out.println("Regards, Will"); For the first run it works as needed, but on the second it skips the String input and goes straight to the Int. What I have tried: It works as needed if personname is an int variable, no matter how many times it loops but once I switch back to String it skips again..


引用:

但是在第二个它跳过String输入并直接进入Int。

but on the second it skips the String input and goes straight to the Int.



不,它不会神奇地跳过字符串!

只需执行达到 keyboard.nextLine(),它可以在键盘中找到所需的一切缓冲然后处理它而不等待。

作为证据,输入捐赠为123 456abc


No, it is not magically skip the string!
Simply when execution reach keyboard.nextLine(), it find all what it need in keyboard buffer and then process it without waiting.
as proof, input a donation as "123 456abc"

123<Space>456abc<Enter>



456abc将是您的下一个人名。



您需要阅读有关的文档键盘输入功能。


"456abc" will be your next "persons name".

You need to read documentation about keyboard input functions.


在每个nextInt()之后添加换行符:

Add a newline after every nextInt():
int donation = keyboard.nextInt();
keyboard.nextLine();

nextLine() [ ^ ]和 nextInt() [ ^ ]表现不同。

+++++ [更多详细说明] +++++

nextInt()只需要输入的下一个整数,忽略换行符,然后在下一次迭代中由

nextLine()[^] and nextInt()[^] behave differently.
+++++[More Elaboration]+++++
nextInt() only takes the next integer inputted, ignoring the newline, which is then taken in by

String personname = keyboard.nextLine()

接收,这解释了为什么跳过它而不等待用户的输入。 nextInt()之后的独立keyboard.nextLine()只是在转到字符串personname = keyboard.nextLine()之前吸收剩余的新行。

in the next iteration, that explained why it was skipped without waiting for user's input. The standalone keyboard.nextLine() after the nextInt() is just a hack to absorb this left over newline before it go to String personname = keyboard.nextLine().


这篇关于Java初学者需要帮助解决while循环问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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