Vous êtes sur la page 1sur 2

Java Programming

Exercises Week 1 (August 2010)

1) Exercise Problem No. 1: Using Loops a Loop in a guessing game. Use of final.
import java.io.*;
public class LoopSample
{
public static void main (String args[]) throws IOException
{
BufferedReader input = new BufferedReader (new InputStreamReader (System.in));
int guess = 0;
final int ans = 35; //final is reserved word used to declare constant values for variables
while (guess != ans)
{
System.out.println("Guess a number: ");
guess = Integer.parseInt(input.readLine());

if (guess == ans)
{
System.out.println("Bingo!");
break;
}
else System.out.println("Try Again");
}
System.out.println("Game Over!");
}
}

2) Exercise Problem No. 2: Level 2 of the game. (Adding a range of input)

import java.io.*;
public class GuessNum2
{
public static void main (String args[]) throws IOException
{
BufferedReader input = new BufferedReader (new InputStreamReader (System.in));
int guess = 0;
final int ans = 35;
while (guess != ans)
{
System.out.println("Guess a number: ");
guess = Integer.parseInt(input.readLine());
if (guess > ans) System.out.println("Lower !");
else if (guess < ans) System.out.println("Higher !");
else {
System.out.println("Bingo ! ");
break;
}
}
System.out.println("GAME OVER");

}
}

1
3) Exercise Problem No. 3: Using sentinel-controlled loop and counter-controlled loop to limit the number of loops.
(use of random class)
import java.io.*;
import java.util.*;
public class Guessnum3
{
public static void main (String args[]) throws IOException
{
BufferedReader input = new BufferedReader (new InputStreamReader (System.in));
Random rand = new Random ();
int guess = 0;
int ans = rand.nextInt(50)+1;
int ctr = 1;
while ((guess != ans) && (ctr <= 5))
{
System.out.println("Guess a number between 1 and 50: ");
guess = Integer.parseInt(input.readLine());
if (guess > ans) System.out.println("Lower !");
else if (guess < ans)System.out.println("Higher !");
else System.out.println("Bingo ! ");
ctr++;
}
if (ctr >5) System.out.println("YOU LOST!");
System.out.println("GAME OVER!");
}
}
4) Exercise Problem No. 4: Reviewing GUI-based output using javax.swing.
import java.util.*;
import javax.swing.*;
public class SwingSample
{
public static void main (String args[])
{
Random rand = new Random ( );
int guess = 0;
int ans = rand.nextInt(50)+1;
int ctr = 1;
JOptionPane.showMessageDialog(null,"Welcome to JAVA ! ");
while ((guess != ans) && (ctr <= 5))
{
String guessStr=JOptionPane.showInputDialog("Guess a number between 1 and 50");
guess=Integer.parseInt(guessStr);
if (guess > ans) JOptionPane.showMessageDialog(null,"Lower !");
else if (guess < ans)JOptionPane.showMessageDialog(null,"Higher !");
else JOptionPane.showMessageDialog(null,"Bingo ! ");
ctr++;
}
if (ctr >5)JOptionPane.showMessageDialog(null,"YOU LOST!");
JOptionPane.showMessageDialog(null,"GAME OVER");
System.exit(0);
}
}
Assignment: Improve the program. Make program request the user to PLAY AGAIN after finishing the
game. “Press 1 to Play Again. And Press 2 to Quit. If the user selects 1, the program/game should
restart and with a new value to guess. If the user quits, the program should say “Thank you for
playing.”. To be submitted next week, during the laboratory session. You should run this next meeting. To be automatically checked in the
computers with interview during the meeting. Print your output if possible.
2

Vous aimerez peut-être aussi