23 May 2011 ~ 0 Comments

User sum test – Java

Well, it’s been a busy day! I’ve been busy going through “Java in Two Semester” and have just coded a program that asks a user to enter two numbers and then asks the user to guess the sum of the two numbers. If they get it right, a well done message is displayed, if its wrong then a sympathy message is shown:-

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import java.util.*; // Ask for a user to input two numbers, work out the sum and ask for a guess

public class AskSum {
public static void main(String[] args)
	{
	Scanner keyboard = new Scanner(System.in);
	int num1, num2, sum, guess;
	System.out.println("***Guess the number game***");
	System.out.println();
	System.out.println("Please enter the first number: ");
	num1 = keyboard.nextInt();
	System.out.println("Please enter the second number: ");
	num2 = keyboard.nextInt();

	System.out.println("Now guess the total: ");
	guess = keyboard.nextInt();

	sum = num1 + num2;
	if (guess == sum)
	{
		System.out.println("Well done, yu guessed right, the answer was: " + sum );
		System.out.println("With your guess off: " + guess);
	}

	else
	{
		System.out.println("Sorry, you got the answer wrong, the right answer was: " + sum);
		System.out.println("With your guess off: " + guess);

	}
	System.out.println("Thanks for trying - Give it another go?");

	}
}

Enjoy – hope it helps someone

Be Sociable, Share!

Related posts (By Relevance):

  1. Child Discount – Java Example

Leave a Reply

show
 
close
#oGame still play it after so many years, if anyone is #bored give it a go http://t.co/3HKrOZtR