浙大JAVA-实验题答案09answer
浙大java练习题答案

40001import class Test40001 {public static void main(String[] args) { int ri, repeat;int i, n;float sum;Scanner in=new Scanner;repeat=();for(ri=1; ri<=repeat; ri++){n=();/*--------------------*/}}}40002import class Test40002 {public static void main(String[] args) { int ri, repeat;int i, n;double fact;Scanner in=new Scanner;repeat=();for(ri=1; ri<=repeat; ri++){n=();/*--------------------*/}}}40003import class Test40003 {int ri, repeat;int i, n;double x, mypow;Scanner in=new Scanner;repeat=();for(ri=1; ri<=repeat; ri++){x=();n=();/*--------------------*/}}}40004import class Test40004 {int ri, repeat;int i, n, flag;float sum;Scanner in=new Scanner;repeat=();for(ri=1; ri<=repeat; ri++){n=();/*--------------------*/}}}40005import class Test40005 {public static void main(String[] args) {int ri, repeat;int i, n, temp;float sum;Scanner in=new Scanner;repeat=();for(ri=1; ri<=repeat; ri++){n=();/*--------------------*/sum=0;for(i=1;i<=n;i++){sum=(float) (sum+(2*i-1));}}}}40006import class Test40006 {public static void main(String[] args) { int ri, repeat;int temp, flag;double eps, item, sum;Scanner in=new Scanner;repeat=();for(ri=1; ri<=repeat; ri++){eps=();/*--------------------*/}}}40007import class Test40007 {public static void main(String[] args){ int ri, repeat;int begin, c, end, f;Scanner in=new Scanner;repeat=();for(ri=1; ri<=repeat; ri++){begin=();end=();"Celsius Fahrenheit");/*--------------------*/" "+f);}}}}40008import class Test40008 {public static void main(String[] args){ int ri, repeat;int x, sum;Scanner in=new Scanner;repeat=();for(ri=1; ri<=repeat; ri++){x=();/*--------------------*/}}}}40009import class Test40009 {public static void main(String[] args){ int ri, repeat;int i , max, n, x;Scanner in=new Scanner;repeat=();for(ri=1; ri<=repeat; ri++){n=();/*--------------------*/}}}}40010import class Test40010 {public static void main(String[] args){int ri, repeat;int number, sum,n,r;Scanner in=new Scanner;repeat=();for(ri=1; ri<=repeat; ri++){n=();/*--------------------*/"number="+number+", sum="+sum);}}}40011import class Test40011 {public static void main(String[] args) { int ri, repeat;int i,n;float a,b,s,t;Scanner in=new Scanner;repeat=();for(ri=1; ri<=repeat; ri++){n=();/*--------------------*/}}}40012import class Test40012{public static void main(String args[]){int i, n, a, sn, tn;Scanner in=new Scanner;repeat=();for(ri=1; ri<=repeat; ri++){a=();n=();/*--------------------*/}}}40013import class Test40013{public static void main(String args[]){ int ri, repeat;boolean flag=true;Scanner in=new Scanner;repeat=();for(ri=1; ri<=repeat; ri++){m=();/*--------------------*/}if(flag) "YES");else"NO");}}}40014import .*;public class Test40014 {public static void main(String []args){Scanner in =new Scanner;int gcd, lcm, m, n,r;int repeat, ri;repeat=();for(ri = 1; ri <= repeat; ri++){m=();n=();if(m <= 0 || n <= 0)"m <= 0 or n <= 0");else{/*---------*/"the least common multiple:"+lcm+", the greatest common divisor:"+gcd);}}}}40021import class Test40021{public static void main(String args[]){ int ri, repeat;int i,n;float s,t;Scanner in=new Scanner;repeat=();for(ri=1; ri<=repeat; ri++){n=();/*--------------------*/}}}40022import class Test40022{public static void main(String args[]){ int ri, repeat;int i, digit, m, n, number, sum;Scanner in=new Scanner;repeat=();for(ri=1; ri<=repeat; ri++){m=();n=();/*------------------*/}}40023import class Test40023{public static void main(String args[]){int ri, repeat;int count, i, j, k, m, n, sum;Scanner in=new Scanner;repeat=();for(ri=1; ri<=repeat; ri++){m=();n=();/*---------------------*/"count="+count+", sum="+sum);}}40031import class Test40031{public static void main(String []args ){ int ri, repeat,count, word,i;String line;char c;Scanner in=new Scanner;repeat=()).charAt(0)-'0'; harAt(0);/*---------*/}}}40034import class Test40034{public static void main(String []args){ int year,m,n,repeat,ri;Scanner in=new Scanner;repeat=();for(ri=1;ri<=repeat;ri++){m=();n=();/*---------*/}}}40035import class Test40035{public static void main(String []args){ int m,n,repeat,ri;Scanner in=new Scanner;repeat=();for(ri=1;ri<=repeat;ri++){n=();m=();/*---------*/}}}40036import class Test40036{public static void main(String []args){ int days,repeat,ri;Scanner in=new Scanner;repeat=();for(ri=1;ri<=repeat;ri++){}}}40037import class Test40037{public static void main(String []args){ int a,n,ri,count,number;double sum,ave;Scanner in=new Scanner;n=();for(ri=1;ri<=n;ri++){/。
浙大JAVA实验题答案13answer

实验13字符串处理(二)和类的设计1.程序填空题,不要改变与输入输出有关的语句。
以下题目请采用字符串处理来实现:40010求整数的位数以及各位数之和输入一个正整数repeat (0<repeat<10),做repeat次下列运算:输入一个整数,输出它的位数以及各位数之和。
例:括号内是说明输入4 (repeat=4)123456 -100 -1 99输出number=6, sum=21 (123456的位数是6,各位数之和是21)number=3, sum=1 (-100的位数是3,各位数之和是1)number=1, sum=1 (-1的位数是1,各位数之和是1)number=2, sum=18 (99的位数是2,各位数之和是18)import java.util.Sca nner;public class Test40010 {public static void main( Stri ng[] args){int ri, repeat;int nu mber, sum,n;Scanner in=new Scann er(System.i n);repeat=in.n extI nt();for(ri=1; ri<=repeat; ri++){n=in.n extI nt();}}}50003统计一个整数中数字的个数输入一个正整数repeat (0<repeat<10),做repeat次下列运算:读入1个整数,统计并输出该数中2的个数。
要求定义并调用函数countdigit(number,digit) ,它的功能是统计整数number中数字digit的个数。
例如,countdigit(10090,0) 的返回值是3。
例:括号内是说明输入:3 (repeat=3)-219022345543输出:count=2 (-21902 中有2 个2)count=1 ( 有1 个2)count=0 (345543 中没有2)import java.util.Sca nner;public class Test50003{public static void main( Stri ngargs[]){ int ri, repeat;int count;long n;Scanner in=new Sca nn er(System.i n); repeat=in.n extI nt();for(ri=1; ri<=repeat; ri++){n=in.n extI nt();n=Math. abs (n);count= coun tdigit (n ,2);System.out.pri ntl n("cou nt="+co unt);50009将一个整数逆序输出输入一个正整数repeat (0<repeat<10),做repeat次下列运算:输入一个整数,将它逆序输出。
JAVA练习题含答案-answertopratice3

JAVA练习题含答案-answertopratice3Chapter 3Flow of ControlMultiple Choice1)An if selection statement executes if and only if:(a)the Boolean condition evaluates to false.(b)the Boolean condition evaluates to true.(c)the Boolean condition is short-circuited.(d)none of the above.Answer:B (see page 97)2) A compound statement is enclosed between:(a)[ ](b){ }(c)( )(d)< >Answer:B (see page 98)3) A multi-way if-else statement(a)allows you to choose one course of action.(b)always executes the else statement.(c)allows you to choose among alternative courses of action.(d)executes all Boolean conditions that evaluate to true.Answer:C (see page 100)4)The controlling expression for a switch statement includes all of the following types except:(a)char(b)int(c)byte(d)doubleAnswer:D (see page 104)Copyright ? 2006 Pearson Education Addison-Wesley. All rights reserved. 125)To compare two strings lexicographically the String method ____________ should be used.(a)equals(b)equalsIgnoreCase(c)compareTo(d)==Answer:C (see page 112)6)When using a compound Boolean expression joined by an && (AND) in an if statement:(a)Both expressions must evaluate to true for the statement to execute.(b)The first expression must evaluate to true and the second expression must evaluate to false forthe statement to execute.(c)The first expression must evaluate to false and the second expression must evaluate to true forthe statement to execute.(d)Both expressions must evaluate to false for the statement to execute.Answer:A (see page 116)7)The OR operator in Java is represented by:(a)!(b)&&(c)| |(d)None of the aboveAnswer:C (see page 116)8)The negation operator in Java is represented by:(a)!(b)&&(c)| |(d)None of the aboveAnswer:A (see page 116)9)The ____________ operator has the highest precedence.(a)*(b)dot(c)+=(d)decrementAnswer:B (see page 123)10)The looping mechanism that always executes at least once is the _____________ statement.(a)if…else(b)do…while(c)while(d)forAnswer:B (see page 132).Chapter 3 Flow of Control 311) A mixture of programming language and human language is known as:(a)Algorithms(b)Recipes(c)Directions(d)PseudocodeAnswer:D (see page 134)12)When the number of repetitions are known in advance, you should use a ___________ statement.(a)while(b)do…while(c)for(d)None of the aboveAnswer:C (see page 141)13)To terminate a program, use the Java statement:(a)System.quit(0);(b)System.end(0);(c)System.abort(0);(d)System.exit(0);Answer:D (see page 148)True/False1)An if-else statement chooses between two alternative statements based on the value of a Booleanexpression.Answer:True (see page 96)2)You may omit the else part of an if-else statement if no alternative action is required.Answer:True (see page 97)3)In a switch statement, the choice of which branch to execute is determined by an expression given inparentheses after the keyword switch.Answer:True (see page 104)4)In a switch statement, the default case is always executed.Answer:False (see page 104)5)Not including the break statements within a switch statement results in a syntax error.Answer:False (see page 104)6)The equality operator (==) may be used to test if two string objects contain the same value.Answer:False (see page 111)47)Boolean expressions are used to control branch and loop statements.Answer:True (see page 116)8)The for statement, do…while statement and while statement are examples of branching mechanisms.Answer:False (see page 130)9)An algorithm is a step-by-step method of solution.Answer:True (see page 134)10)The three expressions at the start of a for statement are separated by two commas.Answer:False (see page 137)Short Answer/Essay1)What output will be produced by the following code?public class SelectionStatements{public static void main(String[] args){int number = 24;if(number % 2 == 0)System.out.print("The condition evaluated to true!");elseSystem.out.print("The condition evaluated to false!");}}Answer:The condition evaluated to true!.Chapter 3 Flow of Control 52)What would be the output of the code in #1 if number was originally initialized to 25?Answer:The condition evaluated to false!3)Write a multi-way if-else statement that evaluates a persons weight on the following criteria: Aweight less than 115 pounds, output: Eat 5 banana splits! A weight between 116 pounds and 130 pounds, output: Eat a banana split! A weight between 131 pounds and 200 pounds, output: Perfect!A weight greater than 200 pounds, output: Plenty of banana splits have been consumed!Answer:if(weight <= 115)System.out.println("Eat 5 banana splits!");else if(weight <= 130)System.out.println("Eat a banana split!");else if(weight <=200)System.out.println("Perfect!");elseSystem.out.println("Plenty of banana splits have been consumed!");4)Write an if-else statement to compute the amount of shipping due on an online sale. If the cost ofthe purchase is less than $20, the shipping cost is $5.99. If the cost of the purchase over $20 and at most $65, the shipping cost is $10.99. If the cost of the purchase is over $65, the shipping cost is $15.99.Answer:if(costOfPurchase < 20)shippingCost = 5.99;else if((costOfPurchase > 20)&&(costOfPurchase <= 65))shippingCost = 10.99;elseshippingCost = 15.99;5)Evaluate the Boolean equation: !( ( 6 < 5) && (4 < 3))Answer:True66)Write Java code that uses a do…while loop that prints even numbers from 2 through 10.Answer:int evenNumber = 2;do{System.out.println(evenNumber);evenNumber += 2;}while(evenNumber <= 10);7)Write Java code that uses a while loop to print even numbers from 2 through 10.Answer:int evenNumber = 2;while(evenNumber <= 10){System.out.println(evenNumber);evenNumber += 2;}Answer:8)Write Java code that uses a for statement to sum the numbers from 1 through 50. Display the totalsum to the console.Answer:.Chapter 3 Flow of Control 7 int sum = 0;for(int i=1; i <= 50; i++){sum += i;}System.out.println("The total is: " + sum);9)What is the output of the following code segment?public static void main(String[] args){int x = 5;System.out.println("The value of x is:" + x);while(x > 0){x++;}System.out.println("The value of x is:" + x);}Answer:.10)Discuss the differences between the break and the continue statements when used in loopingmechanisms.Answer:When the break statement is encountered within a looping mechanism, the loopimmediately 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.8Programming projects:1. Write a complete Java program that prompts the user fora series of numbers to determine the smallestvalue entered. Before the program terminates, display the smallest value.Answer:import java.util.Scanner;public class FindMin{public static void main(String[] args){Scanner keyboard = new Scanner(System.in);int smallest = 9999999;String userInput;boolean quit = false;System.out.println("This program finds the smallest number"+ " in a series of numbers");System.out.println("When you want to exit, type Q");.Chapter 3 Flow of Control 9 while(quit != true){System.out.print("Enter a number: ");userInput = keyboard.next();if(userInput.equals("Q") || userInput.equals("q")){quit = true;}else{int userNumber = Integer.parseInt(userInput);if(userNumber < smallest)smallest = userNumber;}}System.out.println("The smallest number is " + smallest);System.exit(0);}10}2. Write a complete Java program that uses a for loop to compute the sum of the even numbers and thesum of the odd numbers between 1 and 25.public class sumEvenOdd{public static void main(String[] args){int evenSum = 0;int oddSum = 0;//loop through the numbersfor(int i=1; i <= 25; i++){if(i % 2 == 0){//even numberevenSum += i;}else{oddSum += i;.Chapter 3 Flow of Control 11 }}//Output the resultsSystem.out.println("Even sum = " + evenSum);System.out.println("Odd sum = " + oddSum);}}/*** 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* 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){// Variable declarationsint numWins = 0;12int numLosses = 0;int i;int roll;int point;// Play 10,000 gamesfor (i=0; i<="" p="">{// Simulate rolling the two dice, with values from 1-6roll = (int) (Math.random() * 6) + (int) (Math.random() * 6) + 2;// Check for initial win or lossif ((roll == 7) || (roll == 11)){numWins++;}else if ((roll==2) || (roll==3) || (roll==12)){numLosses++;}else{// Continue rolling until we get the point or 7point = roll;do{roll = (int) (Math.random() * 6) + (int) (Math.random() * 6) +2;if (roll==7){numLosses++;}else if (roll==point){numWins++;}} while ((point != roll) && (roll != 7));}}// Output probability of winningSystem.out.println("In the simulation, we won " + numWins +" times and lost " + numLosses + " times, " +" for a probability of " +(double) (numWins) / (numWins+numLosses));}} // Question2.Chapter 3 Flow of Control 13 /*** Question6.java** Created: Sun Nov 09 16:14:44 2003* Modified: Sat Mar 05 2005, Kenrick Mock** @author Adrienne Decker* @version 2*/import java.util.Scanner;public class Question6{public static void main(String[] args){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 " +"calculate any more enter -1.");int initialSize = keyboard.nextInt();if ( initialSize == -1){break;} // end of if ()System.out.println("Enter the number of days: ");int numDays = keyboard.nextInt();int numOfRepCycles = numDays / 5;int prevPrevGen = 0;int prevGen = initialSize;int finalAnswer = initialSize;for ( int i = 0; i < numOfRepCycles; i++ ){finalAnswer = prevPrevGen + prevGen;14prevPrevGen = prevGen;prevGen = finalAnswer;} // end of for ()System.out.println("The final amount of green crud will be: "+ finalAnswer + " pounds.\n");} // end of while ()}} // Question6/*** Question7.java** Created: Sun Nov 09 16:14:44 2003* Modified: Sat Mar 05 2005, Kenrick Mock** @author Adrienne Decker* @version 2*/import java.util.Scanner;public class Question7{public static void main(String[] args){Scanner keyboard = new Scanner(System.in);String line;do{System.out.println("Enter the value of X for this calculation."); double x = keyboard.nextDouble();double sum = 1.0;double temp = 1.0;for ( int n = 1; n <= 10; n++){System.out.print("For n equal to: " + n.Chapter 3 Flow of Control 15+ ", the result of calculating e^x is: ");for ( int inner = 1; inner <= n; inner++){temp = 1.0;for ( double z = inner; z > 0; z--){temp = temp * (x/z);} // end of for ()sum += temp;} // end of for ()System.out.println(sum);sum = 1.0;} // end of for ()System.out.print("For n equal to 50, the result of " + "calculating e^x is: ");for ( int n = 1; n <= 50; n++){temp = 1.0;for ( double z = n; z > 0; z--){temp = temp * (x/z);} // end of for ()sum += temp;} // end of for ()System.out.println(sum);sum = 1;System.out.print("For n equal to 100, the result of " + "calculating e^x is: ");for ( int n = 1; n <= 100; n++){temp = 1.0;for ( double z = n; z > 0; z--){temp = temp * (x/z);} // end of for ()sum += temp;} // end of for ()System.out.println(sum);System.out.println("\nEnter Q to quit or Y to go again.");16keyboard.nextLine(); // Clear bufferline = keyboard.nextLine();} while (line.charAt(0)!='q' && line.charAt(0)!='Q'); // end of while () }} // Question7.。
浙大JAVA-实验题答案08answerdoc资料

浙大J A V A-实验题答案08a n s w e r实验8 Method的使用1.程序填空题,不要改变与输入输出有关的语句。
50001 求1 + 1/2! +....+ 1/n!输入一个正整数repeat (0<repeat<10),做repeat次下列运算:输入1 个正整数n,计算 s 的前n项的和(保留 4 位小数)。
s = 1 + 1/2! +....+ 1/n!要求定义并调用函数fact(n)计算n的阶乘。
例:括号内是说明输入:2 (repeat=2)2 (n=2)10 (n=10)输出:1.51.7183import java.util.Scanner;public class Test50001 {public static void main(String[] args) {int ri,repeat;int i,n;double s;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1;ri<=repeat;ri++){n=in.nextInt();/*-----------*/s=0;for(i=1;i<=n;i++)s+=1.0/fact(i);System.out.println((long)(s*10000+0.5)/10000.);}}/*---------------*/static double fact(int n) {int i;double f=1;for(i=1;i<=n;i++)f*=i;return f;}}50002 求a+aa+aaa+aa…a输入一个正整数repeat (0<repeat<10),做repeat次下列运算:输入2个正整数a和n, 求a+aa+aaa+aa…a(n个a)之和。
要求定义并调用函数fn(a,n),它的功能是返回aa…a(n个a)。
java习题及答案第9章 习题参考答案

第9章习题解答1.与输入/输出有关的流类有哪些?答:与输入/输出有关的流类主要有InputStream、OutputStream和Reader、Writer类及其子类。
除此以外,与流有关的类还有File类、FileDescriptor类、StreamTokenizer类和RandomAccessFile类。
2.字节流与字符流之间有哪些区别?答:字节流是面向字节的流,流中的数据以8位字节为单位进行读写,是抽象类InputStream和OutputStream的子类,通常用于读写二进制数据,如图像和声音。
字符流是面向字符的流,流中的数据以16位字符(Unicode字符)为单位进行读写,是抽象类Reader和Writer的子类,通常用于字符数据的处理。
3.什么是节点流?什么是处理流或过滤流?分别在什么场合使用?答:一个流有两个端点。
一个端点是程序;另一个端点可以是特定的外部设备(如键盘、显示器、已连接的网络等)和磁盘文件,甚至是一块内存区域(统称为节点),也可以是一个已存在流的目的端。
流的一端是程序,另一端是节点的流,称为节点流。
节点流是一种最基本的流。
以其它已经存在的流作为一个端点的流,称为处理流。
处理流又称过滤流,是对已存在的节点流或其它处理流的进一步处理。
对节点流中的数据只能按字节或字符读写。
当读写的数据不是单个字节或字符,而是一个数据块或字符串等时,就要使用处理流或过滤流。
4.标准流对象有哪些?它们是哪个类的对象?答:标准流对象有3个,它们是:System.in、System.out和System.err。
System.in 是InputStream类对象,System.out和System.err是PrintStream类对象。
5.顺序读写与随机读写的特点分别是什么?答:所谓顺序读写是指在读写流中的数据时只能按顺序进行。
换言之,在读取流中的第n个字节或字符时,必须已经读取了流中的前n-1个字节或字符;同样,在写入了流中n-1个字节或字符后,才能写入第n个字节或字符。
浙大java练习题答案

40001import java.util.Scanner;public class Test40001 {public static void main(String[] args) {int ri, repeat;int i, n;float sum;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){n=in.nextInt();/*--------------------*/System.out.println((int)(sum*1000+0.5)/1000.);}}}40002import java.util.Scanner;public class Test40002 {public static void main(String[] args) { int ri, repeat;int i, n;double fact;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){n=in.nextInt();/*--------------------*/System.out.println(fact);}}}40003import java.util.Scanner;public class Test40003 {public static void main(String[] args) { int ri, repeat;int i, n;double x, mypow;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){x=in.nextDouble();n=in.nextInt();/*--------------------*/System.out.println(mypow);}}}40004import java.util.Scanner;public class Test40004 {public static void main(String[] args) {int ri, repeat;int i, n, flag;float sum;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){n=in.nextInt();/*--------------------*/System.out.println((long)(sum*10000+0.5)/10000.);}}}40005import java.util.Scanner;public class Test40005 {public static void main(String[] args) {int ri, repeat;int i, n, temp;float sum;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){n=in.nextInt();/*--------------------*/sum=0;for(i=1;i<=n;i++){sum=(float) (sum+1.0/(2*i-1));}System.out.println(sum);}}}40006import java.util.Scanner;public class Test40006 {public static void main(String[] args) { int ri, repeat;double eps, item, sum;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){eps=in.nextDouble();/*--------------------*/System.out.println((int)(sum*10000+0.5)/10000.);}}}40007import java.util.Scanner;public class Test40007 {public static void main(String[] args){int begin, c, end, f;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){begin=in.nextInt();end=in.nextInt();System.out.println("Celsius Fahrenheit");/*--------------------*/System.out.println(c+" "+f);}}}}40008import java.util.Scanner;public class Test40008 {public static void main(String[] args){ int ri, repeat;int x, sum;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){x=in.nextInt();/*--------------------*/}System.out.println(sum);}}}40009import java.util.Scanner;public class Test40009 {public static void main(String[] args){ int ri, repeat;int i , max, n, x;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){n=in.nextInt();/*--------------------*/}System.out.println(max);}}40010import java.util.Scanner;public class Test40010 {public static void main(String[] args){int ri, repeat;int number, sum,n,r;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){n=in.nextInt();/*--------------------*/System.out.println("number="+number+", sum="+sum);}}40011import java.util.Scanner;public class Test40011 {public static void main(String[] args) {int ri, repeat;int i,n;float a,b,s,t;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){n=in.nextInt();/*--------------------*/System.out.println((int)(s*10000+.5)/10000.);}}}40012import java.util.Scanner;public class Test40012{public static void main(String args[]){int ri, repeat;int i, n, a, sn, tn;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){a=in.nextInt();n=in.nextInt();/*--------------------*/System.out.println(sn);}}40013import java.util.Scanner;public class Test40013{public static void main(String args[]){int ri, repeat;int i, m, n;boolean flag=true;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){m=in.nextInt();/*--------------------*/if(flag) System.out.println("YES");else System.out.println("NO");}}}40014import java.util.*;public class Test40014 {public static void main(String []args){Scanner in =new Scanner(System.in);int gcd, lcm, m, n,r;int repeat, ri;repeat=in.nextInt();for(ri = 1; ri <= repeat; ri++){m=in.nextInt();n=in.nextInt();if(m <= 0 || n <= 0)System.out.println("m <= 0 or n <= 0");else{/*---------*/System.out.println("the least common multiple:"+lcm+", the greatest common divisor:"+gcd);}}}}40021import java.util.Scanner;public class Test40021{public static void main(String args[]){int ri, repeat;int i,n;float s,t;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){n=in.nextInt();/*--------------------*/System.out.println((int)(s*10000+0.5)/10000.);}}}40022import java.util.Scanner;public class Test40022{int ri, repeat;int i, digit, m, n, number, sum;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){m=in.nextInt();n=in.nextInt();/*------------------*/}}}40023import java.util.Scanner;public class Test40023{int ri, repeat;int count, i, j, k, m, n, sum;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){m=in.nextInt();n=in.nextInt();/*---------------------*/System.out.println("count="+count+", sum="+sum);}}}40031import java.util.Scanner;public class Test40031{public static void main(String []args ){int ri, repeat,count, word,i;String line;char c;Scanner in=new Scanner(System.in);repeat=(in.nextLine()).charAt(0)-'0'; //输入repeatfor(ri=1; ri<=repeat; ri++){line=in.nextLine(); //输入一行字符/*---------*/System.out.println(count);}}}40032import java.util.Scanner;public class Test40032{public static void main(String []args ){int ri, repeat;int digit;long n, temp, pow;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){n=in.nextLong();/*---------*/System.out.println();}}}import java.util.Scanner;public class Test40033{public static void main(String args[]) {int ri, repeat;int op1, op2, res;char operator;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){op1=in.nextInt();operator =(in.next()).charAt(0);/*---------*/System.out.println(res);}}40034import java.util.Scanner;public class Test40034{public static void main(String []args){ int year,m,n,repeat,ri;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1;ri<=repeat;ri++){m=in.nextInt();n=in.nextInt();/*---------*/}}40035import java.util.Scanner;public class Test40035{public static void main(String []args){ int m,n,repeat,ri;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1;ri<=repeat;ri++){n=in.nextInt();m=in.nextInt();/*---------*/}}}40036import java.util.Scanner;public class Test40036{public static void main(String []args){ int days,repeat,ri;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1;ri<=repeat;ri++){}}}40037import java.util.Scanner;public class Test40037{public static void main(String []args){int a,n,ri,count,number;double sum,ave;Scanner in=new Scanner(System.in);n=in.nextInt();for(ri=1;ri<=n;ri++){/。
java课后习题及答案
java课后习题及答案Java课后习题及答案Java作为一门广泛应用于软件开发领域的编程语言,其学习和掌握对于计算机专业的学生来说是非常重要的。
在学习过程中,课后习题是巩固知识、提高编程能力的重要环节。
本文将为大家提供一些常见的Java课后习题及其答案,希望能对大家的学习有所帮助。
一、基础习题1. 编写一个Java程序,输出"Hello, World!"。
```javapublic class HelloWorld {public static void main(String[] args) {System.out.println("Hello, World!");}}```2. 编写一个Java程序,计算并输出1到100之间的所有偶数的和。
```javapublic class SumOfEvenNumbers {public static void main(String[] args) {int sum = 0;for (int i = 2; i <= 100; i += 2) {sum += i;System.out.println("1到100之间的所有偶数的和为:" + sum); }}```3. 编写一个Java程序,判断一个整数是否是素数。
```javapublic class PrimeNumber {public static void main(String[] args) {int num = 17;boolean isPrime = true;for (int i = 2; i <= Math.sqrt(num); i++) {if (num % i == 0) {isPrime = false;break;}}if (isPrime) {System.out.println(num + "是素数");} else {System.out.println(num + "不是素数");}}```二、进阶习题1. 编写一个Java程序,实现一个简单的计算器,能够进行加、减、乘、除四则运算。
浙江大学java课本的某些答案
第1章P.161.PC机上不能直接运行Java程序,应先安装JDK,并在Windows的环境变量Path中增加JDK 的安装路径,然后需要编译Java源程序生成.class文件,才可运行。
2.使用命令javac编译Java程序,使用命令java运行编译后的结果。
6.System.out.println() 执行输出操作,输出指定内容后换行。
第2章P.29一、概念思考题1.Java对变量命名的规定:由字母、数字、_和$组成,首字符必须为字母、“_”或“$”,不能是数字,长度不限,区分字母大小写,不能与Java关键字相同。
2.Java的基本数据类型有:逻辑(boolean)、字节(byte)、短整数(short)、整数(int)、长整数(long)、浮点数(float)、双精度浮点数(double)、字符(char)。
其中,整数类型有4种:byte、short、int、long二、程序理解题1.System.out.println("34+30="+34+30); //34+30=3430System.out.println("34+30="+(34+30)); //34+30=64,注意与这一句的区别2.int count=10; //10count*=count+2; //120count/=100; //1count++; //23.int res,n1=12,n2=24,n3=16,n4=3;double d,v1=10.0,v2=3.1416;res=n1/n4; //4d=n1/n4; //4.0res=n3/n4; //5d=n3/n4; //5.0d=v1/n4; //3.3333333333333335d=v1/v2; //3.183091418385536res=n1/n2; //0d=(double)n1/n2; //0.5d=n1/(double)n2; //0.5d=(double)(n1/n2); //0.0第3章P.50一、概念思考题2. 如果在switch语句中的某个case中没有以break结尾会发生什么情况?如果没有break语句,接着执行下一个case子句,直到遇到一个break,或者switch结束为止。
浙大java实验题答案09answer
实验9-1 Method的使用(二)1.程序填空题,不要改变与输入输出有关的语句。
50010 十进制转换二进制输入一个正整数 repeat (0<repeat<10),做 repeat 次下列运算:输入1 个正整数n,将其转换为二进制后输出。
要求定义并调用函数 dectobin(n),它的功能是输出 n 的二进制。
例如,调用dectobin(10),输出1010。
输出语句:..+ 1/n!输入一个正整数repeat (0<repeat<10),做repeat次下列运算:输入1 个正整数n,计算 s 的前n项的和(保留 4 位小数)。
s = 1 + 1/2! +....+ 1/n!要求定义并调用函数fact(n)计算n的阶乘。
例:括号内是说明输入:2 (repeat=2)2 (n=2)10 (n=10)输出:import class Test50001 {public static void main(String[] args) {int ri,repeat;int i,n;double s;Scanner in=new Scanner;repeat=();for(ri=1;ri<=repeat;ri++){n=();/*-----------*/s=0;for(i=1;i<=n;i++)s+=fact(i);}}/*---------------*/static double fact(int n) {....要求定义并调用函数fib(n),它的功能是返回第n项Fibonacci数。
例如,fib(7)的返回值是13。
输出语句:" ");例:括号内是说明输入:3 (repeat=3)1 10 (m=1, n=10)20 100 (m=20, n=100)1000 6000 (m=1000, n=6000)输出:1 123 5 8 (1到10之间的Fibonacci数)21 34 55 89 (20到100之间的Fibonacci数)1597 2584 4181 (1000到6000之间的Fibonacci数)import class Test50006{public static void main(String args[]){ int ri,repeat;int i, m, n;long f;Scanner in=new Scanner;repeat=();for(ri=1; ri<=repeat; ri++){m=();n=();/*---------*/i=1;f=1;while(f<=n){if(f>=m) " ");i++;f=fib(i);}}}/*------------*/static long fib(int n){ //递归方法if(n==1||n==2) return 1;else return fib(n-1)+fib(n-2);}}实验9-2 一维数组的使用1.求平均值输入一个正整数repeat (0<repeat<10),做repeat次下列运算:输入一个正整数n (1<n≤10),再输入n个整数,输出平均值。
浙江大学java上机参考答案48页word文档
一、求两个数的和与差。
程序填空,不要改变与输入输出有关的语句。
输入整数a和b,计算并输出a、b的和与差。
import java.io.*;import java.util.Scanner;public class Test20001{public static void main(String args[]){int a, b, sum, diff;Scanner in=new Scanner(System.in);a=in.nextInt();b=in.nextInt();sum=a+b;diff=a-b;System.out.println("The sum is "+sum);System.out.println("The difference is "+diff);二、求平方根。
程序填空,不要改变与输入输出有关的语句。
输入1个实数x,计算并输出其平方根。
例:输入1.21输出The square root of 1.21 is 1.1import java.io.*;import java.util.Scanner;public class Test20002{public static void main(String args[]){double x, root;Scanner in=new Scanner(System.in);x=in.nextDouble();r oot=Math.sqrt(x);System.out.println("The square root of "+x+" is "+root);三、华氏温度转换为摄氏温度。
程序填空,不要改变与输入输出有关的语句。
输入华氏温度f,计算并输出相应的摄氏温度c。
c = 5/9(f-32).例:括号内是说明:输入17.2 (华氏温度)输出The temprature is -8.222222222222223 import java.util.Scanner;public class Test20003 {public static void main(String[] args) {Scanner in=new Scanner(System.in);double f, c;f=in.nextDouble();c=5.0/9*(f-32);System.out.println("The temprature is "+c);四、计算旅途时间。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
浙大JAVA-实验题答案09answer实验9-1 Method的使用(二)1.程序填空题,不要改变与输入输出有关的语句。
50010 十进制转换二进制输入一个正整数 repeat (0<repeat<10),做 repeat 次下列运算:输入1 个正整数n,将其转换为二进制后输出。
要求定义并调用函数 dectobin(n),它的功能是输出 n 的二进制。
例如,调用dectobin(10),输出1010。
输出语句:System.out.print(t); //t为某位二进制数例:括号内是说明输入:3 (repeat=3)15100输出:11111100100import java.util.Scanner;public class Test50010{public static void main(String args[]){int ri,repeat;int i,n;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1;ri<=repeat;ri++){n=in.nextInt();/*---------*/dectobin(n);System.out.println();}}/*---------*/static void dectobin(int n){String t=""; //保存二进制数do {t=n%2+t; //n除2后的余数拼接到t的前面n=n/2; //获得除2后的商}while(n>0);System.out.print(t); //本方法无返回值,需要在方法体中输出结果 }}说明:本题中方法dectobin(n)的输出虽然与要求有所出入,但上传是正确的。
以下用递归算法实现方法的设计:50001 求1 + 1/2! +....+ 1/n!输入一个正整数repeat (0<repeat<10),做repeat次下列运算:输入1 个正整数n,计算 s 的前n项的和(保留 4 位小数)。
s = 1 + 1/2! +....+ 1/n!要求定义并调用函数fact(n)计算n的阶乘。
例:括号内是说明输入:2 (repeat=2)2 (n=2)10 (n=10)输出:1.51.7183import java.util.Scanner;public class Test50001 {public static void main(String[] args) {int ri,repeat;int i,n;double s;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1;ri<=repeat;ri++){n=in.nextInt();/*-----------*/s=0;for(i=1;i<=n;i++)s+=1.0/fact(i);System.out.println((long)(s*10000+0.5)/10000.);}}/*---------------*/static double fact(int n) {//递归方法if(n==1)return 1;else return n*fact(n-1);}}50002 求a+aa+aaa+aa…a输入一个正整数repeat (0<repeat<10),做repeat次下列运算:输入2个正整数a和n, 求a+aa+aaa+aa…a(n个a)之和。
要求定义并调用函数fn(a,n),它的功能是返回aa…a(n个a)。
例如,fn(3,2)的返回值是33。
例:括号内是说明输入2 (repeat=2)2 3 (a=2, n=3)8 5 (a=8, n=5)输出246 (2+22+222)98760 (8+88+888+8888+88888)import java.util.Scanner;public class Test50002{public static void main(String args[]){int ri, repeat;int i, n,a;long sn;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){a=in.nextInt();n=in.nextInt();/*------------*/sn=0;for(i=1;i<=n;i++)sn+=fn(a,i);System.out.println(sn);}}/*------------*/static int fn(int a,int n){ //递归方法if (n==1)return a;else return fn(a,n-1)*10+a;}}50006 输出 Fibonacci 序列输入一个正整数repeat (0<repeat<10),做repeat次下列运算:输入2 个正整数m和n(1<=m,n<=10000),输出m 和n之间所有的Fibonacci数。
Fibonacci 序列(第1项起):1 1 2 3 5 8 13 21 ......要求定义并调用函数fib(n),它的功能是返回第n项Fibonacci数。
例如,fib(7)的返回值是13。
输出语句:System.out.print(f+" ");例:括号内是说明输入:3 (repeat=3)1 10 (m=1, n=10)20 100 (m=20, n=100)1000 6000 (m=1000, n=6000)输出:1 123 5 8 (1到10之间的Fibonacci数)21 34 55 89 (20到100之间的Fibonacci数)1597 2584 4181 (1000到6000之间的Fibonacci数)import java.util.Scanner;public class Test50006{public static void main(String args[]){int ri,repeat;int i, m, n;long f;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){m=in.nextInt();n=in.nextInt();/*---------*/i=1;f=1;while(f<=n){if(f>=m) System.out.print(f+" ");i++;f=fib(i);}System.out.println();}}/*------------*/static long fib(int n){ //递归方法if(n==1||n==2) return 1;else return fib(n-1)+fib(n-2);}}实验9-2 一维数组的使用1.求平均值输入一个正整数repeat (0<repeat<10),做repeat次下列运算:输入一个正整数n (1<n≤10),再输入n个整数,输出平均值。
例:括号内是说明输入2 (repeat=2)3 1 2 -65 12 2 5 4 0输出aver=-1.0aver=4.6import java.util.Scanner;public class Test60001{public static void main(String []args){int ri, repeat;int i, n, sum,a[];float aver;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){n=in.nextInt();a=new int[n];for(i=0; i<n; i++)a[i]=in.nextInt();//这个循环输入数组各元素/*--------------*/sum=0;for(i=0; i<n; i++) //这个循环实现累加sum+=a[i];aver=(float)sum/n; //求平均值,注意要先把sum转换成float,再计算System.out.println("aver="+aver);}}}2.求最大值及其下标输入一个正整数repeat (0<repeat<10),做repeat次下列运算:输入一个正整数n (1<n≤10),再输入n个整数,输出最大值极其下标(设最大值惟一,下标从0开始)。
例:括号内是说明输入3 (repeat=3)3 1 6 43 10 8 15 1 2 5 4 0输出max=6,index=1 (最大值6的下标是1)max=10,index=0 (最大值10的下标是0)max=5,index=2 (最大值5的下标是2)import java.util.Scanner;public class Test60002{public static void main(String []args){int ri, repeat;int i, index, n, a[];Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){n=in.nextInt();a=new int[n] ;for(i=0; i<n; i++)a[i]=in.nextInt();/*--------------*/index=0; //用index保存最大数的下标,开始假设a[0]是最大数for(i=1; i<n; i++)if(a[index]<a[i]) index=i;//a[i]与当前最大数a[index]比较,若a[i]更大,index变为iSystem.out.println("max="+a[index]+",index="+index); }}}3.逆序输出输入一个正整数repeat (0<repeat<10),做repeat次下列运算:输入一个正整数n (1<n≤10),再输入n个整数,按逆序输出这些数。
例:括号内是说明输入2 (repeat=2)4 10 8 1 25 1 2 5 4 0输出2 1 8 100 4 5 2 1import java.util.Scanner;public class Test60003{public static void main(String []args){int ri, repeat;int i, n, temp,a[];Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){n=in.nextInt();a=new int[n];for(i=0; i<n; i++)a[i]=in.nextInt();/*--------------*/for(i=0; i<n/2; i++){ //a[i]与a[n-1-i]交换,注意交换次数 temp=a[i];a[i]=a[n-1-i];a[n-1-i]=temp;}for(i=0; i<n; i++)//输出一个数组System.out.print(a[i]+" ");System.out.println();}}}4.交换最小值和最大值输入一个正整数repeat (0<repeat<10),做repeat次下列运算:输入一个正整数n,再输入n个整数,将最小值与第一个数交换,最大值与最后一个数交换,然后输出交换后的n个数。