Console.readInt()JAVA编程 [英] Console.readInt() JAVA Programming

查看:369
本文介绍了Console.readInt()JAVA编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以任何人告诉我这个代码是什么错误,为什么Eclipse说方法readInt(String)未定义为类型控制台?
提前谢谢!

can anyone of you tell me what is wrong with this code and why Eclipse says "The method readInt(String) is undefined for the type Console"? Thank you in advance!

    import java.io.*;

    public class Matrix {

        private int [] [] matrice; 

        /* Metodo Costruttore */
        Matrix(int i, int j) {
            matrice = new int [i] [j];
        }

        /* reads matrix */
        public void leggiMatrix (int ni, int nj) {
            int i, j;

            for (i=0; i<ni; i++ )
                for (j=0; j<nj; j++)
                    matrice [i] [j] = Console.readInt("Inserire elemento " + i    + ", " + j + " : ");
        }

        /* prints matrix */
        public void stampaMatrix(int ni, int nj) {
            int i, j;

            for (i=0; i<ni; i++) {
                for (j=0; j<nj; j++) {
                    System.out.print(matrice[i][j] + " ");
                    System.out.println();
                }
            }
        }` 

UPDATE

        /* reads matrix */
        public void leggiMatrix (int ni, int nj) {
            int i, j;

            for (i=0; i<ni; i++ )
                for (j=0; j<nj; j++)
                    matrice [i] [j] = Integer.parseInt(Console.readline("Inserire elemento " + i + ", " + j + " : "));
        }

现在我得到了在控制台类型中的方法readLine适用于参数(String)

now I get "The Method readLine(Boolean) in the type Console is not applicable for the arguments (String)"

推荐答案

这是因为 readInt 不是控制台的功能。你正在寻找的是:

This is because readInt isn't a function of Console. What you are looking for is:

//Read input from console
String input = System.console.readLine()

//Convert string to integer
Integer.parseInt(input)

这篇关于Console.readInt()JAVA编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆