Java 打印最大值和最小值,检查程序
我一直在尝试编写一个程序来打印5个整数类型变量的最小值和最大值,不使用数组,只使用这种技术进行交换Java 打印最大值和最小值,检查程序,java,Java,我一直在尝试编写一个程序来打印5个整数类型变量的最小值和最大值,不使用数组,只使用这种技术进行交换 if (x > y) { int tmp = x; x = y; y = tmp; } 现在我想到了这一点(虽然我知道只有6笔掉期交易是可能的,但我无法理解): 扫描仪输入=新扫描仪(System.in); int a=input.nextInt(); int b=input.nextInt(); int c=input.nextInt(); int d=inpu
if (x > y) {
int tmp = x;
x = y;
y = tmp;
}
现在我想到了这一点(虽然我知道只有6笔掉期交易是可能的,但我无法理解):
扫描仪输入=新扫描仪(System.in);
int a=input.nextInt();
int b=input.nextInt();
int c=input.nextInt();
int d=input.nextInt();
int e=input.nextInt();
//在a中始终存储最大值,在b中存储最小值
if(a
现在应该可以了,但我还需要编写一段代码来确保程序正常运行,这样我就可以检查所有包含0和1的组合。
i、 e 2^5=32个组合。如果其中一个失败,打印出来。
我尝试过这样做:
for (int a = 0; a <= 1; a++) {
for(int b = 0; b <= 1; b++) {
for(int c = 0; c <= 1; c++) {
for(int d = 0; d <= 1; d++) {
for(int e = 0; e <= 1; e++) {
if (a < b){
int tmp = a;
a = b;
b = tmp;
}
if (a < c) {
int tmp = a;
a = c;
c = tmp;
}
if (a < d) {
int tmp = a;
a = d;
d = tmp;
}
if (a < e) {
int tmp = a;
a = e;
e = tmp;
}
if (c < b) {
int tmp = b;
b = c;
c = tmp;
}
if (d < b) {
int tmp = b;
b = d;
d = tmp;
}
if (e < b) {
int tmp = b;
b = e;
e = tmp;
}
System.out.println(b + "\n" + a);
}
}
}
}
}
对于(inta=0;a我会使用Math.min(int,int)
和Math.max(int,int)
以及一长串调用。比如
Scanner input = new Scanner(System.in);
int a = input.nextInt();
int b = input.nextInt();
int c = input.nextInt();
int d = input.nextInt();
int e = input.nextInt();
int min = Math.min(Math.min(Math.min(Math.min(a, b), c), d), e);
int max = Math.max(Math.max(Math.max(Math.max(a, b), c), d), e);
你的“交换一段代码”毫无意义
Scanner input = new Scanner(System.in);
int a = input.nextInt();
int b = input.nextInt();
int c = input.nextInt();
int d = input.nextInt();
int e = input.nextInt();
int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE;
min = (a < min) ? a : min;
min = (b < min) ? b : min;
min = (c < min) ? c : min;
min = (d < min) ? d : min;
min = (e < min) ? e : min;
max = (a > max) ? a : max;
max = (b > max) ? b : max;
max = (c > max) ? c : max;
max = (d > max) ? d : max;
max = (e > max) ? e : max;
扫描仪输入=新扫描仪(System.in);
int a=input.nextInt();
int b=input.nextInt();
int c=input.nextInt();
int d=input.nextInt();
int e=input.nextInt();
int min=Integer.MAX\u值,MAX=Integer.min\u值;
最小值=(a最大值)?a:最大值;
最大值=(b>最大值)?b:最大值;
最大值=(c>最大值)?c:最大值;
最大值=(d>最大值)?d:最大值;
max=(e>max)?e:max;
我会使用Math.min(int,int)
和Math.max(int,int)
和一长串调用。比如
Scanner input = new Scanner(System.in);
int a = input.nextInt();
int b = input.nextInt();
int c = input.nextInt();
int d = input.nextInt();
int e = input.nextInt();
int min = Math.min(Math.min(Math.min(Math.min(a, b), c), d), e);
int max = Math.max(Math.max(Math.max(Math.max(a, b), c), d), e);
你的“交换一段代码”毫无意义
Scanner input = new Scanner(System.in);
int a = input.nextInt();
int b = input.nextInt();
int c = input.nextInt();
int d = input.nextInt();
int e = input.nextInt();
int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE;
min = (a < min) ? a : min;
min = (b < min) ? b : min;
min = (c < min) ? c : min;
min = (d < min) ? d : min;
min = (e < min) ? e : min;
max = (a > max) ? a : max;
max = (b > max) ? b : max;
max = (c > max) ? c : max;
max = (d > max) ? d : max;
max = (e > max) ? e : max;
扫描仪输入=新扫描仪(System.in);
int a=input.nextInt();
int b=input.nextInt();
int c=input.nextInt();
int d=input.nextInt();
int e=input.nextInt();
int min=Integer.MAX\u值,MAX=Integer.min\u值;
最小值=(a最大值)?a:最大值;
最大值=(b>最大值)?b:最大值;
最大值=(c>最大值)?c:最大值;
最大值=(d>最大值)?d:最大值;
max=(e>max)?e:max;
为什么不将a、b、c、d、e值存储在最里面的循环中,并在交换代码后将其重新分配回变量?就像这样
public static void main(String args[]) {
for (int a=0 ;a<=1 ;a++ ){
for(int b=0 ;b<=1 ;b++ ){
for(int c=0 ;c<=1 ;c++ ){
for(int d=0 ;d<=1 ;d++ ){
for(int e=0 ; e<=1 ;e++ ){
// Store the variable values
int ap = a;
int bp = b;
int cp =c;
int dp = d;
int ep = e;
// Swapping logic for finding min and max
// Due to swapping, the values of a,b,c,d,e may change
if ( a < b){
int tmp = a;
a=b;
b=tmp;
}
if ( a < c){
int tmp = a;
a=c;
c=tmp;
}
if ( a < d){
int tmp = a;
a=d;
d=tmp;
}
if ( a < e){
int tmp = a;
a=e;
e=tmp;
}
if ( c < b){
int tmp = b;
b=c;
c=tmp;
}
if ( d < b){
int tmp = b;
b=d;
d=tmp;
}
if ( e < b){
int tmp = b;
b=e;
e=tmp;
}
System.out.print(a+""+b+""+c+""+d+""+e+" ");
System.out.println(b + " " + a);
// Reassign the variable values
a = ap;
b = bp;
c=cp;
d=dp;
e=ep;
}
}
}
}
}
}
publicstaticvoidmain(字符串参数[]){
对于(int a=0;a为什么不将a、b、c、d、e值存储在最内部的循环中,并在交换代码后将其重新分配回变量?就像这样
public static void main(String args[]) {
for (int a=0 ;a<=1 ;a++ ){
for(int b=0 ;b<=1 ;b++ ){
for(int c=0 ;c<=1 ;c++ ){
for(int d=0 ;d<=1 ;d++ ){
for(int e=0 ; e<=1 ;e++ ){
// Store the variable values
int ap = a;
int bp = b;
int cp =c;
int dp = d;
int ep = e;
// Swapping logic for finding min and max
// Due to swapping, the values of a,b,c,d,e may change
if ( a < b){
int tmp = a;
a=b;
b=tmp;
}
if ( a < c){
int tmp = a;
a=c;
c=tmp;
}
if ( a < d){
int tmp = a;
a=d;
d=tmp;
}
if ( a < e){
int tmp = a;
a=e;
e=tmp;
}
if ( c < b){
int tmp = b;
b=c;
c=tmp;
}
if ( d < b){
int tmp = b;
b=d;
d=tmp;
}
if ( e < b){
int tmp = b;
b=e;
e=tmp;
}
System.out.print(a+""+b+""+c+""+d+""+e+" ");
System.out.println(b + " " + a);
// Reassign the variable values
a = ap;
b = bp;
c=cp;
d=dp;
e=ep;
}
}
}
}
}
}
publicstaticvoidmain(字符串参数[]){
对于(int a=0;aI也可以,但我必须使用那段交换代码。这是一个赋值我也可以,但我必须使用那段交换代码。这是一个赋值我在你的“测试”中看到的问题所有的组合都是,你改变了你在for循环中使用的变量。为了交换和计算你的min/max,你应该在其他变量中保存所需的值(编辑:几乎像mettleap在他的回答中所做的那样)我在你的“测试”中看到的问题对于所有的组合,您都需要更改for循环中使用的变量。为了交换和计算最小值/最大值,您应该将所需的值保存在其他变量中(编辑:几乎像mettleap在回答中所做的那样)但这是如何测试代码的?比如,如果程序不能正常工作,它如何打印组合,或者如果它对所有组合都能正常工作,它如何打印“已验证”之类的内容?但是,这是如何测试代码的?比如,如果程序不能正常工作,它如何打印组合,或者如果程序正常工作,它如何打印“已验证”之类的内容所有的组合都正确吗?