本文介绍了Java用户输入以及readInt和nextInt之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这有什么问题?

import java.io.*;

class TUI{

    public static void main(String[] args) {

        System.out.println("Enter the two numbers:");
        int n1=readInt("Enter n1:");
        int n2=readInt("Enter n2:");
        int total=n1+n2;
        System.out.println("Total is =" + total+".");
    }
}

遇到这些错误

Day2.java:5: error: cannot find symbol
    int n1=readInt("Enter n1:");
           ^
  symbol:   method readInt(String)
  location: class TUI
Day2.java:6: error: cannot find symbol
    int n2=readInt("Enter n2:");
           ^
  symbol:   method readInt(String)
  location: class TUI

PS- readInt nextInt 有什么区别?
我可以在这里使用 nextInt

PS- Also What is the difference between readInt and nextInt ?Can I use nextInt here

推荐答案

您需要一些东西像扫描仪一样从控制台读取值。
代码应如下所示:

You need something like a scanner to read-in values from console.The code should look like that:

import java.util.Scanner;

class TUI {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter the two numbers:");
        System.out.println("Enter n1:");
        int n1 = scanner.nextInt();
        System.out.println("Enter n2:");
        int n2 = scanner.nextInt();
        int total = n1 + n2;
        System.out.println("Total is =" + total + ".");
        scanner.close();
    }
}

我希望它会有所帮助。

这篇关于Java用户输入以及readInt和nextInt之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-21 19:20