Hi For this Simple Input coding challenge, I have tried and have googled too. But I couldn’t figure out where I should modify to get stop after the sum is -. Could you guide me on the current code? Thanks public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int num = s.nextInt();
int sum = 0;
sum = sum + num;
if (sum > 0) {
System.out.println(num);
s.hasNextInt();
}else{
System.out.println(“Invalid negative”);
}
}
Simple Input coding challenge
- Initially sum will be 0.
- Start a while loop where condition is sum>=0.
- Take input of a new int inside the loop and add it to sum.
- If sum becomes less than 0, exit the while loop using break else just print the input you took.
In your code: int num = s.nextInt(); and sum = sum + num; should be inside the while loop because we have to take input until sum >= 0.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.