代码之家  ›  专栏  ›  技术社区  ›  user225269

在Java中使用方法时的循环问题

  •  2
  • user225269  · 技术社区  · 15 年前

    但我有一个问题。除了循环外,一切都已经开始工作了。 代码如下:

    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);
    
    
    
    
        }
    

    下面是获取name值以及年份和节的方法:

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

    我真的对这个问题很恼火,我不知道怎么解决这个问题。请帮忙。谢谢

    3 回复  |  直到 15 年前
        1
  •  2
  •   Mark Byers    15 年前

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

    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=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();
        }
    }
    

        2
  •  1
  •   Michael Barker    15 年前

    线路:

    dec = rew.nextInt();
    

    正在从输入流中读取一个int值,并且不处理换行符,当您返回到获取名称的点时,此时新行仍在读取器的缓冲区中,并被返回空名称值的stringGetter占用。

    更改行以执行以下操作:

    do {
        //....
        s = stringGetter("Another (y/n)? ");
    } while ("y".equals(s));
    
        3
  •  1
  •   Jon Skeet    15 年前

    你还没告诉我们“rew”是什么,也没告诉我们什么 rew.nextInt() 做。有没有可能 正在等待用户点击return,但实际上只消耗了输入的一个字符—以便下次调用 rew.nextLine() 犯罪嫌疑人 这是因为你在使用 System.in System.in

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

    为了测试这一点,当你被问到是否继续时,试着输入“1 Jon”——我想它会用“Jon”作为下一个名字。

    基本上,我认为 Scanner.nextInt() 下次打电话的时候会有问题 Scanner.nextString() . 你最好使用 BufferedReader 打电话来 readLine()