为什么我的控制台显示为空白?
在我的作业的第一 部分 /初始阶段中,我只是试图让计算机告诉我一周中的哪一天用户根据相应的整数输入(0是星期日,1是星期一,等等)。
/*
Write a program that prompts the user to enter an integer for today’s day of
the week
(Sunday is 0, Monday is 1, ..., and Saturday is 6). Also prompt the user to
enter the
number of days after today for a future day and dis- play the future day of
the week.
*/
import java.util.Scanner; //import scanner
public class Program{
public static void main(String[] args){
Scanner input = new Scanner(System.in); //create a scanner object
int dayIntInput = input.nextInt(); //assign next integer input to dayInt
System.out.println("What is today's corresponding integer (Sunday=0, Monday=1, etc...."); //ask user for integer
String[] daysOfWeek = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; //make array of days of week
String dayOfWeek = daysOfWeek[dayIntInput];
System.out.println(dayOfWeek);
}
}
最佳答案
您在显示消息以告诉用户输入内容之前请求输入。因此,当您运行该程序时,它看起来刚刚停止运行。事实上,它正在等待您输入内容。
对代码进行一些重新排列将使程序更加用户友好:
Scanner input = new Scanner(System.in); //create a scanner object
// Prompt user first...
System.out.println("What is today's corresponding integer (Sunday=0, Monday=1, etc...."); //ask user for integer
// ... THEN accept input
int dayIntInput = input.nextInt(); //assign next integer input to dayInt
关于java - 获取空白控制台输出(System.out.println 不起作用),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44663498/