问题是要求用户输入两个不同的数字,然后JAVA随即打乱输出包含在这两个数之间的所有整数,然后计算最大和第二大的数字中间夹着的数字和。例子:Enteranumber:6Enteranumber:-35,4,0,-2...
问题是 要求用户输入两个不同的数字,然后JAVA随即打乱输出包含在这两个数之间的所有整数,然后计算最大和第二大的数字中间夹着的数字和。例子:
Enter a number: 6
Enter a number: -3
5,4,0,-2,1,-3,6,-1,3,2
Largest pair sum: 9
输入数字的一部分我已经完成,剩下的部分实在有点不会....求各位大大指教!!
附上已完成的部分= =
import java.io.*;
import java.util.Random;
public class A6{
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
boolean same = false;
int num1 = 0;
int num2 = 0;
while (!same) {//if the numbers which user enter are the same,do it again
try {
System.out.print("enter a number: ");//ask user a number
num1 = Integer.parseInt(in.readLine());
System.out.print("enter another number: ");//ask user another number
num2 = Integer.parseInt(in.readLine());
if (num1 != num2) {
same = true;//if the number are different,continue the next part and don't need to enter again
} else {
System.out.println("two numbers is same.");//if not , ask user enter again
}
} catch (NumberFormatException nfc) {//text the enter is a number or not,if not,ask user enter again
System.out.println("not a number");//show user the reason why get false
}
}
最后一个要求错了.....应该是找出相加结果最大的相邻的两个数字,然后把结果打印出来
展开