简体   繁体   中英

In Java, how do do I use Scanner Class to take in multiple inputs in one line without Loop Statements?

在此处输入图片说明

This is my current code thus far. As you can see, it takes three seperate lines to input the coefficients of the quadratic equation. However, I would like to just input

a , b , c in one line. The output should be something like the following:

Enter the coefficients of a quadratic: 1 -5 6
Equation: 1.0x^2 - 5.0x + 6.0
Roots: 3.0 and 2.0

How would I be able to enter 1 -5 6 on the same line?

The code you have written already does exactly what you want: if you enter all three doubles on one line separated by spaces, then calling nextDouble three times will get the three numbers you entered. The reason for this is that Scanner reads tokens separated by any whitespace; they don't have to be separated by newlines. But you do have to press enter for anything to be read, since keyboard input is line-buffered.

If you only enter one or two numbers before pressing enter, it will wait for you to enter the rest on the next line before continuing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM