如何在单独的方法中删除重复操作?
How to remove repetitive operations in a separate method?
我的代码必须在 7 次尝试中猜测从 0 到 100 的隐藏数字。每次我需要再次调用相同的操作时。如何将这些操作移动到一个单独的方法中并从那里调用它们?
public class Main {
public static void main(String[] args) throws IOException {
int min = 0;
int max = 100;
int midrange = Math.round((min + max)/2);
String strInput = "";
Scanner scan = new Scanner(System.in);
while (!strInput.equals("1")){
System.out.println("Guess a number from 0 to 100: I'll guess it in 7 attempts! Enter 1 to continue:");
strInput = scan.nextLine();
}
while (!strInput.equals("+") && !strInput.equals("-") && !strInput.equals("=")){
System.out.println("Is this number greater than, less than or equal to " + midrange + "? " +
"Enter '+', if it's greater, '-' if it's less and '=' if it's equal:");
strInput = scan.nextLine();
}
if (strInput.equals("=")) System.out.println("Great! Thank you for the game.");
else if (strInput.equals("+")){
// reduce the range
min = midrange;
// find a new midrange
midrange = Math.round((min + max)/2);
} else if (strInput.equals("-")){
max = midrange;
midrange = Math.round((min + max)/2);
}
strInput = "";
while (!strInput.equals("+") && !strInput.equals("-") && !strInput.equals("=")){
System.out.println("Is this number greater than, less than or equal to " + midrange + "? ");
strInput = scan.nextLine();
}
// ... and so on until the correct number is found.
}
}
您不需要多个 while 循环。只需检查相等性并将 if-else-block 放入 while 循环。如果你猜对了数字就跳出循环。
public static void main(String[] args) throws IOException {
int min = 0;
int max = 100;
int midrange = Math.round((min + max) / 2);
String strInput = "";
Scanner scan = new Scanner(System.in);
System.out.println("Guess a number from 0 to 100: I'll guess it in 7 attempts! Enter 1 to continue:");
strInput = scan.nextLine();
while (!strInput.equals("=")) {
System.out.println("Is this number greater than, less than or equal to " + midrange + "? " +
"Enter '+', if it's greater, '-' if it's less and '=' if it's equal:");
strInput = scan.nextLine();
if (strInput.equals("=")) {
System.out.println("Great! Thank you for the game.");
break;
} else if (strInput.equals("+")) {
min = midrange;
midrange = Math.round((min + max) / 2);
} else if (strInput.equals("-")) {
max = midrange;
midrange = Math.round((min + max) / 2);
}
}
}
我的代码必须在 7 次尝试中猜测从 0 到 100 的隐藏数字。每次我需要再次调用相同的操作时。如何将这些操作移动到一个单独的方法中并从那里调用它们?
public class Main {
public static void main(String[] args) throws IOException {
int min = 0;
int max = 100;
int midrange = Math.round((min + max)/2);
String strInput = "";
Scanner scan = new Scanner(System.in);
while (!strInput.equals("1")){
System.out.println("Guess a number from 0 to 100: I'll guess it in 7 attempts! Enter 1 to continue:");
strInput = scan.nextLine();
}
while (!strInput.equals("+") && !strInput.equals("-") && !strInput.equals("=")){
System.out.println("Is this number greater than, less than or equal to " + midrange + "? " +
"Enter '+', if it's greater, '-' if it's less and '=' if it's equal:");
strInput = scan.nextLine();
}
if (strInput.equals("=")) System.out.println("Great! Thank you for the game.");
else if (strInput.equals("+")){
// reduce the range
min = midrange;
// find a new midrange
midrange = Math.round((min + max)/2);
} else if (strInput.equals("-")){
max = midrange;
midrange = Math.round((min + max)/2);
}
strInput = "";
while (!strInput.equals("+") && !strInput.equals("-") && !strInput.equals("=")){
System.out.println("Is this number greater than, less than or equal to " + midrange + "? ");
strInput = scan.nextLine();
}
// ... and so on until the correct number is found.
}
}
您不需要多个 while 循环。只需检查相等性并将 if-else-block 放入 while 循环。如果你猜对了数字就跳出循环。
public static void main(String[] args) throws IOException {
int min = 0;
int max = 100;
int midrange = Math.round((min + max) / 2);
String strInput = "";
Scanner scan = new Scanner(System.in);
System.out.println("Guess a number from 0 to 100: I'll guess it in 7 attempts! Enter 1 to continue:");
strInput = scan.nextLine();
while (!strInput.equals("=")) {
System.out.println("Is this number greater than, less than or equal to " + midrange + "? " +
"Enter '+', if it's greater, '-' if it's less and '=' if it's equal:");
strInput = scan.nextLine();
if (strInput.equals("=")) {
System.out.println("Great! Thank you for the game.");
break;
} else if (strInput.equals("+")) {
min = midrange;
midrange = Math.round((min + max) / 2);
} else if (strInput.equals("-")) {
max = midrange;
midrange = Math.round((min + max) / 2);
}
}
}