当前位置:
文档之家› JAVA练习题含答案-answer to pratice 3
JAVA练习题含答案-answer to pratice 3
1)
The condition evaluated to false!
2)
Write an if-else statement to compute the amount of shipping due on an online sale. If the cost of the purchase is less than $20, the shipping cost is $5.99. If the cost of the purchaseover $20andat most$65, the shipping cost is $10.99. If the cost of the purchase is over $65, the shipping cost is $15.99.
roll = (int) (Math.random() * 6) + (int) (Math.random() * 6) + 2;
// Check for initial win or loss
if ((roll == 7) || (roll == 11))
{
numWins++;
}
else if ((roll==2) || (roll==3) || (roll==12))
"calculate any more enter -1.");
int initialSize = keyboard.nextInt();
{
Scanner keyboard = new Scanner(System.in);
while ( true )
{
System.out.println("Enter the initial size of the green crud"
+ " in pounds.\nIf you don't want to " +
executes all Boolean conditions that evaluate to true.
A
4)
char
int
byte
double
A
5)
equals
equalsIgnoreCase
compareTo
==
A
6)
Both expressions must evaluate to true for the statement to execute.
Programming projects:
1.
import java.util.Scanner;
public class FindMin
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
}
}
/**
* Question2.java
*
* This program simulates 10,000 games of craps.
* It counts the number of wins and losses and outputs the probability
* of winning.
*
* Created: Sat Mar 05, 2005
*
* @author Kenrick Mock
* @version 1
*/
public class Question2
{
private static final int NUM_GAMES = 10000;
/**
* This is the main method. It loops 10,000 times, each simulate
{
if(i % 2 == 0)
{
//even number
evenSum += i;
}
else
{
oddSum += i;
}
}
//Output the results
System.out.println("Even sum = " + evenSum);
System.out.println("Odd sum = " + oddSum);
* a game of craps. Math.random() is used to get a random number,
* and we simulate rolling two dice (Math.random() * 6 + 1 simulates
* a single die).
*/
public static void main(String[] args)
do…while
while
for
A
11)
Algorithms
Recipes
Directions
Pseudocode
A
12)
while
do…while
for
None of the above
A
13)
System.quit(0);
System.end(0);
System.abort(0);
System.exit(0);
" times and lost " + numLosses + " times, " +
" for a probability of " +
(double) (numWins) / (numWins+numLosses));
}
} // Question2
/**
* Question6.java
*
* Created: Sun Nov 09 16:14:44 2003
Chapter 3
1
1)
the
the
the
none of the above.
A
2)
[ ]
{ }
( )
< >
A
3)
allows you to choose one course of action.
always executes the else statement.
allows you to choose among alternative courses of action.
}
A
8)
When the break statement is encountered within a looping mechanism, the loop immediately terminates. When the continue statement is encountered within a looping mechanism, the current iteration is terminated, and execution continues with the next iteration of the loop.
The first expression must evaluate to true and the second expression must evaluate to false for the statement to execute.
The first expression must evaluate to false and the second expression must evaluate to true for the statement to execute.
Both expressions must evaluate to false for the statement to execute.
A
7)
!
&&
| |
None of the above
A
8)
!
&&
| |
None of the above
A
9)
*
dot
+=
decrement
A
10)
if…else
{
numLosses++;
}
else if (roll==point)
{
numWins++;
}
} while ((point != roll) && (roll != 7));
}
}
// Output probability of winning
System.out.println("In the simulation, we won " + numWins +
{
numLosses++;
}
else
{
// Continue rolling until we get the point or 7
point = roll;
do
{
roll = (int) (Math.random() * 6) + (int) (Math.random() * 6) + 2;
if (roll==7)
evenNumber+=2;
}
6)
7)
publicstaticvoidmain(String[]args)
{
intx=5;
System.out.println("The value of x is:"+x);