0

How to multiply positive number with do while rule

Get user input

28th Aug 2022, 3:49 PM
Nguyen Thi Thao
Nguyen Thi Thao - avatar
4 odpowiedzi
+ 3
Can you show what you've written? Then someone can help you.
29th Aug 2022, 7:56 AM
Ausgrindtube
Ausgrindtube - avatar
+ 3
Firstly, you're missing some ";" Look at the line "Thank you for using..." And the While statement. Secondly, if you're while condition is that the number has to be greater than 0, your if condition will always be false. Thirdly, that means you're using the else condition, which makes "mul = 1". And the "int" before "int number" in the else statement needs to be removed. I'm not sure what output you're expecting. It looks to me like the final answer you would receive is "The result is: 1" Here's some of the code fixed: https://code.sololearn.com/cE8kNfXKfb8M/?ref=app
29th Aug 2022, 9:26 AM
Ausgrindtube
Ausgrindtube - avatar
0
import java.util.Scanner; public class Mul { public static void main(String[] args) { int mul ; Scanner input = new Scanner (System .in); System .out.println ("Enter your initial number:"); int number = input.nextInt(); do{ mul *= number; if(number<=0){ mul =0; System .out.println (" Thank for using this program.") }else{ mul=1; System .out.println ("What do you want to multiply by (0 to end)"); int number = input.nextInt();} }while(number>0 ) System .out .println ("The result is:"+ mul); } }
29th Aug 2022, 7:57 AM
Nguyen Thi Thao
Nguyen Thi Thao - avatar
0
Input I want like that : Enter number: 7 Multiply it by : 2 Result : 14 Multiply it by: 3 Result: 42 Multiply it by : 0 Thank for using this program.
29th Aug 2022, 9:52 AM
Nguyen Thi Thao
Nguyen Thi Thao - avatar