gpt4 book ai didi

Java 的 java.util.Scanner 在 PowerShell ISE 中挂起

转载 作者:太空宇宙 更新时间:2023-11-04 06:54:46 25 4
gpt4 key购买 nike

我很困惑为什么这个 .java 程序(来自 Deitel 的 Java 8 如何编程)在使用 ISE 控制台运行时会在指示的位置挂起,但在使用常规 PowerShell 控制台运行时却不会挂起。对于这个问题有什么想法或经验吗?

// Fig. 2.7: Addition.java
// Addition program that inputs two numbers then displays their sum.
import java.util.Scanner; // program uses class Scanner

public class Addition
{
// main method begins execution of Java application
public static void main(String[] args)
{
// create a Scanner to obtain input from the command window
Scanner input = new Scanner(System.in);

int number1 = 0; // first number to add
int number2 = 0; // second number to add
int sum = 0; // sum of number1 and number2

System.out.print("Enter first integer: "); // prompt
number1 = input.nextInt(); // read first number from user *** hangs! ***

System.out.print("Enter second integer: "); // prompt
number2 = input.nextInt(); // read second number from user *** hangs! ***

sum = number1 + number2; // add numbers, then store total in sum

System.out.printf("Sum is %d%n", sum); // display sum
} // end method main
} // end class Addition

最佳答案

遗憾的是,Windows PowerShell 集成脚本编辑器 (ISE) 不支持接受用户输入的控制台程序(也称为交互式应用程序)。例如,尝试从 PowerShell ISE 运行 nslookup.exe。您会注意到收到一条错误消息。

Cannot start "nslookup". Interactive console applications are not supported. To run the application, use the Start-Process cmdlet or use "Start PowerShell.exe" from the File menu. To view/modify the list of blocked console applications, use $psUnsupportedConsoleA pplications, or consult online help. At line:0 char:0

PowerShell ISE

内置变量 $psUnsupportedConsoleApplications 控制哪些可执行文件可以或不能在 PowerShell ISE 中运行。这只是一个保护层,以确保内置的 Windows 交互程序不会在 PowerShell ISE 中意外执行。您仍然可以运行第三方交互式应用程序,但根据您的经验,它们会失败。

关于Java 的 java.util.Scanner 在 PowerShell ISE 中挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22902045/

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