I wrote this program, it works fine but doesn't pass the testing. What is my mistake? Please, help.

public class Solution

{ public static void main(String[] args) throws Exception { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String m1 = reader.readLine(); int n1 = Integer.parseInt(m1); String m2 = reader.readLine(); if (m2.equals("total")) { System.out.println(n1); } else { int n2 = Integer.parseInt(m2);

        while (true)
        {
            n1 = n1 + n2;
            String m3 = reader.readLine();
            if (m3.equals("total"))
            {
                System.out.println(n1);
                break;
            }
            else
            {
                int n3 = Integer.parseInt(m3);
                n2 = n3;
            }
        }

    }
}

}

asked 14 Dec '15, 15:47

ksuhak's gravatar image

ksuhak
111
accept rate: 0%


Need to use "сумма" instead of "total". Passed test:

package com.javarush.test.level05.lesson12.home05;
/* Read numbers from keyboard and calculate their total
Read numbers from keyboard and calculate their total until the user enters the word «total». Display to the screen the total.
*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Solution
{
    public static void main(String[] args) throws Exception
    {
        BufferedReader scan = new BufferedReader(new InputStreamReader(System.in));
        int total = 0;
        while(true){
            String s = scan.readLine();
            if(s.equals("сумма")){
                System.out.println(total);
                break;
            }
            total += Integer.parseInt(s);
        }
    }
}
link

answered 18 May, 17:29

Igor's gravatar image

Igor
112
accept rate: 0%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×29
×21
×6

Asked: 14 Dec '15, 15:47

Seen: 634 times

Last updated: 18 May, 17:29