我刚刚在大学开始我的第二个编程课程,我们的第一个任务很简单,旨在基本检查我们的环境,并检查我们是否知道如何通过课程网站提交作业。
当我运行我们提供的代码时,它会挂起它应该提示用户输入数字的位置,以便它可以打印它。我插入了一系列println语句来确定它挂在哪里。
它打印TEST1,TEST2和TEST3,但从未进入TEST4。所以这条线肯定有问题: number = input.nextInt();
但我不能为我的生活看到这条线的错误。任何帮助将不胜感激! :)
无论如何,这是代码
package rossassignment1;
import java.util.Scanner; // use the Scanner class located in the "java.util" directory
public class RossAssignment1 {
public static void main (String[] args) {
System.out.println("TEST 1");
int number;
System.out.println("TEST 2");
Scanner input = new Scanner(System.in);
System.out.println("TEST 3");
number = input.nextInt();
System.out.println("TEST 4"); // display the number with other messages
System.out.print("This program reads an integer from a keyboard,\n"
+ " and print it out on the display screen.\n"
+ "The number is: " + number + ".\n"
+ "make sure that you get the exact same output as the expected one!\n");
}
}
答案 0 :(得分:0)
在运行line.nextInt()之后,程序正在等待用户(您或者正在使用的任何标记系统)在控制台中输入整数。完成后,程序将继续执行TEST4系列。
答案 1 :(得分:0)
如果在输入整数后,之后的代码仍未显示,则可以执行以下操作:
number = input.nextInt();
input.nextLine(); //Add this
它将清除换行符。
或者,我更喜欢这样做:
number = Integer.parseInt(input.nextLine());