英文:
I want to make Java count from one input integer to another, printing weekdays
问题
首先,我完全是Java的新手,我知道对于大多数人来说,这可能非常简单。但现在我卡在了一个练习上。我已经疯狂地搜索了,但找不到针对这个特定问题的有用提示。要么是我不知道该搜索什么。
我有一段代码,看起来像这样:
import java.util.Scanner;
public class Week {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Type a number: ");
int day = input.nextInt();
if (day == 1) {
System.out.println("Monday ");
}
if (day == 2) {
System.out.println("Tuesday ");
}
if (day == 3) {
System.out.println("Wednesday ");
}
if (day == 4) {
System.out.println("Thursday ");
}
if (day == 5) {
System.out.println("Friday ");
}
if (day == 6) {
System.out.println("Saturday ");
}
if (day == 7) {
System.out.println("Sunday");
}
}
}
正如大家可以看到的,这个程序提示用户输入一个数字,如果该数字在1到7之间,程序将回答对应的星期几。然而,我现在想修改代码以启动一个计数器或循环或其他什么东西。我想让它从我的输入数字开始计数加1,直到达到指定的停止位置。比如说,我输入2,程序会打印:星期二、星期三、星期四、星期五,然后停止。
我只是浅尝辄止,了解了一些if语句等知识,但我知道关键在哪里。在尝试的过程中,我只成功地让程序打印2五次,或者让我无限输入数字。
非常感谢任何帮助!
英文:
So first off, I'm completely brand new to Java and I know this is probably really easy for most of you. But right now I'm stuck with an exercise. I have googled almost frantically, but I can't find any helpful tips to this particular problem. That or I don't really know what to search for.
I have a piece of code that looks like this:
import java.util.Scanner;
public class Week {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Type a number: ");
int day = input.nextInt();
if (day == 1) {
System.out.println("Monday ");
}
if (day == 2) {
System.out.println("Tuesday ");
}
if (day == 3) {
System.out.println("Wednesday ");
}
if (day == 4) {
System.out.println("Thursday ");
}
if (day == 5) {
System.out.println("Friday ");
}
if (day == 6) {
System.out.println("Saturday ");
}
if (day == 7) {
System.out.println("Sunday");
}
}
}
As all you can probably see, this program prompts the user to type a number, and if that number is somewhere between 1-7 the program will answer with the corresponding day of the week. What I would like to do now however, is to modify the code to start a counter or loop or something. I want to make it count +1 from my input number to a specified break. Let's say I type 2, and the program would print: Tuesday, Wednesday, Thursday, Friday and then make it stop.
I have only really dipped my toe in for, while, if statements etc but I know that somewhere there is the key. While playing around I only succeeded in making the program print 2 five times, or letting me type numbers for infinity.
Any help would be greatly appreciated!
答案1
得分: 1
因为你现在正在学习,所以我会给你一些线索,来点亮前方的道路,而不是直接给出答案。
首先,要解决一个问题,你必须清楚地定义它是什么。我们假设小于1或大于7的数字不在考虑范围内。
我理解你需要输入两个数字:开始日期和结束日期。你已经知道如何存储第一个数字。
第二个数字也是同样的过程。但有一个问题。Scanner.nextInt() 会读取在控制台输入的数字,但不会读取你用于验证值的<ENTER>键。你需要找到如何在两个nextInt()调用之间消耗那个<ENTER>键。
然后是循环。一个循环应该包括:
- 初始化:循环开始时程序的状态是什么(使用开始日期的值,因为它是第一个要显示的内容)
- 循环条件:循环什么时候停止?(或许使用结束日期的值+1,因为它是最后一个要显示的内容)
- 进化指令:每次执行循环时,某些内容都应该进展,以使程序状态越来越接近循环结束的条件(每次循环都加1,因为你想要在开始和结束之间的所有日期)
再进一步:
- 我建议你查看 switch case 语句,以避免在一个变量上执行给定一组值的连续 if 语句进行区分。
- 稍后,你可以学习枚举。这是一种清晰地存储有限状态的方式,而"星期几"是一个很好的例子,可以用来学习枚举。(当然,它们已经存在 -> https://kodejava.org/how-do-i-use-the-java-time-dayofweek-enum/)
英文:
Since you are currently learning, I'll point you some leads to brighten the path to follow instead of giving the answer.
First of all to solve a problem, you have to clearly define what it is. We will assume that numbers bellow 1 or higher than 7 are not in scope.
I understand that you need to enter two numbers : the starting day and the end day. You already know how to store the first one.
The second one is the same process. But there is a catch. Scanner.nextInt() consumes the number entered in the console but not the <ENTER> you do to validate the value. You have to find how to consume that <ENTER> between the two nextInt() calls.
Then loops. A loop shall be :
- an initialization : what is the state of the program when the loops begin (use the start day value since it is the first thing to display)
- a condition to loop : when does the loop stops ? (use the end day value maybe +1 since it is the last one to display)
- an evolution instruction : every time the loop is performed something shall progress to make your program state go closer and closer to the end loop condition (+1 every loop since you want all days between start and end)
Go further :
- I advise you to look at switch cases to avoid chained if statements that perform a discrimination on a given set of values on one variable
- Later, you could look at enums. It is a way to store finite states and week Days clearly is one good exemple to learn them. (of cours they already exist -> https://kodejava.org/how-do-i-use-the-java-time-dayofweek-enum/)
答案2
得分: 0
public class Week {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入一个数字:");
int day = input.nextInt();
System.out.println("请输入一个停止数字:");
int stopDay = input.nextInt();
for (int i = day; i <= stopDay; i++) { // 如果停止日需要打印,则使用'<=', 否则使用'<'
printWeekDay(i);
}
}
private static void printWeekDay(int day) {
if (day == 1) {
System.out.println("星期一");
}
if (day == 2) {
System.out.println("星期二");
}
if (day == 3) {
System.out.println("星期三");
}
if (day == 4) {
System.out.println("星期四");
}
if (day == 5) {
System.out.println("星期五");
}
if (day == 6) {
System.out.println("星期六");
}
if (day == 7) {
System.out.println("星期日");
}
}
}
英文:
public class Week {
public static void main(String[] args) { Scanner input = new Scanner(System.in);
System.out.println("Type a number: ");
int day = input.nextInt();
System.out.println("Type a stop number: ");
int stopDay = input.nextInt();
for (int i = day; i <= stopDay; i++) { // if the stopday should be printed, then use '<=', '<' otherwise
printWeekDay(i);
}
}
private static void printWeekDay(int day) {
if (day == 1) {
System.out.println("Monday ");
}
if (day == 2) {
System.out.println("Tuesday ");
}
if (day == 3) {
System.out.println("Wednesday ");
}
if (day == 4) {
System.out.println("Thursday ");
}
if (day == 5) {
System.out.println("Friday ");
}
if (day == 6) {
System.out.println("Saturday ");
}
if (day == 7) {
System.out.println("Sunday");
}
}
}
答案3
得分: 0
你可以创建一个DAYS[]
数组,并从特定索引开始迭代数组。
String DAYS[] = {"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
System.out.println("请输入一个数字:");
int day = input.nextInt();
if(day >= 1 && day <= 7) {
for(int i = day - 1; i < DAYS.length; i++) {
System.out.println(DAYS[i]);
}
}
else
System.out.println("请输入有效的数字");
如果你想要通过输入停止迭代,逻辑仍然相同。只需将for
循环中的DAYS.length
替换为你想要的停止索引
。
英文:
You could just create an array of DAYS[]
and iterate through the array from that particular index towards the end.
String DAYS[] = {"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
System.out.println("Type a number: ");
int day = input.nextInt();
if(day >= 1 && day <= 7) {
for(int i = day - 1; i < DAYS.length; i++) {
System.out.println(DAYS[i]);
}
}
else
System.out.println("Enter a valid number");
The logic would be the same if you want to stop by taking in the stop index. Just replace the DAYS.length
in the for
loop with your desired stop index
答案4
得分: 0
以下是翻译好的内容:
我会使用switch-case来完成这个操作,在其中当我想要程序打印特定的一天时,我会使用break
来在执行匹配的case
后跳出switch-case块。如果在一个case中没有使用break
,执行将会继续穿过剩余的case,直到找到一个break
或块结束的地方。
为了能够持续重复这个过程,我会使用一个无限循环(while(true) { }
),可以根据业务逻辑使用break
语句来跳出循环。
注意,我还使用了Integer.parseInt(input.nextLine())
代替input.nextint()
,以避免在这里描述的问题(https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo)。
演示:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("+=+=+ 菜单 +=+=+");
System.out.println("A. 显示特定的一天");
System.out.println("B. 显示从特定日期开始的所有日期");
System.out.println("C. 退出");
String choice;
while (true) {
System.out.print("输入你的选择: ");
choice = input.nextLine().toUpperCase();
if ("C".equals(choice)) {
System.out.println("再见!");
break;
}
System.out.print("输入一个数字: ");
int day = Integer.parseInt(input.nextLine());
if ("A".equals(choice)) {
switch (day) {
case 1:
System.out.println("星期一");
break;
case 2:
System.out.println("星期二");
break;
case 3:
System.out.println("星期三");
break;
case 4:
System.out.println("星期四");
break;
case 5:
System.out.println("星期五");
break;
case 6:
System.out.println("星期六");
break;
case 7:
System.out.println("星期日");
break;
}
} else if ("B".equals(choice)) {
switch (day) {
case 1:
System.out.println("星期一");
case 2:
System.out.println("星期二");
case 3:
System.out.println("星期三");
case 4:
System.out.println("星期四");
case 5:
System.out.println("星期五");
case 6:
System.out.println("星期六");
case 7:
System.out.println("星期日");
}
}
}
}
}
一个样例运行:
+=+=+ 菜单 +=+=+
A. 显示特定的一天
B. 显示从特定日期开始的所有日期
C. 退出
输入你的选择: A
输入一个数字: 4
星期四
输入你的选择: B
输入一个数字: 4
星期四
星期五
星期六
星期日
输入你的选择: C
再见!
英文:
I would do it using switch-case in which when I want the program to print a specific day, I would use break
which break the switch-case block after executing the matching case
. If break
is not used in a case, the execution falls through the remaining cases until a break
is found or where the block ends.
In order to continue doing it repeatedly, I would use an infinite loop (while(true) { }
) which can be broken using break
statement as per the business logic.
Note that I have also used Integer.parseInt(input.nextLine())
instead of input.nextint()
to avoid the problem described here.
Demo:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("+=+=+ Menu +=+=+");
System.out.println("A. Show a specific day");
System.out.println("B. Show all days starting from the specific day");
System.out.println("C. Exit");
String choice;
while (true) {
System.out.print("Enter your choice: ");
choice = input.nextLine().toUpperCase();
if ("C".equals(choice)) {
System.out.println("Good bye!");
break;
}
System.out.print("Type a number: ");
int day = Integer.parseInt(input.nextLine());
if ("A".equals(choice)) {
switch (day) {
case 1:
System.out.println("Monday ");
break;
case 2:
System.out.println("Tuesday ");
break;
case 3:
System.out.println("Wednesday ");
break;
case 4:
System.out.println("Thursday ");
break;
case 5:
System.out.println("Friday ");
break;
case 6:
System.out.println("Saturday ");
break;
case 7:
System.out.println("Sunday");
break;
}
} else if ("B".equals(choice)) {
switch (day) {
case 1:
System.out.println("Monday ");
case 2:
System.out.println("Tuesday ");
case 3:
System.out.println("Wednesday ");
case 4:
System.out.println("Thursday ");
case 5:
System.out.println("Friday ");
case 6:
System.out.println("Saturday ");
case 7:
System.out.println("Sunday");
}
}
}
}
}
A sample run:
+=+=+ Menu +=+=+
A. Show a specific day
B. Show all days starting from the specific day
C. Exit
Enter your choice: A
Type a number: 4
Thursday
Enter your choice: B
Type a number: 4
Thursday
Friday
Saturday
Sunday
Enter your choice: C
Good bye!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论