gpt4 book ai didi

java - 找不到符号 System.console().readInt();

转载 作者:行者123 更新时间:2023-12-01 18:10:20 27 4
gpt4 key购买 nike

这是我的代码:

import java.io.PrintWriter;
import java.io.FileNotFoundException;

public class Main {
public static void main(String[] args) throws FileNotFoundException {

String enter = "Enter";
String resolve = "Resolve";
String store = "Store";

// if file doesn't exist
int nextjob = 1;
int jobnumber = nextjob;
int phonenumber = System.console().readInt();
int numberoflines = System.console().readInt();
String problem = System.console().readLine();
int time = System.console().readInt();

String command = System.console().readLine();
if(command.equals(store)){
PrintWriter writer = new PrintWriter("openjobs.txt");
writer.println(nextjob);
writer.println(jobnumber);
writer.println(phonenumber);
writer.println(numberoflines);
writer.println(problem);
writer.println(time);
writer.close();
}
}
}

这是输出:

Main.java:14: error: cannot find symbol
int phonenumber = System.console().readInt();
^
symbol: method readInt()
location: class Console

最佳答案

使用ScannerScanner.nextInt()方法仅将整数作为用户的输入。

Scanner sc = new Scanner(System.in);
int anyNumber = sc.nextInt();

如果用户输入的不是整数,则会抛出InputMismatchException .

关于java - 找不到符号 System.console().readInt();,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33521839/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com