小编高亮节*_*高亮节的帖子

input.nextInt()如何正常工作?

这是该计划

public class bInputMismathcExceptionDemo {
public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    boolean continueInput = true;
    do {
        try {
            System.out.println("Enter an integer:");
            int num = input.nextInt();

            System.out.println("the number is " + num);
            continueInput = false;
        }
        catch (InputMismatchException ex) {
            System.out.println("Try again. (Incorrect input: an integer is required)");
        } 
        input.nextLine();
    }   
    while (continueInput);

}
}
Run Code Online (Sandbox Code Playgroud)

我知道nextInt()只读整数而不是"\n",但为什么我们需要input.nextLine()读取"\n"?有必要吗??因为我认为即使没有input.nextLine(),在它返回之后try {},input.nextInt()仍然可以读取我输入的下一个整数,但实际上它是一个无限循环.

我仍然不知道它背后的逻辑,希望有人能帮助我.

java

10
推荐指数
2
解决办法
5万
查看次数

根据标准ML中的foldr定义foldl

定义的代码是

fun foldl f e l = let
    fun g(x, f'') = fn y => f''(f(x, y)) 
    in foldr g (fn x => x) l e end
Run Code Online (Sandbox Code Playgroud)

我不明白这是如何运作的; 目的是g(x, f'')什么?

我也在Haskell中找到了一个类似的例子,定义很简短

myFoldl f z xs = foldr step id xs z
    where
        step x g a = g (f a x)
Run Code Online (Sandbox Code Playgroud)

haskell functional-programming sml fold

4
推荐指数
1
解决办法
649
查看次数

标签 统计

fold ×1

functional-programming ×1

haskell ×1

java ×1

sml ×1