[英]How to put the sum of two numbers beside the " = "
I want to display the sum of two numbers beside the equal sign.我想在等号旁边显示两个数字的总和。
Scanner scan = new Scanner(System.in);
int i ;
System.out.println("enter a number: " );
i = scan.nextInt();
int a = i - 1 ;
while(a >= 1){
System.out.println(i +" + "+ a + " = " );
//i want to display the sum of two numbers beside the equal sign.
i =i + a ;
System.out.println(i);
a --;
// how can I display the answer beside the equal sign?
}
}
}
How can I display the answer beside the equal sign?如何在等号旁边显示答案?
将您的第一个println
更改为print
。
As per your question I think you are most probably asking how we can show the sum of two numbers in the print statement.根据您的问题,我认为您很可能会问我们如何在打印语句中显示两个数字的总和。 So in your code after "=" you just need to add (i+a) this will sum the value of i and a.
因此,在“=”之后的代码中,您只需要添加 (i+a) 这将对 i 和 a 的值求和。
System.out.println(i +" + "+ a + " = " + (i+a))
. System.out.println(i +" + "+ a + " = " + (i+a))
。
I hope this answers your question.我希望这回答了你的问题。
System.out.println()
method prints a "newline character" ( \\n ) right after its' input. System.out.println()
方法在其输入后立即打印一个“换行符” ( \\n )。
There is another method that does not do this:还有另一种方法不这样做:
System.out.print()
You should change你应该改变
System.out.println(i +" + "+ a + " = " );
to到
System.out.print(i +" + "+ a + " = " );
this.这个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.