《Java程序设计教程DevCloud》第三章练习答案
·
【示例1】输入一个数,求其平方根。
package javaone.programming.devcloud.chapterthree;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ChapterThreeExampleOne {
public static void main(String[] args) throws IOException {
int x;
double y;
String str;
BufferedReader buf;
System.out.print("Please input a number: ");
buf = new BufferedReader(new InputStreamReader(System.in));
str = buf.readLine();
x = Integer.parseInt(str);
y = Math.sqrt(x);
System.out.println(x + " sqrt value is: " + y);
}
}
【示例2】输入一个数,求其平方根。
package javaone.programming.devcloud.chapterthree;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ChapterThreeExampleTwo {
public static void main(String[] args) throws IOException {
int x;
double y;
String str;
BufferedReader buf;
System.out.print("Please input a number: ");
buf = new BufferedReader(new InputStreamReader(System.in));
str = buf.readLine();
x = Integer.parseInt(str);
if(x>=0){
y = Math.sqrt(x);
System.out.println(x + " sqrt value is: " + y);
}
}
}
【示例3】输入一个数,求其平方根。
package javaone.programming.devcloud.chapterthree;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ChapterThreeExampleThree {
public static void main(String[] args) throws IOException {
int x;
double y;
String str;
BufferedReader buf;
System.out.print("Please input a number: ");
buf = new BufferedReader(new InputStreamReader(System.in));
str = buf.readLine();
x = Integer.parseInt(str);
if(x>=0){
y = Math.sqrt(x);
System.out.println(x + " sqrt value is: " + y);
}
else
System.out.println("Input error!");
}
}
【示例4】编写程序,输入一个成绩,输出成绩的等级。等级划分标准:85分以上为优,75-84分为良,60-74分为中,60分以下为不及格。
package javaone.programming.devcloud.chapterthree;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ChapterThreeExampleFour {
public static void main(String[] args) throws IOException {
int x;
String str;
BufferedReader buf;
System.out.print("Input Student Socre(0~100): ");
buf = new BufferedReader(new InputStreamReader(System.in));
str = buf.readLine();
x = Integer.parseInt(str);
if(x>=85)
System.out.println("Score Excellent");
else if(x>=75)
System.out.println("Socre Good");
else if(x>=60)
System.out.println("Score Pass");
else
System.out.println("Socre Bad");
}
}
【示例5】输入成绩的英文等级A、B、C、D,输出对应的中文等级,即优秀、良好、及格、不及格。
package javaone.programming.devcloud.chapterthree;
import java.io.IOException;
public class ChapterThreeExampleFive {
public static void main(String[] args) throws IOException {
char ch;
System.out.print("Please input level(A、B、C、D): ");
ch = (char) System.in.read();
switch (ch){
case 'A':
case 'a':
System.out.println("Score Excellent");
break;
case 'B':
case 'b':
System.out.println("Score Good");
break;
case 'C':
case 'c':
System.out.println("Score Pass");
break;
case 'D':
case 'd':
System.out.println("Score Bad");
break;
default:
System.out.println("Input Error!");
}
}
}
【示例6】用户从键盘输入字符,直到输入“#”时程序结束。要求:输入字符后显示输入字符的ASCII值并最终统计出输入字符的个数。
package javaone.programming.devcloud.chapterthree;
import java.io.IOException;
public class ChapterThreeExampleSix {
public static void main(String[] args) throws IOException {
char ch;
int count = 0;
System.out.println("Please input a character, '#' for end: ");
ch = (char) System.in.read();
while (ch != '#') {
System.out.println("character: " + ch +", ASCII value is: " + (int) ch);
System.in.skip(2);
count = count + 1;
ch = (char) System.in.read();
}
System.out.println("input character count: " + count);
}
}
【示例7】对示例6进行改编,用do-while语句实现。
package javaone.programming.devcloud.chapterthree;
import java.io.IOException;
public class ChapterThreeExampleSeven {
public static void main(String[] args) throws IOException {
char ch;
int count = 0;
System.out.println("Please input a character, '#' for end: ");
do{
ch = (char) System.in.read();
System.out.println("character: " + ch +", ASCII value is: " + (int) ch);
System.in.skip(2);
count = count + 1;
}while(ch != '#');
System.out.println("input character count: " + count);
}
}
【示例8】计算1+2+3+...+100的值。
package javaone.programming.devcloud.chapterthree;
public class ChapterThreeExampleEight {
public static void main(String[] args) {
functionOne();
functionTwo();
functionThree();
}
static void functionOne(){
int i, sum;
sum = 0;
for (i = 1; i <= 100; i++) {
sum = sum + i;
}
System.out.println("1+2+3+...+100="+sum);
}
static void functionTwo(){
int i, sum;
for (sum=0, i = 1; i <= 100; sum = sum + i,i++);
System.out.println("1+2+3+...+100="+sum);
}
static void functionThree(){
int i, sum;
i = 1;
sum = 0;
for(;;){
sum = sum + i;
if(i>=100)
break;
i++;
}
System.out.println("1+2+3+...+100="+sum);
}
}
【示例9】求1~1000的所有完全数。完全数时等于其所有因子和的数。
package javaone.programming.devcloud.chapterthree;
public class ChapterThreeExampleNine {
public static void main(String[] args) {
int i,j,sum;
for(i=1;i<1000;i++){
sum=0;
for(j=1;j<i;j++){
if(i%j==0){
sum=sum+j;
}
}
if(sum==i){
System.out.print(i+"\t");
}
}
System.out.println();
}
}
【示例10】在一维数组中找出指定的数。
package javaone.programming.devcloud.chapterthree;
public class ChapterThreeExampleTen {
public static void main(String[] args) {
int[] array = {10,78,57,89,37,64,5,23,45,76};
int find = 5;
int i = 0;
boolean flag = false;
for(;i<array.length;i++){
if(array[i] == find){
flag = true;
break;
}
}
if(flag==true)
System.out.println("Found "+ find + " at index:" + i);
else
System.out.println("not found!");
}
}
【输入11】找出2~100的所有素数。
package javaone.programming.devcloud.chapterthree;
public class ChapterThreeExampleElevem {
public static void main(String[] args) {
int i,j;
loop:
for(i=2;i<=100;i++){
for(j=2;j<i;j++){
if(i%j==0){
continue loop;
}
}
if(j>=i)
System.out.print(i+"\t");
}
}
}
课后习题
1. 输入一批整数,输出所有整数之和,输入数字0时结束循环。
package javaone.programming.devcloud.chapterthree;
import java.util.Scanner;
public class ChapterThreeExerciseOne {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int total = 0;
while(true){
System.out.println("Enter an integer(0 for end): ");
int number = scanner.nextInt();
if(number == 0){
break;
}
total += number;
}
System.out.println("Total is " + total);
scanner.close();
}
}
2. 输入一个数,求其平方根。
package javaone.programming.devcloud.chapterthree;
import java.util.Scanner;
public class ChapterThreeExerciseTwo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Input a non-negative integer: ");
double number = scanner.nextDouble();
if(number < 0){
System.out.println("Input error!");
}else{
double squareRoot = Math.sqrt(number);
System.out.printf("The square root of %.2f is %.2f\n", number, squareRoot);
}
}
}
3. 编写一个程序,从键盘输入一位整数,当输入1~7时,显示对应的英文星期名称的缩写。1表示MON,2表示TUE,3表示WED,4表示THU,5表示FRI,6表示SAT,7表示SUN,输入其他数字时,提示用户重新输入,输入数字0时程序结束。
package javaone.programming.devcloud.chapterthree;
import java.util.Scanner;
public class ChapterThreeeExerciseThree {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
boolean running = true;
while (running) {
System.out.print("Enter number between 1 and 7 (0 for end): ");
int number = scanner.nextInt();
switch (number){
case 1:
System.out.println("MON");
break;
case 2:
System.out.println("TUE");
break;
case 3:
System.out.println("WED");
break;
case 4:
System.out.println("THU");
break;
case 5:
System.out.println("FRI");
break;
case 6:
System.out.println("SAT");
break;
case 7:
System.out.println("SUN");
break;
case 0:
System.out.println("program end!");
running = false;
break;
default:
System.out.println("Invalid number!");
}
}
scanner.close();
}
}
更多推荐


所有评论(0)