Java语言用户输入错误与导入文件

发布于 2025-02-13 08:55:03 字数 1228 浏览 0 评论 0 原文

我面临这个问题,我的代码没有错误。这是代码。请记住,请记住我在Android Studio上写Java,因为我正在从事应用程序开发。

练习.java

package com.example.javastart;
import java.util.Scanner;

public class practice {
public static void main(String[] args) {
    System.out.println("Taking Input");
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter Number for Table ");
    int a = sc.nextInt();


    for (int i = 1; i <= 10; i++) {
        int total = a * i;
        System.out.println(a + " * "+ i + " = "+ total);

    }
}

}

这是一些错误

Task :javastart:practice.main() FAILED
Taking Input
Enter Number for Table 
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 com.example.javastart.practice.main(practice.java:9)

 Execution failed for task ':javastart:practice.main()'.

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

I am facing this problem, I have no errors in my code. Here is the code .Please remember I am writing java in android studio because I am working on app development.

practice.java

package com.example.javastart;
import java.util.Scanner;

public class practice {
public static void main(String[] args) {
    System.out.println("Taking Input");
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter Number for Table ");
    int a = sc.nextInt();


    for (int i = 1; i <= 10; i++) {
        int total = a * i;
        System.out.println(a + " * "+ i + " = "+ total);

    }
}

}

Here's some error

Task :javastart:practice.main() FAILED
Taking Input
Enter Number for Table 
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 com.example.javastart.practice.main(practice.java:9)

 Execution failed for task ':javastart:practice.main()'.

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

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

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

发布评论

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

评论(1

深巷少女 2025-02-20 08:55:03

nosuchelementException 如果没有更多令牌,将抛出。这是由调用 nextInt()而无需检查是否可用的整数引起的。尝试使用hasnextint()在调用 nextint()之前检查是否有任何令牌。

NoSuchElementException will be thrown if no more tokens are available. This is caused by invoking nextInt() without checking if there's any integer available. Try using hasNextInt() to check if any tokens are available before calling nextInt().

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