Twenty Questions.java
public class TwentyQuestions
{
public static void main(String[] args)
{
int N = 1 + (int) (Math.random() * 1000000);
StdOut.print("I'm thinking of a number ");
StdOut.println("between 1 and 1,000,000");
int m = 0;
while (m != N)
{
StdOut.print("What's your guess? ");
m = StdIn.readInt();
if (m == N) StdOut.println("You win!");
if (m < N) StdOut.println("Too low ");
if (m > N) StdOut.println("Too high");
}
}
}
Average.java
public class Average
{
public static void main(String[] args)
{
int count = 0;
double sum = 0.0;
while (!StdIn.isEmpty())
{
double value = StdIn.readDouble();
sum += value;
count++;
}
double average = sum / count;
StdOut.println("Average is " + average);
}
}
RangeFilter.java
public class RangeFilter
{
public static void main(String[] args)
{
int lo = Integer.parseInt(args[0]);
int hi = Integer.parseInt(args[1]);
while (!StdIn.isEmpty())
{
int t = StdIn.readInt();
if (t >= lo && t <= hi)
{
StdOut.println(t + " ");
}
}
StdOut.println();
}
}
PlotFilter.java
public class PlotFilter
{
public static void main(String[] args)
{
double x0 = StdIn.readDouble();
double y0 = StdIn.readDouble();
double x1 = StdIn.readDouble();
double y1 = StdIn.readDouble();
StdDraw.setXscale(x0, x1);
StdDraw.setYscale(y0, y1);
while (!StdIn.isEmpty())
{
double x = StdIn.readDouble();
double y = StdIn.readDouble();
StdDraw.point(x, y);
}
}
}
No comments:
Post a Comment