打印Java Scanner在print()语句之前要求用户输入
我正在跟随我的大学课程,逐字逐句地写代码。我遇到了一个问题,print()
语句将被跳过,用户必须输入两个不同的输入,直到输出打印消息(参见下面的输出)。我之所以不使用println()
,是因为要求键盘输入与print语句在同一行。当我使用println()
语句时,它工作,但在println()
输出下输出键盘输入
以下是代码:
import java.util.Scanner;
public class temperature {
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter today's temperature: ");
int today = keyboard.nextInt();
System.out.print("Enter tomorrow's temperature: ");
int tomorrow = keyboard.nextInt();
System.out.println("Today is " + today + " degrees.);
System.out.println("Tomorrow is " + tomorrow + " degrees.");
}
}
此操作的输出为:
12
13
Enter today's temperature: Enter tomorrow's temperature: Today is 12 degrees.
Tomorrow is 13 degrees.
所需的输出:
Enter today's temperature:12
Enter tomorrow's temperature:13
Today is 12 degrees.
Tomorrow is 13 degrees.
有没有办法在不太修改代码的情况下解决这个问题
共 (0) 个答案