Loop's
#6

K dis;

Okay guys, today we're going to write a program.
This program is going to accept 2 integer parameters, num1 and num2.

Upon running the program, the user will be prompted for two numbers.
The program will then add the two numbers together, if the sum is between 0 and 19, a dialog will be show - otherwise, the user shall receive an error message and be prompted for the two numbers again. For simplicity the prompt will be looped inside a while loop.

I write you java code, dunno if it helps or not :3
Код:
import java.util.Scanner;
public class SumAuth {

  public SumAuth() {
  }
  
  public static void main(String[] args)
  {
  	Scanner scan = new Scanner(System.in);
  	System.out.print("Enter the first number: ");
  	int num1 = scan.nextInt();
  	System.out.print("Enter the second number: ");
  	int num2 = scan.nextInt();
  	
  	int sum = num1+num2;
  	
  	while(sum >= 20 || sum < 0)
		{
			System.out.println("ERROR : Sum of the two numbers is not between 0 and 19");
			System.out.print("Enter the first number: ");
	  		num1 = scan.nextInt();
	  		System.out.print("Enter the second number: ");
	  		num2 = scan.nextInt();
			sum = num1+num2;
		}
		
	System.out.println("suces!");
  }  
  
}
Reply


Messages In This Thread
Loop's - by V1ceC1ty - 05.03.2010, 10:54
Re: Loop's - by Donny_k - 05.03.2010, 13:19
Re: Loop's - by V1ceC1ty - 05.03.2010, 23:21
Re: Loop's - by Donny_k - 06.03.2010, 00:25
Re: Loop's - by V1ceC1ty - 06.03.2010, 00:40
Re: Loop's - by iLinx - 06.03.2010, 01:36
Re: Loop's - by V1ceC1ty - 06.03.2010, 03:16
Re: Loop's - by Donny_k - 06.03.2010, 04:28

Forum Jump:


Users browsing this thread: 1 Guest(s)