没有字符输入扫描仪会产生无限循环

时间:2018-09-19 15:31:04

标签: java if-statement java.util.scanner

我遇到了一个奇怪的问题(或者至少对我来说很奇怪)。我有一些if-else-if语句,它们引起奇怪的行为。对于上下文,我实质上是使用char类并使用Scanner来输入Scanner.nextInt().charAt(0)。如果我检查所需的字符并输入一个完全随机的字符(它会向终端返回文本,中断并退出),那么似乎可以正常工作。 但是,如果我只是在char提示符下按回车键,它将创建一个似乎无限循环的内容(可能是内存泄漏?)。随附的是我正在处理的代码,以及在Coderunner2中运行的屏幕截图。

谢谢!

import java.util.*;
import java.io.*;

public class phonebill 
{
    public static void main(String[] arg)
    {
        Scanner sc = new Scanner(System.in);

        System.out.print("Please input the account number: ");
        int acct = sc.nextInt(); // reads arbitrary account number
        System.out.print("Please input service code: ");
        char svc_code = sc.next().charAt(0); // reads service code 
character

    /* the following blocks check for the appropriate service codes.
     * valid codes include p or P for 'Premium' service, and 
     * r or R for regular service. if no valid code is read,
     * the program exits, informing the user to input a valid code.
     */
        if (svc_code == 'p' || svc_code == 'P')
        {
            System.out.print("Please input daytime minutes: ");
            int day_min = sc.nextInt();
            System.out.print("Please input nighttime minutes: ");
            int night_min = sc.nextInt();
            System.out.println("Service code is " + svc_code + " and this condition works.");
        }
        else if (svc_code == 'r' || svc_code == 'R')
        {
            System.out.print("Please input used minutes: ");
            int mins = sc.nextInt();
            System.out.println("Service code is " + svc_code + " and this condition works.");
        }
        else 
        {
            System.out.println("Please input a valid service code.");
        }
        System.out.print("That's all for now folks");
    }
}

Code running normally

Code being weird

更新:我只是对Java和扫描仪不好。

1 个答案:

答案 0 :(得分:1)

这不是无限循环或内存泄漏。

它只是在等待您输入非空字符串。

$ make dev
==> Checking that code complies with gofmt requirements...
go generate ./...
2018/09/19 15:27:28 Generated command/internal_plugin_list.go
stringer: checking package: decoder_spec.go:4:2: could not import github.com/hashicorp/hcl2/hcldec (type-checking package "github.com/hashicorp/terraform/vendor/github.com/hashicorp/hcl2/hcldec" failed (/opt/gopath/src/github.com/hashicorp/terraform/vendor/github.com/hashicorp/hcl2/hcldec/block_labels.go:4:2: could not import github.com/hashicorp/hcl2/hcl (type-checking package "github.com/hashicorp/terraform/vendor/github.com/hashicorp/hcl2/hcl" failed (/opt/gopath/src/github.com/hashicorp/terraform/vendor/github.com/hashicorp/hcl2/hcl/diagnostic.go:4:2: could not import fmt (type-checking package "fmt" failed (/opt/go/src/fmt/format.go:8:2: could not import strconv (type-checking package "strconv" failed (/opt/go/src/strconv/atof.go:13:8: could not import math (type-checking package "math" failed (/opt/go/src/math/exp_asm.go:9:8: could not import internal/cpu (type-checking package "internal/cpu" failed (/opt/go/src/internal/cpu/cpu_x86.go:9:7: CacheLineSize redeclared in this block))))))))))))
config/configschema/schema.go:80: running "stringer": exit status 1

(空白行是我刚按下Enter的地方。我最终键入了“字符串”,然后停止了。)