The test deadline of April 3, after the end of the experiment will give a complete reference code.
1. How to use the following code template:
1.1 Create a class of the corresponding name in eclipse
1.2 Copy the code into the class file
1.3 Enter the code you use to solve the problem in //todo gaze.
1.4 Example : "show class two name" in thefirst question. That's what everyone can do.
1.4.1 Create a class in eclipse. Named Passorfail.
1.4.2 Copy the following code into the. java file. and delete//todo gaze, start to write code in the while loop (infer whether the score is greater than 60, output, etc.)
1.4.3 the written passorfail.java to the experimental site.
2. Do not understand what indentation is able to reference what is code indent, or discuss with classmates around.
3. Self-active typesetting shortcut keys:CTRL + SHIFT + F (very handy, be sure to try.) Who knows by the way: P)
Show Level two name
The name of the subject is really rotten ... Code:
Import Java.util.scanner;public class Passorfail {public static void main (string[] args) {Scanner in = new Scanner (System. IN), int repeat = In.nextint (); while (repeat--! = 0) {//Todo}}}
Find Minimum value
Tips
1. Minimum value of two digits
int minimal = Math.min (A, b);
Import Java.util.scanner;public class Findminimal {public static void main (string[] args) {Scanner in = new Scanner (System . in); int repeat = In.nextint (), while (repeat--! = 0) {//TodoSystem.out.println ("min is" + min);}}
Finding the area and perimeter of a triangle
Tips
1. If you follow multiple statements, surround the "multiple statements" with curly braces and make them into a "compound statement."
2. Condition A, B's "logic and" relationship
if (Conditiona && conditionb) { //todo} else { //Todo}
3. Condition A, B's "logical OR" relationship
if (Conditiona | | conditionb) { //todo} else { //Todo}
4. Finding the square root
Sqrt method you have used before, to see [Java] Experiment 3 reference code.
Code templates
Import Java.util.scanner;public class Triangle {public static void main (string[] args) {Scanner in = new Scanner (system.in ); int repeat = In.nextint (); while (repeat--! = 0) {Float a = in.nextfloat (); float B = in.nextfloat (); float C = In.nextfloa t ();//Todo}}}
The symbol of the inferred number
Import Java.util.scanner;public class Judgesign {public static void main (string[] args) {Scanner in = new Scanner (system.i n); int repeat = In.nextint (); while (repeat--! = 0) {int num = In.nextint ();//Todo}}}
Calculate personal income Tax
Tips
1. What is initialization
We can go to the group to download the basic knowledge of Java Core Technology Vol. 1 (Original book 9th edition), the "3.4.1 Variable Initialization" section.
2. Why in this problem, some alumni have compiled error:"may use uninitialized variables"
Depending on the syntax, variables must be assigned before reading (Variables must be initialized before used.).
Consider the following code:
Double Rate;if (Conditiona) { rete = 0.05;} else if (condition B) {Rate = 0.1,} else if (condition C) { rate = 0.2;} Double res = rate; COMPILATION Error!!!
What the last line of code does. Is
reading the value of rate, assign this value to res.
According to the syntax, the value of this rate must be initialized before it is read (informally, it can be interpreted as "assignment"). The problem is that, from the compiler's point of view, if branch A, B, and C are not running, then rate may not be assigned. So in double res = rate. Attempting to read the value of rate is illegal.
Some students say that to solve a problem, you can assign a value to it when you define it: double rate = 0.
This will prevent the compiler from error, but it may not be the best solution. Suppose A, B, and C three branches can include the entire condition space . Then I think the code should be written in such a more reasonable way:
Double Rate;if (Conditiona) { rete = 0.05;} else if (condition B) {Rate = 0.1;} else {//There are no if here!!! Rate = 0.2;} Double res = rate;
Program templates
Import Java.util.scanner;public class Tax {public static void main (string[] args) {Scanner in = new Scanner (system.in); int repeat = In.nextint (); while (repeat--! = 0) {Float salary = in.nextfloat ();//TodoSystem.out.println ("tax=" + (int) (TAX * + 0.5)/100d);}}}
Show The price of fruit
Import Java.util.scanner;public class Fruits {public static void main (string[] args) {Scanner in = new Scanner (system.in); int repeat = In.nextint (), while (repeat--! = 0) {System.out.println ("[1] apples"); System.out.println ("[2] pears"); System.out.println ("[3] oranges"); System.out.println ("[4] grapes"); int choice = In.nextint ();//Todo}}}
Letter Conversion
Import java.io.*;p Ublic class Characterconversion {public static void main (string[] args) throws IOException {for (int ch = System.in.read (); ch! = '?'; ch = System.in.read ()) {//TodoSystem.out.print ((char) ch);}}}
Calculate the value of a segment function
Import Java.util.scanner;public class Piecewisefunction {public static void main (string[] args) {Scanner in = new Scanner ( system.in), int repeat = In.nextint (), while (repeat--! = 0) {int x = In.nextint ();d ouble y;//todoSystem.out.println ("F (" + x + ") =" + y);}}}
Finding the root of a two-second equation
Tips
1. Why (int) (x * + 0.5)/100d method sometimes error?
Since this method can only cope with the case of X >= 0, consider x =-2.5, then
X * 100 =-250
-250 + 0.5 =-249.5
(int)-249.5 = 249
-249/100d =-2.49
Notice that. The result we want here is-2.5 instead of-2.49
2. Rounding retains two decimal places
public class Test {public static void Main (string[] args) { double num = 3.1415926535; for (int i = 0; I <=; + + i) { System.out.println (remaindigits (num, i)); } } Static double remaindigits (double num, int digitstoremain) { //e.g. Remaindigits (3.14159, 2) was called and then it would return //Math.Round (3.14159 * 100d)/100d, which equals 3.14 return Math.Round (num * MATH.POW (Digitstoremai N) /Math.pow (ten, Digitstoremain);} }
Import Java.util.scanner;public class Root {public static void main (string[] args) {Scanner in = new Scanner (system.in); T repeat = In.nextint (); while (repeat--! = 0) {int a = In.nextint (); int b = In.nextint (); int c = In.nextint ();//Todo}}}
shows the percentile score interval corresponding to level five score system
Import Java.util.scanner;public class Grade {public static void main (string[] args) {Scanner in = new Scanner (system.in); I NT repeat = In.nextint (); while (repeat--! = 0) {//Todo}}}
[Java] Experiment 5 Reference Code