用户输入由空格分隔的整数

时间:2016-02-16 16:32:06

标签: java

int a,b,c,d;

system.out.println("enter number");

a=input.nextInt();

system.out.println("enter number");

b=input.nextInt();

system.out.println("enter number");

c=input.nextInt();

d=(a*b)+c;

system.out.println("the total number is" + d);

我的代码有效,但当用户输入a,b和c的值时,它会打印在不同的行上。我希望一次输入所有值,并用空格分隔(例如10 11 13)然后计算。如何将所有这些输入放在一行上,用空格分隔然后计算?

2 个答案:

答案 0 :(得分:3)

扫描仪开箱即用的空间界定。以下代码可以帮助您获得所需内容。

public static void main(String[] args) {
    Scanner myScanner = new Scanner(System.in);
    System.out
            .println("Please enter three integers separated by white space.");
    int myInt1 = myScanner.nextInt();
    int myInt2 = myScanner.nextInt();
    int myInt3 = myScanner.nextInt();
    int mySum = myInt1 + myInt2 + myInt3;

    System.out.println("The sum of " + myInt1 + " + " + myInt2 + " + "
            + myInt3 + " = " + mySum);

    myScanner.close();
}

控制台应显示:

Please enter three integers separated by white space.
1 30 45
The sum of 1 + 30 + 45 = 76

答案 1 :(得分:2)

我发现您的代码没有任何问题。我认为你只需删除额外的set.seed(101) dd <- data.frame(y=rnorm(5), a=1:5,b=2:6,c=3:7,d=letters[1:5]) model <- model.matrix(y~., data=dd) n <- nrow(dd) train <- sample(1:n, size=round(0.8*n)) test <- setdiff(1:n,train) ,它应该可以正常工作。

试试这段代码:

sysouts

输出:

int a,b,c,d;
Scanner input = new Scanner(System.in);
System.out.println("Enter 3 Numbers (Separated By White-space): ");

a = input.nextInt();
b = input.nextInt();
c = input.nextInt();

d = (a*b)+c;

System.out.println("The Total Number: " + d);