早教吧作业答案频道 -->其他-->
如何在JAVA中随机打乱输出一组数字而不重复要求用户输入两个不同的数字,然后JAVA随即打乱输出包含在这两个数之间的所有整数,然后计算最大和第二大的数字中间夹着的数字和.例子:Enteranu
题目详情
如何在JAVA中随机打乱输出一组数字而不重复
要求用户输入两个不同的数字,然后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
}
}
最后一个要求错了.应该是找出相加结果最大的相邻的两个数字,然后把结果打印出来
要求用户输入两个不同的数字,然后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
}
}
最后一个要求错了.应该是找出相加结果最大的相邻的两个数字,然后把结果打印出来
▼优质解答
答案和解析
这是后改的:
int length = num1-num2;
int i = 0;
int j = num1;
if(length
int length = num1-num2;
int i = 0;
int j = num1;
if(length
看了 如何在JAVA中随机打乱输出...的网友还看了以下:
观察下面一列数,探究其规律:-1/2,2/3,-3/4,4/5,-5/6,6/7.(1)写出第7, 2020-04-09 …
有相等表面积的球和正方体,它们的体积记为V球和V正,球的直径为d,正方体的棱长为a,则它们的大小关 2020-05-15 …
谁能把我把这些公式弄成手写公式s(t)=at^2/2+v(0)t=(v(t)^2-v(0)^2)/ 2020-05-16 …
(1)若等差数列{an}的首项为a1=C11−2m5m-A2m−211−3m(m∈N*),公差是( 2020-06-11 …
v=kx的V-T图像一质点由静止开始沿直线运动,运动的速度大小为v,位移大小为x,v与x存在如下关 2020-06-19 …
u=arctanv,当u>=0,为什么是v>=0而不是v为任意实数tanu=v,u>=0,tanu 2020-07-07 …
1.存在一个整数j=10,设计4个线程,每个线程执行十次循环操作,其中两个线程每次对j增加5,另外 2020-07-21 …
3道填空-|||①负数的相反数是正数,把这句话用符号可以表示为()②下列说法中,正确的是().A、无 2020-10-30 …
相反数问题?下面这道题我不知道该怎么做:推理:(1)由书中知识,+5的相反数是-5,-5的相反数是5 2020-11-06 …
关于浮力的推算问题教科书上写“当P液大于P物时,有P液gV排大于P物gV物,即F浮大于G物,物体会上 2020-11-16 …