如何確保0不作爲最小值?我已經確定答案不是一個負數,但現在我不知道如何排除0作爲輸入,當它被按下以便找到最小值時。Java程序接受int值直到用戶輸入一個零,然後找到最小正整數
import java.util.Scanner;
public class Q2
{
public static void main (String [] args)
{
System.out.println("Write a list of integers and type 0 when you are finished");
Scanner kb=new Scanner(System.in);
int input=kb.nextInt();
int smallestValue=Integer.MAX_VALUE;
boolean negative=false;
do{
input=kb.nextInt();
if(!negative){
smallestValue=input;
negative=false;
}
if (input<smallestValue){
smallestValue=input;
}
}
while(input!=0);
System.out.println(smallestValue+">0");
}
}
謝謝,Aditya –
Np。如果解決了您的問題,請接受答案 –