java语⾔程序设计第五章答案_Java语⾔程序设计(基础篇)
原书第⼗版课后习题第五章...
import java.util.Scanner;
public class Code_Practice_5 {
public static void main(String[] args) {
// 5.1
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer, the input ends if it is 0:");
int number = Int();
int countP = 0, countN = 0;
int total = 0, count = 0;
if (number != 0) {
// 如果写成while(true),会出现Unreachable code的问题,原因没到
while (number != 0) {
if (number > 0)
countP++;
else
countN++;
total += number;
number = Int();
}
double average = total / (countP + countN);
System.out.println("The number of positives is " + countP);
System.out.println("The number of negatives is " + countN);
System.out.println("The total is " + total);
System.out.println("The average is " + average);
}
else
System.out.println("No numbers are entered except 0");
// 5.2
final int NUMBER_OF_QUESTIONS = 5; // Number of questions
int correctCount = 0 ; // Count the correct
int count = 0; // Count the questions
long startTime = System.currentTimeMillis();
String output = " "; // output string is initially empty;
Scanner input = new Scanner(System.in);
while (count < NUMBER_OF_QUESTIONS) {
// Generate two random single-digit integers
int num1 = (int)(1 + Math.random() * 14);
int num2 = (int)(1 + Math.random() * 14);
System.out.print(
"What is " + num1 + "+" + num2 + " ? ");
int answer = Int();
if (num1 + num2 == answer){
System.out.println("You are correct!");
correctCount ++;
}
else
System.out.println("Your answer is wrong.\n" + num1
+ "+" + num2 + " should be " + (num1 + num2));
count ++;
output += "\n" + num1 + "+" + num2 + "=" + answer + ((num1 + num2 == answer) ? " correct" : " wrong");
}
long endTime = System.currentTimeMillis();
long testTime = endTime - startTime;
System.out.println("\nCorrect count is " + correctCount + "\nTest time is " + testTime / 1000 + " seconds\n" + output); // 5.3
int kilo = 1;
double pound = 0;
System.out.println("千克" + " " + " 磅");
for (int i = 0; i < 199; i++) {
System.out.print(kilo);
pound = kilo * 2.2;
System.out.printf("%12.1f\n", pound);
kilo++;
}
// 5.4
int miles = 1;
double kilo = 0;
System.out.println("英⾥" + " " + " 千⽶"); for (int i = 0; i < 10; i++) {
System.out.printf("%-9d",miles);
kilo = miles * 1.609;
System.out.printf("%-7.3f\n", kilo);
miles++;
}
// 5.5
int kilo1 = 1, pound2 = 20;
double pound1 = 1, kilo2 = 1;
System.out.println("千克" + " " + " 磅" + " "+"磅" + " " + " 千克");
for (int i = 0; i < 199; i++) {
System.out.print(kilo1);
pound1 = kilo1 * 2.2;
System.out.printf("%12.1f", pound1);
for (int j = 0; j < 99; j++) {
System.out.printf("%6d",pound2);
kilo2 = pound2 / 2.2;
System.out.printf("%8.2f\n",kilo2); pound2 += 5;
java程序设计基础视频
break;
}
kilo1++;
}
// 5.6
int miles1 = 1, kilo2 = 20;
double kilo1 = 0, miles2 = 0;
System.out.println("英⾥" + " " + " 千⽶" + " " + "千⽶" + " " + " 英⾥");
for (int i = 0; i < 10; i++) {
System.out.printf("%-9d",miles1);
kilo1 = miles1 * 1.609;
System.out.printf("%-7.3f", kilo1);
for (int j = 0; j < 10; j++) {
System.out.printf("%6d",kilo2);
miles2 = kilo2 / 1.609;
System.out.printf("%13.3f\n",miles2);
kilo2 += 5;
break;
}
miles1++;
}
// 5.7
int tuition = 10000;
int year = 0;
int total = 0;
while (year < 14) {
tuition *= 1.05;
year++;
System.out.println(year + " " +tuition);
while (year > 10) {
total += tuition;
break;
}
}
System.out.println(total);
/
/ 5.8
Scanner input = new Scanner(System.in); System.out.print("Enter the number of student:"); int num = Int();
double highestScore = 0, higherScore = 0;
String highestName = "", higherName = "";
for (int count = 0; count < num; count++) {
System.out.print("Enter the " + (count + 1) + " student\'s name:"); String name = ();
System.out.print("Enter the " + (count + 1) + " student\'s score:"); double score = Double();
if (score > highestScore && score > higherScore) {
highestScore = score;
highestName = name;
}
else if (score < highestScore && score > higherScore){ higherScore = score;
higherName = name;
}
}
System.out.println("The best student is " + highestName +
" and the socre is " + highestScore);
System.out.println("The second student is " + higherName +
" and the socre is " + higherScore);
// 5.9
Scanner input = new Scanner(System.in);
// Prompt the user to enter the number of students
System.out.print("Enter the number of students: ");
int numberOfStudents = Int();
System.out.print("Enter a student name: ");
String student1 = ();
System.out.print("Enter a student score: ");
double score1 = Double();
System.out.print("Enter a student name: ");
String student2 = ();
System.out.print("Enter a student score: ");
double score2 = Double();
// Make sure that student1 is for the highest
/
/ and student2 is for the second highest
if (score1 < score2) {
// Swap

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。