为什么我从控制台变空?
在我的任务的第一个 部分 /初始阶段中,我只是想让电脑告诉我一周的哪一天用户输入的对应整数(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);
}
}
答案 0 :(得分:5)
您在显示消息之前请求输入以告知用户输入内容。因此,当您运行该程序时,看起来像它刚刚停止冷。事实上,它正等着你输入一些东西。
稍微重新排列代码将使程序更加用户友好:
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
答案 1 :(得分:1)
我认为你的意思是将System.out.println放在获得用户输入的行之前(nextInt()一行)