I am a beginner about a month after I started studying. I am writing within the knowledge I know, so I would be grateful if you could tell me any other ideas. It may also contain incorrect content.
I thought that I could do it by using a switch statement, and it was completed in about 40 minutes.
//Kitasoft Kobo Java Exercises
import java.util.Scanner;
public class Example {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int week; //Day of the week
int time; //Time zone
int result = 2; //0: Closed 1: Open 2:Update with error switch
String[] weeks = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
String[] times = {"a.m.", "afternoon", "Night"};
String[] results = {"It is a closed day", "it is open","error:Invalid input"};
System.out.println("Please enter the number corresponding to the desired day and time");
do {
System.out.println("Day of the week: 0=Sunday 1=Monday 2=Tuesday 3=Wednesday 4=Thursday 5=Friday 6=Saturday");
System.out.print("Day of the week:");
week = sc.nextInt();
if(week > 6) {
System.out.println("Error: Invalid input");
System.out.println("=====================");
}
} while(week > 6);
do {
System.out.println("Time: 0=1 am=2 pm=Night");
System.out.print("Time zone:");
time = sc.nextInt();
if(time > 2) {
System.out.println("Error: Invalid input");
System.out.println("=====================");
}
} while(time > 2);
sc.close();
switch(week) {
case 0:
result = 1; //All closed
break;
case 1:
case 4:
result = 0; //All open
break;
case 2:
case 5:
if(time == 0) { //Closed in the morning
result = 0;
break;
} else {
result = 1;
break;
}
case 3:
if(time == 2) { //Closed at night
result = 0;
break;
} else {
result = 1;
break;
}
case 6:
if(time == 0) { //Open in the morning
result = 1;
break;
} else {
result = 0;
break;
}
}
System.out.println(weeks[week] + "of" + times[time] + "The hospital" + results[result]);
}
}
I wanted to write it out for the time being, so I stopped using methods. Enter the day of the week and the time zone, and if you enter a different value, enter it again. ** → How can I start over? ** ** → Repeat with a do while statement
It is troublesome to write a lot when using a switch statement. ** → Is it possible to judge the day of the week and the time zone by logical product? ** ** → Give up without thinking of a way
**-The local variable result may not have been initialized ** Initially, the value of the result field was not initialized.
int result; //Before correction
Because I will update it later, so I don't have to decide now. Later, when I checked that the error occurred because it was initialized properly ... ** "The Java compiler performs some parsing including control statements (if, switch, for, etc.) to determine compilation errors. In this program, the process of initializing the result is the switch statement. If it does not meet the conditional expression, it will not be executed, so it is judged as a compile error that "it may not have been initialized". ** Then it's okay if you initialize the field from the beginning, right? → By the way, if the value of the field is not updated, an error will be displayed.
** Resource leak
I feel like I can put together more. When I realized that I was supposed to use boolean type, this happened. Even if I enter an integer in the week field, if it is not correct, I tried to redo it → I gave up because I don't know what to do when a minus or a character is typed.
** ・ Points to put together well ** I noticed during posting now that I could make 6 patterns depending on whether it was a time zone x a holiday. It should have been stored more beautifully ... I would like to know if you have the idea of doing this.
** ・ // How to use comments ** Anyway, I didn't have a chance to show the source code to others, so I don't know how to use the comments well. What else should I tell you? Is it just hard to see if you add too many comments?
** ・ What is a resource leak? ** ** I looked it up, but I don't think I can understand it very well. What kind of keyword should I search for?
I'm really just starting to learn, so I only know the basic fields From now on, I will publish my creations regularly, including what I was thinking I hope to make it public. I would be happy if you could give me a response.
Recommended Posts