loopy
//save as loopy.java
public class loopy {
final static String lineBreak =
System.getProperty("line.separator");
public static void main(String[] args) {
int counter = 5;
//bottom-driven loop happens at least once:
do
{
System.out.print(counter);
counter--;
}
while (counter >= 1);
System.out.print(lineBreak);
//top-driven loops may never happen:
while (counter <= 10)
{
System.out.print(counter);
counter=counter+2;
}
System.out.print(lineBreak);
for(counter=10; counter>=1; counter--)
{
System.out.print(counter);
}
System.out.print(lineBreak);
}
}
guess number
//save as guess.java
import java.util.*;
public class guess {
public static void main(String[] args) {
//SET UP THE VARIABLES FOR THE GAME:
int guessesRemaining = 3;
Random seed = new Random();
int unknownNumber = seed.nextInt(10) + 1;
Scanner myInput = new Scanner(System.in);
int userGuess = 0;
//GIVE THE USER 3 GUESSES TO GET THE NUMBER:
while(guessesRemaining > 0)
{
userGuess = Integer.valueOf(myInput.nextLine());
//IF I GET IT RIGHT:
if (userGuess == unknownNumber)
{
System.out.println("you win");
guessesRemaining = 0;
}
//IF IM LOWER:
if (userGuess < unknownNumber) { System.out.println("higher"); }
//IF IM HIGHER:
if (userGuess > unknownNumber) { System.out.println("lower"); }
guessesRemaining--;
}
}
}