gpt4 book ai didi

Java Codelab While 语句

转载 作者:行者123 更新时间:2023-12-01 10:26:56 26 4
gpt4 key购买 nike

有人可以解释为什么当我输入-99时,程序没有停止吗?当我第一次输入 -99 时,程序停止,但如果我先输入 1,然后第二次输入 -99,程序不会停止

import java.util.Scanner;

public class TESTTEST {
public static void main(String [] args) {
// creates the scanner
Scanner input = new Scanner(System.in);

int number;
int max=0;
int min=0;
int counter = 0;

System.out.print("Enter an integer (-99 to stop): ");
number = input.nextInt();
{
while (number != -99) {
System.out.print("Enter an integer (-99 to stop): ");
number = input.nextInt();

if (number> max ){
number = max;
}
else if (number<max ) {
number = min;
}
}

System.out.println("max value is " +max);
System.out.println("min value is "+min);

}
}
}

最佳答案

您颠倒了数字max/min

if (number> max ){
number = max; //Since max = 0, number turns into 0
}
else if (number<max ) {
number = min; //Same thing here
}
}

System.out.println("max value is " +max);
System.out.println("min value is "+min);

}

切换它们:

if (number> max ){
max = number;
}
else if (number<max ) {
min= number;
}
}

System.out.println("max value is " +max);
System.out.println("min value is "+min);

}

关于Java Codelab While 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35327809/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com