Java中使用方法时出现循环问题

发布于 2024-09-14 00:46:31 字数 987 浏览 2 评论 0原文

我正在做一个关于方法的简单程序。 但我有一个问题。除了循环之外,一切都已经正常工作。 当我选择再次循环时。程序会跳过输入名称。并直接进入年份和章节。 这是代码:

public static void main(String[] args) {
do{
    System.out.println("Input info:");
        name=stringGetter("Name: ");
        yearandsec=stringGetter("Year and section: ");
        sex_code=charGetter("Sex code: " + "\n"  + "[M]" + "\n" + "[F]:");
        scode=intGetter("Scholarship code: ");
        ccode=intGetter("Course code: ");
        units=intGetter("Units: ");

        fee_per_unit=doubleGetter("Fee per unit: ");
        misc=doubleGetter("Miscellaneous: ");
        display();
         switches(scode, units, fee_per_unit, misc);
System.out.println("Another?");
dec=rew.nextInt();
}while(dec==1);




    }

这是获取名称值以及年份和部分的方法:

public static String stringGetter(String ny){
       String sget;
        System.out.println(ny);
       sget=rew.nextLine();
       return sget;

    }

我真的对这个问题很恼火,而且我不知道如何解决这个问题。请帮忙。谢谢

I'm doing a simple program regarding methods.
But I have one problem. Everything is already working except when looping.
When I choose to loop again. The program skips on inputting the name. And proceeds directly to the year and section.
Here's the code:

public static void main(String[] args) {
do{
    System.out.println("Input info:");
        name=stringGetter("Name: ");
        yearandsec=stringGetter("Year and section: ");
        sex_code=charGetter("Sex code: " + "\n"  + "[M]" + "\n" + "[F]:");
        scode=intGetter("Scholarship code: ");
        ccode=intGetter("Course code: ");
        units=intGetter("Units: ");

        fee_per_unit=doubleGetter("Fee per unit: ");
        misc=doubleGetter("Miscellaneous: ");
        display();
         switches(scode, units, fee_per_unit, misc);
System.out.println("Another?");
dec=rew.nextInt();
}while(dec==1);




    }

Here's the method getting the value for name together with the year and section:

public static String stringGetter(String ny){
       String sget;
        System.out.println(ny);
       sget=rew.nextLine();
       return sget;

    }

I'm really annoyed with this problem, and I don't have any idea on how to fix this. Please help. thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冰葑 2024-09-21 00:46:31

下面是一个更简单、更完整的程序,它重现了该错误:

public static Scanner rew = new Scanner(System.in);

public static void main(String[] args) {
    int dec;
    do {
        System.out.println("Input info:");
        String name=stringGetter("Name: ");
        String yearandsec=stringGetter("Year and section: ");
        dec=rew.nextInt();
    } while(dec==1);
}

public static String stringGetter(String ny){
    System.out.println(ny);
    return rew.nextLine();
}

问题是,在调用 nextInt() 后,对 nextLine() 的调用会读取到int(给出一个空行),不超过下一个新行。

如果将 dec 更改为字符串并将 dec=rew.nextInt(); 更改为 dec=rew.nextLine(); 那么它将正常工作。这是一个完整的示例,您可以将其复制并粘贴到空白文件中以查看其是否正常工作:

import java.util.*;

public class Program
{
    public static Scanner rew = new Scanner(System.in);

    public static void main(String[] args) {
        String dec;
        do {
            System.out.println("Input info:");
            String name = stringGetter("Name: ");
            String yearandsec = stringGetter("Year and section: ");
            dec = stringGetter("Enter 1 to continue: ");
        } while(dec.equals("1"));
    }

    public static String stringGetter(String ny){
        System.out.println(ny);
        return rew.nextLine();
    }
}

您可能还需要考虑向程序添加适当的解析和验证。目前,如果用户输入无效数据,您的程序将会出现不良行为。

Here is a simpler and more complete program that reproduces the error:

public static Scanner rew = new Scanner(System.in);

public static void main(String[] args) {
    int dec;
    do {
        System.out.println("Input info:");
        String name=stringGetter("Name: ");
        String yearandsec=stringGetter("Year and section: ");
        dec=rew.nextInt();
    } while(dec==1);
}

public static String stringGetter(String ny){
    System.out.println(ny);
    return rew.nextLine();
}

The problem is that after calling nextInt() the call to nextLine() reads up to the new line after the int (giving a blank line), not up to the next new line.

If you change dec to a String and change dec=rew.nextInt(); to dec=rew.nextLine(); then it will work fine. Here is a complete example that you can copy and paste into a blank file to see that it works correctly:

import java.util.*;

public class Program
{
    public static Scanner rew = new Scanner(System.in);

    public static void main(String[] args) {
        String dec;
        do {
            System.out.println("Input info:");
            String name = stringGetter("Name: ");
            String yearandsec = stringGetter("Year and section: ");
            dec = stringGetter("Enter 1 to continue: ");
        } while(dec.equals("1"));
    }

    public static String stringGetter(String ny){
        System.out.println(ny);
        return rew.nextLine();
    }
}

You may also want to consider adding proper parsing and validation to your program. Currently your program will behave in an undesirable way if the user enters invalid data.

野生奥特曼 2024-09-21 00:46:31

该行:

dec = rew.nextInt();

正在从输入流中读取 int 值并且不处理换行符,然后当您返回到获取名称的位置时,新行仍在 Reader 的缓冲区中并被 stringGetter 消耗返回名称的空值。

更改该行以执行以下操作:

do {
    //....
    s = stringGetter("Another (y/n)? ");
} while ("y".equals(s));

The line:

dec = rew.nextInt();

Is reading an int value from the input stream and is not processing the newline character, then when you come back to point where you get the name at which point a new line is still in the Reader's buffer and gets consumed by the stringGetter returning an empty value for name.

Change the line to do something like:

do {
    //....
    s = stringGetter("Another (y/n)? ");
} while ("y".equals(s));
眸中客 2024-09-21 00:46:31

好吧,您还没有告诉我们“rew”是什么,也没有告诉我们 rew.nextInt() 的作用。是否有可能 rew.nextInt() 正在等待用户按回车键,但实际上只消耗输入的一个字符 - 以便下一次调用 rew.nextLine()< /code> (名称)立即占据该行的其余部分?我怀疑这就是发生的事情,因为您正在使用 System.in - 通常从 System.in 读取仅在您按回车键时提供任何输入。

(这可能只是 Windows 上的一个问题 - 我想知道它是否使用 System.in 中的“\r”作为分隔符,而将“\n”留在缓冲区中。不确定.)

要测试这一点,请尝试在系统询问您是否继续时输入“1 Jon” - 我认为它将使用“Jon”作为下一个名称。

本质上,我认为当下一次调用 Scanner.nextString() 时,使用 Scanner.nextInt() 将会出现问题。您最好使用 BufferedReader 并重复调用 readLine(),然后自己解析数据。

Well you haven't told us what "rew" is, nor what rew.nextInt() does. Is it possible that rew.nextInt() is waiting for the user to hit return, but only actually consuming one character of the input - so that the next call to rew.nextLine() (for the name) just immediately takes the rest of that line? I suspect that's what's happening because you're using System.in - usually reading from System.in only gives any input when you hit return.

(It's possible that this is also only a problem on Windows - I wonder whether it consumes the "\r" from System.in as the delimiter, leaving "\n" still in the buffer. Not sure.)

To test this, try typing in "1 Jon" when you're being asked whether or not to continue - I think it will then use "Jon" as the next name.

Essentially, I think using Scanner.nextInt() is going to have issues when the next call is to Scanner.nextString(). You might be better off using a BufferedReader and calling readLine() repeatedly, then parsing the data yourself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文