简体   繁体   English

无法使用扫描仪类完成简单功能

[英]Unable to complete a simple function using scanner class

I am trying to solve a question on the online platform of Infosys Ltd InfyTQ platform.我正在尝试在 Infosys Ltd InfyTQ 平台的在线平台上解决问题。 The problem is in java.问题出在java中。 The problem description is as follows:问题描述如下:

Implement a program to display the geometric sequence as given below for a given value n, where n is the number of elements in the sequence.实现一个程序来显示给定值 n 的几何序列,如下所示,其中 n 是序列中元素的数量。

-------> 1, 2, 4, 8, 16, 32, 64, ______, 1024 ------> 1, 2, 4, 8, 16, 32, 64, ______, 1024

Sample Input and Output示例输入和输出

-------> 5:::: 1, 2, 4, 8, 16 -------> 8:::: 1, 2, 4, 8, 16, 32, 64, 128 ------> 5:::: 1, 2, 4, 8, 16 ------> 8::: 1, 2, 4, 8, 16, 32, 64, 128

What I am given is the following:我得到的是以下内容:

class Tester {
    public static void main(String[] args) {
        // Implement your code here 
    }
}

What I am trying to implement is the following我要实现的是以下内容

import java.util.Scanner;

class Tester {
    public static void main(String[] args) {
        // Implement your code here
        int sequence=1;
        
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the length of sequence: ");      

        // input the lenght of the sequence
        int n = sc.nextInt();
        while(n!=0)
        {
           System.out.print(sequence);
           System.out.print(" ");
           sequence*=2;
           n-=1;
        }
    }
}

I have tried the above code in an online compiler and on my system on the Visual Studio Code.我已经在在线编译器和我的 Visual Studio Code 系统上尝试了上述代码。 While in the online compiler it's working flawlessly.在在线编译器中,它运行完美。 On my system it's not giving any errors.在我的系统上它没有给出任何错误。 The errors while compiling on their platform are as follows:在他们的平台上编译时的错误如下:

Enter the length of sequence: Runtime Exception输入序列长度:Runtime Exception

Exception in thread "main" java.util.NoSuchElementException
at java.base/java.util.Scanner.throwFor(Scanner.java:937)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
at Tester.main(myCode.java:12)

I am unable to modify anything on the online platform that I am working on.我无法修改我正在使用的在线平台上的任何内容。 So nothing deep can be done.所以没有什么深层次的可以做。 Please help solve the above problem so that it's accepted by the platform, so that I may proceed further.请帮忙解决以上问题,让平台接受,以便我进行下一步。

The errors while compiling on their platform are as given above.在他们的平台上编译时的错误如上所述。

The logic is flawless.逻辑是完美的。 The only problem is with their system I think.我认为唯一的问题是他们的系统。 But maybe I am going wrong somewhere here.但也许我在这里的某个地方出错了。 Maybe the SC is leaking.也许 SC 正在泄漏。 I don't know for sure.我不确定。 The errors are the only ones that are given above.错误是上面给出的唯一错误。 Please help in the regard.请在这方面提供帮助。

It seems like standard input is not available on this platform... a thing I would try is using next() and Integer.parseInt() but I'm almost 100% sure this wouldn't help.似乎标准输入在这个平台上不可用……我会尝试使用 next() 和 Integer.parseInt() 但我几乎 100% 肯定这不会有帮助。 Since the exception involved is NoSuchElementException, I'm pretty sure it's also useless to use try{} and catch (java.util.NoSuchElementException e) {}... it really seems like standard input is not enabled.由于涉及的异常是 NoSuchElementException,我很确定使用 try{} 和 catch (java.util.NoSuchElementException e) {} 也是没用的……看起来确实没有启用标准输入。 Still, give this a try:不过,试试这个:

import java.util.Scanner;
class Tester {
    public static void main(String[] args) {
        // Implement your code here
        int sequence=1;
        
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the length of sequence: ");      

        // input the lenght of the sequence
        int n = 0;
        do
        {
            try
            {
               n = sc.nextInt();
            }
            catch (java.util.NoSuchElementException e)
            { // intentionally left blank
            }
        } while (sc.hasNextLine());
        while(n!=0)
        {
           System.out.print(sequence);
           System.out.print(" ");
           sequence*=2;
           n-=1;
        }
        sc.close();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM