课程: Learning Java 17

Input and output in Java - Java教程

课程: Learning Java 17

Input and output in Java

- So far, we've learned how to output information to the user using system.out.println. We've created several variables and printed out their values to the user, both in the same line and on separate lines. In this lesson, we'll look at how we can create a program that allows the user to input information, affecting the program's output. Thinking back to the student program, we might need to update a student's GPA. With some modifications to the code, we can let the user dynamically change the value of a student's GPA. In the previous lesson, we wrote a print statement that tells a given student their GPA. Before updating the GPA, we should ask the user what they want to update it to. That's the print statement I've added here. Next, we need to get access to the user's input. The user should be able to use the console window at the bottom of intelligae, and input what they want the updated value to be. To do this, we can create a scanner with system.n and save the scanner in a variable called input. This is the opposite of system.out. That's what we've been using to print to the console. The scanner has some built in operations that will allow us to retrieve input from the user. This means we'll write input equals new scanner system.n. This creates a new scanner that will scan system.n for the user's input. Now, we get an error here, because we need to add a data type to the variable. The data type is scanner. There are lots of data types in Java, but for now you can think of the scanner as a tool that has operations that allow us to get the input. In order to use those operations, we have to create the scanner first. We also need to import Java.util.scanner, in order to use the scanner. You can think of it as a special data type that we need to import in order to use. Similar to how we used the char at operation on strings, we'll use the next double operation on the scanner. We'll want to save this value that the user enters in a variable. Specifically, the student GPA variable. To do this, we'll add student GPA equals... And, this will assign the value that the user inputs to this variable. Since we've already already introduced and defined the variable, we do not need to add a data type. Java recognizes this variable as a double from its initial definition and it can only store double values. Let's create a nice output string, so the user can see that the student GPA has been updated. We'll use the same print statement as before, and add the word now. Let's run it. The GPA starts at 3.45 and we'll update it to a 4.0. The student now has a GPA of 4.0. We overwrite the value in the student GPA variable with one provided by the user. Now, you might be thinking wouldn't it be awesome if the program could automatically calculate the GPA given a series of grades? As we go deeper into the fundamentals of Java, we'll be able to create more complicated programs like this. You also might be thinking, does this scale? No, right now it only works for one student. For the way we have the program structured right now, we would have to execute this program and edit some of the source code for each student in the class. It's also important to note that nothing about the GPA update is saved externally. The GPA update works for the program's single execution, but it doesn't save it indefinitely. There are ways to make this program more scalable, but this chapter is meant to be an introduction to how we can store and reference simple data pieces using the Java programming language. We'll be building more complicated programs in later chapters.

内容