[英]How to put the sum of two numbers beside the " = "
我想在等號旁邊顯示兩個數字的總和。
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?
}
}
}
如何在等號旁邊顯示答案?
將您的第一個println
更改為print
。
根據您的問題,我認為您很可能會問我們如何在打印語句中顯示兩個數字的總和。 因此,在“=”之后的代碼中,您只需要添加 (i+a) 這將對 i 和 a 的值求和。 System.out.println(i +" + "+ a + " = " + (i+a))
。
我希望這回答了你的問題。
System.out.println()
方法在其輸入后立即打印一個“換行符” ( \\n )。
還有另一種方法不這樣做:
System.out.print()
你應該改變
System.out.println(i +" + "+ a + " = " );
到
System.out.print(i +" + "+ a + " = " );
這個。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.