NEWS: Total Picture Views: 151536 | Total Article Views: 161045 | Top 5 Most Popular Article: 1. Am I Stuck Algorithm with Becker Robot | 2. PickUpThings with Becker Robot | 3. How to read a JSON and work with it | 4. Encrypted Data GUI (Download .Zip) | 5. Create Wall and Things with Becker

Method & Loop with verification Java

Description:

In this example I will be showing how we use method/function in Java

Code:

import java.util.Scanner;

public class Main {


    public static void main(String[] args) {

        int x,y,result,resultUser;
        Scanner input = new Scanner(System.in);
        System.out.println("We are going to do a addition problem");
        System.out.print("Enter your first number: ");
        x = input.nextInt();
        System.out.print("Enter your second number: ");
        y = input.nextInt();

        result = Addition(x,y);

        do{
            System.out.print("Enter the result of " + x + " + " + y + ": " );
            resultUser = input.nextInt();
            if (result != resultUser){
                System.out.println("Bad answer... Try again!");
            }

        }while(result != resultUser);
        System.out.println("You have the correct answer!");

    }


    public static int Addition(int a, int b) {
        int result = (a + b);
        return result;
    }

}
Attachments: None

Tags: method do loop function

Total Views: 810

My name is Jean-Mathieu

I created this website so other people could enjoy finding useful stuff easier. If you have any question do not hesitate to contact me.

jean8mathieuCreated on 09/03/14


affiliate_link

Disclosure: We are a website that needs compensation to operate like any other website on the internet.
We may receive consideration for our reviews but we are totally unbiased and do not accept paid reviews or fake reviews claiming to be something they are not.